easy for understand using c++


  • 0
    B

    class Solution {
    public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
    vector<vector<string> > result;
    vector<string> str_sort;
    int length = strs.size();
    for(int i=0; i<length; i++)
    {
    bool is_exist = false;
    string tmp_current(strs[i]);
    sort(tmp_current.begin(), tmp_current.end());
    int res_length = result.size();
    for(int j=0; j<res_length; j++)
    {
    string &tmp = str_sort[j];
    if(tmp==tmp_current)
    {
    result[j].push_back(strs[i]);
    is_exist = true;
    break;
    }
    }
    if(!is_exist)
    {
    vector<string> tmp;
    tmp.push_back(strs[i]);
    result.push_back(tmp);
    str_sort.push_back(tmp_current);
    }
    }
    return result;
    }
    };


Log in to reply
 

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