4ms C++ solution


  • 6
    J
    vector<vector<string>> groupStrings(vector<string>& strings) {
    	unordered_map<string, vector<string>> hmap;
    
    	for (int i = 0; i<strings.size(); i++) {
    		string t = strings[i];
    		int diff = t[0] - 'a';
    		for (int j = 0; j<strings[i].size(); j++) {
    			if (t[j] - diff - 'a' >= 0)
    				t[j] = t[j] - diff;
    			else 
    				t[j] = (t[j] - diff) + 26;
    		}
    
    		hmap[t].push_back(strings[i]);
    	}
    
    	vector<vector<string>> result;
    	for (auto iter = hmap.begin(); iter != hmap.end(); ++iter) {
    		result.push_back(iter->second);
    		sort(result.back().begin(), result.back().end());
    	}
    
    	return result;
    }

  • 0
    S

    nice solution


Log in to reply
 

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