fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8. vector<string> commonChars(vector<string>& words) {
  9. int freq[26];
  10. for(int i = 0; i < 26; i++) freq[i] = 1000;
  11.  
  12. for(string word : words) {
  13. int temp[26] = {0};
  14. for(char c : word) temp[c - 'a']++;
  15. for(int i = 0; i < 26; i++)
  16. freq[i] = min(freq[i], temp[i]);
  17. }
  18.  
  19. vector<string> result;
  20. for(int i = 0; i < 26; i++) {
  21. for(int j = 0; j < freq[i]; j++)
  22. result.push_back(string(1, 'a' + i));
  23. }
  24.  
  25. return result;
  26. }
  27. };
  28.  
  29. int main() {
  30. Solution sol;
  31. vector<string> words = {"bella", "label", "roller"};
  32. vector<string> res = sol.commonChars(words);
  33.  
  34. for(string s : res) cout << s << " ";
  35. cout << endl;
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
e l l