3-line Python Solution Using Counter


  • 0
    _

    We can use the Counter data structure to more easily create & compare our hash tables.

    import collections
    
    class Solution(object):
        def isAnagram(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """  
            c1 = collections.Counter(list(s))
            c2 = collections.Counter(list(t))
          
            return len(c1) == len(c2) and not c1 - c2
    

Log in to reply
 

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