Python solution with detailed explanation


  • 0
    G

    Solution

    Valid Anagram

    Algorithm

    • Simple question. Build a frequency map for s. Now check t against this frequency map.
    • Read the editorial about the followup about unicode characters.
    • Unicode has 4 bytes per character. So 2^32 or 4 billion characters
    • Using an array so big is not good. Use a hash-table.
    from collections import Counter  
    class Solution(object):
        def isAnagram(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            return Counter(s) == Counter(t)
    

Log in to reply
 

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