Skip to content

Latest commit

 

History

History

242-ValidAnagram

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Valid Anagram

Problem can be found in here!

Solution: Hash Table

def isAnagram(s: str, t: str) -> bool:
    memo = {}
    for token in s:
        try:
            memo[token] += 1
        except KeyError:
            memo[token] = 1

    for token in t:
        try:
            memo[token] -= 1
        except KeyError:
            return False

    return all(count == 0 for count in memo.values())

Time Complexity: O(n), Space Complexity: O(1)