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
| class Solution { public: vector<string> commonChars(vector<string>& words) {
int* hash = new int[26]();
for(int i = 0;i < words[0].size(); i++) { hash[ words[0][i] - 'a']++; }
int* hashOrt = new int[26]();
for(int i = 1; i < words.size(); i++) { memset(hashOrt, 0, 26 * sizeof(int)); for(int j = 0; j < words[i].size(); j++ ) { hashOrt[ words[i][j] - 'a']++; } for(int j = 0; j < 26; j++) { hash[j] = min(hash[j],hashOrt[j]); } }
vector<string> result;
for(int i = 0; i < 26; i++) { while(hash[i] > 0) { string s(1,i + 'a'); result.push_back(s); hash[i]--; } }
delete[] hash; delete[] hashOrt;
return result; } };
|