Why can't use set()


  • 0
    A

    In this problem, when my code is this:

    class Solution:
    # @param strs, a list of strings
    # @return a list of strings
    def anagrams(self, strs):
        dic = {}
        s = []
        for ele in strs:
            setS = str(sorted(set(ele)))
            if dic.has_key(setS):
                dic[setS].append(ele)
            else:
                dic[setS] = [ele]
    
        for x in dic.itervalues():
            if len(x) >= 2:
                s.extend(x)
                
        return s
    

    the answer is : wrong answer

    but if I changed the line:setS = str(sorted(set(ele))) to setS = str(sorted(ele))
    It has been accepted. I don't understand why.


  • 0
    A

    You lose the character counts when using a set, so you'll have false-positives.


Log in to reply
 

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