Advertisement
nikunjsoni

249

Jun 22nd, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<vector<string>> groupStrings(vector<string>& strings) {
  4.         unordered_map<string, vector<string> > mp;
  5.         // Have strings pushed back to distance hash.
  6.         for (string  s : strings)
  7.             mp[shift(s)].push_back(s);
  8.        
  9.         // Just return the group of vector of strings.
  10.         vector<vector<string> > groups;
  11.         for(auto m : mp)
  12.             groups.push_back(m.second);
  13.         return groups;
  14.     }
  15. private:
  16.     string shift(string& s) {
  17.         // get the difference string.
  18.         string t;
  19.         int n = s.length();
  20.         for(int i = 1; i < n; i++) {
  21.             int diff = s[i] - s[i - 1];
  22.             if(diff < 0) diff += 26;
  23.             t += 'a' + diff;
  24.         }
  25.         return t;
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement