C++ Using baseline string as signature

  • 0

    My solution is not far from others. I shift every string to its "baseline" and use it as the key of the group. The "baseline" is the string started with "a". For example, "bcd"->"abc", "xyz"->"abc"

    class Solution {
        vector<vector<string>> groupStrings(vector<string>& strings) {
            unordered_map<string, vector<string>> memo;
            for (int i=0; i<strings.size(); i++)
            vector<vector<string>> res;
            for (auto m:memo) res.push_back(m.second);
            return res;
        string signature(string s){
            int shift = s[0]-'a';
            for (int i=0; i<s.length(); i++){
                int tmp = s[i]-shift;
                if (tmp<'a')  s[i] = tmp+26;
                else s[i] = tmp;
            return s;

Log in to reply

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