beats 97.99%, 49ms c++ solution


  • 1
    M
    class Solution {
    public:
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            int primes[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};
            map<int, vector<string>> m;
            for(int i = 0; i < strs.size(); ++i) {
                int key = 1;
                for(int j = 0; j < strs[i].size(); ++j) {
                    key *= primes[strs[i][j] - 'a'];
                }
                m[key].push_back(strs[i]);
            }
            vector<vector<string>> result;
            for(auto& v : m) {
                result.push_back(v.second);
            }
            return result;
        }
    };
    

  • 0
    Y

    @mingmingyang

    if characters is too many to be contained in an int, how your code deal with it?


Log in to reply
 

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