An efficient C++ solution 3ms

  • 0

    To solve this problem, simply assign each word to its bucket by making the first letter to a, then subsequent chars are converts to new lowercase char with the same difference as the first char.

    class Solution {
         vector<vector<string> > groupStrings(vector<string>& strings) {
            vector<vector<string> > result;
            map<string, vector<string> > m;
            for (auto s : strings){
                string sp = s;
                int d = sp[0]- 'a';
                for(int i = 0; i< sp.size(); ++i){
                    sp[i] = sp[i]-d;
                    if ('a' > sp[i]){ // if the normalized string is a char before 'a', then plus 26 to make it in range a-z
            for(auto k: m){
            return result;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.