-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1160.cpp
More file actions
25 lines (23 loc) · 690 Bytes
/
1160.cpp
File metadata and controls
25 lines (23 loc) · 690 Bytes
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
class Solution {
public:
int countCharacters(vector<string>& words, string chars) { //O(NM + L), where N is number of words, M is max word length, and L is length of chars
char have[26] = {};
for(char c : chars){
have[c-'a']++;
}
int total = 0;
for(string w : words){
char need[26] = {};
bool works = true;
for(char c : w){
need[c-'a']++;
if(need[c-'a'] > have[c-'a']){
works = false;
break;
}
}
if(works) total += w.length();
}
return total;
}
};