Python solution with list of size 26.

  • 0
        def isAnagram(self, s, t):
            :type s: str
            :type t: str
            :rtype: bool
            c_count = [0] * 26
            for c in s:
                c_count[ord(c) - ord('a')] += 1
            for c in t:
                c_count[ord(c) - ord('a')] -= 1
            return all([v == 0 for v in c_count])

    Equivalent to using a hash. 26 element list to count the frequency of each car in s, then subtract the count of each one in t. If the list is not completely 0's at the end, it means they didn't have the same chars.

Log in to reply

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