Any place to optimize?


  • 0
    S

    My python version answer use 332ms

       class Solution:
        # @param strs, a list of strings
        # @return a list of strings
        def anagrams(self, strs):
             res = []
             factors = [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,103,107,109]
             
             # first time occured
             genes = {}
             times = {}
     
             mul = 1 
     
             for str in strs:
                 num = 1 
                 for c in str: 
                     num =num *factors[ord(c)-ord('a') + 1 ]
                 if num in times:
                     times[num] = times[num]+ 1 
     
                     if times[num] == 2:
                         res.append(genes[num])
     
                     if times[num] >= 2:
                         res.append(str)
                 else:   
                     genes[num] = str 
                     times[num] = 1
     
             return res

Log in to reply
 

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