Python one line Solution


  • -1
    A
    class Solution(object):
    def isAnagram(self, s, t):
        return sorted(list(s)) == sorted(list(t))

  • 0
    D

    This is indeed short.

    However, looking from computation complexity aspect, sort() has a complexity of O(n log n).
    So there is space for improvement.
    A O(n) solution is as follows:

    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.