-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count the Number of Consistent Strings-LC-1684.cpp
51 lines (45 loc) · 1.25 KB
/
Count the Number of Consistent Strings-LC-1684.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
//using vector
class Solution {
public:
int countConsistentStrings(string allowed, vector<string>& words) {
int count=0,flag=1;
vector<bool> allowedChar(26,0);
for(int i = 0; i<allowed.size();i++)
allowedChar[allowed[i]-97]=true;
for(auto word: words){
flag=1;
for(int i=0;i<word.size();i++){
if(!allowedChar[word[i]-97]){
flag=0;
break;
}
}
if(flag)
count++;
}
return count;
}
};
//really slow and space consuming method
//a map and iterating through each word
class Solution {
public:
int countConsistentStrings(string allowed, vector<string>& words) {
int count=0,flag=1;
map<char,bool> allowedChar;
for(int i = 0; i<allowed.size();i++)
allowedChar[allowed[i]]=true;
for(auto word: words){
flag=1;
for(int i=0;i<word.size();i++){
if(!allowedChar[word[i]]){
flag=0;
break;
}
}
if(flag)
count++;
}
return count;
}
};