-
Notifications
You must be signed in to change notification settings - Fork 1
/
K-Pangrams.cpp
56 lines (50 loc) · 1.11 KB
/
K-Pangrams.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public:
bool kPangram(string str, int k) {
unordered_map<char,int> m;
int extra=0;
// calculate the characters present in excess
for(int i=0;i<str.size();i++)
{
if(!isalpha(str[i]))
{
continue;
}
if(m[str[i]]==1)
{
extra++;
}
else{
m[str[i]]=1;
}
}
// calculate the total characters brought in
int t=0;
for(char ch='a';ch<='z';ch++)
{
if(extra==0)
{
break;
}
if(m[ch]==0 && extra>0)
{
m[ch]=1;
extra--;
t++;
}
}
// check if all the conditions satisfied
for(char cc='a';cc<='z';cc++)
{
if(m[cc]==0)
{
return false;
}
}
if(t<=k)
{
return true;
}
return false;
}
};