Single line python code beats 97.5% (53ms)

  • 0

    Beats 97.5%

    def isAnagram(self, s, t):
            return all([s.count(c)==t.count(c) for c in string.ascii_lowercase])

    Using defaultdict beats 97%

     def isAnagram(self, s, t):
            if len(s) != len(t):
                return False 
            count = collections.defaultdict(int)
            for c in s:
                count[c] += 1
            for c in t:
                count[c] -= 1
                if count[c] < 0:
                    return False
            return True

Log in to reply

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