42ms C++ solution beats 100%,use prime number


  • 0
    L

    class Solution {
    public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {

        vector<vector<string>> result;
        int s_hash[26]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
        int v_len = strs.size();
        unordered_map<int,int> val_result;
        for(int i=0; i<v_len; i++){
            int i_val=1;
            int i_len=strs[i].size();
            for(int j=0;j<i_len;++j){
                i_val *= s_hash[strs[i][j]-'a'];
            }
            int index = val_result[i_val];
            if(index == 0){
                result.push_back(vector<string>(1, strs[i]));
                val_result[i_val]=result.size();
            }else{
                result[index-1].push_back(strs[i]);
            }
        }
        return result;
    }
    

    };


  • 1
    J

    when one string's length in vector strs is larger than 32, variable i_val in your code will be overflow.


Log in to reply
 

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