-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1079. Letter Tile Possibilities.cpp
55 lines (50 loc) · 1.88 KB
/
1079. Letter Tile Possibilities.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
//Runtime: 120 ms, faster than 17.63% of C++ online submissions for Letter Tile Possibilities.
//Memory Usage: 31.3 MB, less than 100.00% of C++ online submissions for Letter Tile Possibilities.
class Solution {
public:
set<string> lastCombs, curCombs, allCombs;
map<char, int> charCount;
void genCombs(string& tiles, int N){
curCombs.clear();
if(N == 1){
for(map<char, int>::iterator it = charCount.begin(); it!=charCount.end(); it++){
//string(1, c): convert char 'c' to string
curCombs.insert(string(1, it->first));
allCombs.insert(string(1, it->first));
}
}else{
//construct curCombs from lastCombs by appending one char after them
for(string lastComb : lastCombs){
//choose the char to append
for(map<char, int>::iterator it = charCount.begin(); it!=charCount.end(); it++){
char c = it->first;
//check if we still have that char
if(count(lastComb.begin(), lastComb.end(), c) < charCount[c]){
string comb = lastComb + c;
curCombs.insert(comb);
allCombs.insert(comb);
}
}
}
}
lastCombs = curCombs;
}
int numTilePossibilities(string tiles) {
for(char c : tiles){
if(charCount.find(c) == charCount.end()){
charCount[c] = 1;
}else{
charCount[c]++;
}
}
for(int i = 1; i <= tiles.size(); i++){
genCombs(tiles, i);
// cout << i << endl;
// for(string s : lastCombs){
// cout << s << " ";
// }
// cout << endl;
}
return allCombs.size();
}
};