My 1 line Python solution using 60ms


  • 1
    C
    class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        return set(s) == set(t) and map(s.count,sorted(list(set(s)))) == map(t.count,sorted(list(set(s))))

  • 0
    J

    it's pretty cool


  • 0
    J
    return sorted(list(s)) == sorted(list(t))
    

    used 100 ms...


  • 0
    D

    sorted() is of O(n*logn)
    I don't understand why it runs so fast.
    Faster than my O(n) solution.

    class Solution(object):
    def isAnagram(self, s, t):
    maps = {}
    mapt = {}
    for c in s:
    maps[c] = maps.get(c,0)+1
    for c in t:
    mapt[c] = mapt.get(c,0)+1
    return maps == mapt


Log in to reply
 

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