fork download
  1. class Solution {
  2.  
  3. public:
  4. vector<vector<string>> groupAnagrams(vector<string>& strs) {
  5. vector<vector<string> > ans;
  6. if (strs.empty()) {
  7. return ans;
  8. }
  9. unordered_map<int, vector<string>> hash;
  10. vector<int> primes{2, 3, 5, 7, 11, 13, 17, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 107};
  11. for (const string& str : strs) {
  12. int getHash = 1;
  13. for (int i = 0; i < (int)str.size(); i++) {
  14. getHash *= primes[str[i] - 'a'];
  15. }
  16. if (hash[getHash].size()) {
  17. hash[getHash].push_back(str);
  18. } else {
  19. hash[getHash] = vector<string>{str};
  20. }
  21. }
  22. for (const auto& hashList : hash) {
  23. if (hashList.second.size()) {
  24. ans.push_back(hashList.second);
  25. }
  26. }
  27. return ans;
  28. }
  29. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:5: error: ‘vector’ does not name a type
     vector<vector<string>> groupAnagrams(vector<string>& strs) {
     ^~~~~~
stdout
Standard output is empty