fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. class Solution {
  12. public:
  13. vector<vector<string>> groupAnagrams(vector<string>& strs)
  14. {
  15. vector<vector<string>> result;
  16. map<string,vector<string>> myMap;
  17.  
  18. if(strs.size() == 0)
  19. {
  20. return result;
  21. }
  22.  
  23. for(const string& s : strs)
  24. {
  25. string temp = s;
  26. sort(temp.begin(),temp.end());
  27. std::vector<string>& dest = myMap[temp];
  28. dest.emplace_back(s);
  29. }
  30.  
  31. cout<< myMap["abt"].size() <<endl;
  32.  
  33. for (auto& kv : myMap) // kv: key value
  34. {
  35. result.emplace_back(std::move(kv.second));
  36. }
  37.  
  38. return result;
  39. }
  40. };
  41.  
  42.  
  43. int main(int argc, const char * argv[])
  44. {
  45. Solution mySolution;
  46. vector<string> myStrings {"eat", "tea", "tan", "ate", "nat", "bat"};
  47. auto result = mySolution.groupAnagrams(myStrings);
  48.  
  49. for(vector<string> v: result)
  50. {
  51. //cout << v.size() << endl;
  52. for(string s: v)
  53. {
  54. cout << s << " ";
  55. }
  56. cout << endl;
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
1
bat 
eat tea ate 
tan nat