Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<string>> groupStrings(vector<string>& strings) {
- unordered_map<string, vector<string> > mp;
- // Have strings pushed back to distance hash.
- for (string s : strings)
- mp[shift(s)].push_back(s);
- // Just return the group of vector of strings.
- vector<vector<string> > groups;
- for(auto m : mp)
- groups.push_back(m.second);
- return groups;
- }
- private:
- string shift(string& s) {
- // get the difference string.
- string t;
- int n = s.length();
- for(int i = 1; i < n; i++) {
- int diff = s[i] - s[i - 1];
- if(diff < 0) diff += 26;
- t += 'a' + diff;
- }
- return t;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement