A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1079. Letter Tile Possibilities, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are genCombs, numTilePossibilities.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 120 ms, faster than 17.63% of C++ online submissions for Letter Tile Possibilities.
02//Memory Usage: 31.3 MB, less than 100.00% of C++ online submissions for Letter Tile Possibilities.
03
04class Solution {
05public:
06 set<string> lastCombs, curCombs, allCombs;
07 map<char, int> charCount;
08
09 void genCombs(string& tiles, int N){
10 curCombs.clear();
11 if(N == 1){
12 for(map<char, int>::iterator it = charCount.begin(); it!=charCount.end(); it++){
13 //string(1, c): convert char 'c' to string
14 curCombs.insert(string(1, it->first));
15 allCombs.insert(string(1, it->first));
16 }
17 }else{
18 //construct curCombs from lastCombs by appending one char after them
19 for(string lastComb : lastCombs){
20 //choose the char to append
21 for(map<char, int>::iterator it = charCount.begin(); it!=charCount.end(); it++){
22 char c = it->first;
23 //check if we still have that char
24 if(count(lastComb.begin(), lastComb.end(), c) < charCount[c]){
25 string comb = lastComb + c;
26 curCombs.insert(comb);
27 allCombs.insert(comb);
28 }
29 }
30 }
31 }
32 lastCombs = curCombs;
33 }
34
35 int numTilePossibilities(string tiles) {
36 for(char c : tiles){
37 if(charCount.find(c) == charCount.end()){
38 charCount[c] = 1;
39 }else{
40 charCount[c]++;
41 }
42 }
43
44 for(int i = 1; i <= tiles.size(); i++){
45 genCombs(tiles, i);
46 // cout << i << endl;
47 // for(string s : lastCombs){
48 // cout << s << " ";
49 // }
50 // cout << endl;
51 }
52
53 return allCombs.size();
54 }
55};
Cost