Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def isAnagram(self, s: str, t: str) -> bool:
- if len(s) != len(t):
- return False
- # create a dictionary to keep track of the frequency of each character in s
- freq = {}
- for char in s:
- if char in freq:
- freq[char] += 1
- else:
- freq[char] = 1
- # iterate through t and decrement the frequency of each character
- # if a character is not in freq, or its frequency becomes negative, return False
- for char in t:
- if char not in freq or freq[char] == 0:
- return False
- freq[char] -= 1
- return True
Advertisement
Add Comment
Please, Sign In to add comment