The trick here is to name the state correctly, then let the implementation follow. For 1002. Find Common Characters, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are commonChars.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- 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/**
02Given an array A of strings made only from lowercase letters,
03return a list of all characters that show up in all strings within the list (including duplicates).
04For example, if a character occurs 3 times in all strings but not 4 times,
05you need to include that character three times in the final answer.
06
07You may return the answer in any order.
08**/
09
10//Runtime: 48 ms, faster than 6.57% of C++ online submissions for Find Common Characters.
11//Memory Usage: 16.7 MB, less than 100.00% of C++ online submissions for Find Common Characters.
12class Solution {
13public:
14 vector<string> commonChars(vector<string>& A) {
15 vector<map<char, int>> charCounts;
16
17 //build maps for strings in A
18 for(string s : A){
19 map<char, int> charCount;
20 for(char c : s){
21 if(charCount.find(c)==charCount.end()){
22 charCount[c] = 1;
23 }else{
24 charCount[c] += 1;
25 }
26 }
27 charCounts.push_back(charCount);
28 }
29
30 map<char, int> charCount0 = charCounts[0];
31 for(int i = 1; i < charCounts.size(); i++){
32 //compare charCount0 and charCounts[i]
33 // cout << i << "th" << endl;
34 for(map<char, int>::iterator it=charCount0.begin(); it!=charCount0.end(); it++){
35 char c = it->first;
36 if(charCounts[i].find(c)==charCounts[i].end()){
37 charCount0[c] = 0;
38 }else{
39 charCount0[c] = min(charCount0[c], charCounts[i][c]);
40 }
41 // cout << c << ": " << charCount0[c] << ", ";
42 }
43 // cout << endl;
44 }
45
46 // for(const auto& c : charCount0){
47 // cout << c.first << ", " << c.second << endl;
48 // }
49 // cout << endl;
50
51 vector<string> ans;
52 for(map<char, int>::iterator it=charCount0.begin(); it!=charCount0.end(); it++){
53 while(it->second>0){
54 ans.push_back(string(1,it->first));
55 it->second-=1;
56 }
57 }
58
59 return ans;
60 }
61};
Cost