Share my Python solution that beats 99%


  • 2
    C
    class Solution(object):
        def isAnagram(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            if len(s) != len(t):
                return False
            holder = {}
            for ch in s:
                if ch in holder:
                    continue
                if s.count(ch) != t.count(ch):
                    return False
                else:
                    holder[ch] = 1
            return True

  • 1
    K

    @carlonelong maybe you could use set(list(s)) instead of constructing a dictionary to make it more simple :) like this:

        if len(s) != len(t):
            return False
        for i in set(list(s)):
            if s.count(i) != t.count(i):
                return False
        return True

Log in to reply
 

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