Takeaways:
- Automatic pass/fail cases
- If the lengths aren’t equal, they can’t be anagrams
- If the strings are identical, they must be anagrams
- Progressively optimized solution
- Could parse each character of each string and verify that they are all the same, but this is slow and inefficient
- Could use the sort method O(n log n), but hash map is faster O(n)
Simple Solution:
- This solution requires a binary sort of the characters in
sandt - Consequently, this solution isn’t the most efficient with a time complexity of
O(n log(n) + m log(m))
def isAnagram(self, s: str, t: str) -> bool:
if sorted(s) == sorted(t):
return True
else:
return False
Optimal Solution:
It’s possible to solve this problem in O(n) time
Algorithm:
- Flag for early exits with checks for length equality and string congruency
- Map the frequency of each character in
sto its corresponding alphabetical value (Hashmap lookups are anO(1)operation) - Parse the characters in
t-
If there’s a character in t that isn’t in the s map, return
False -
Subtract the encountered characters from the s map
-
If the number of mapped characters < 0, return
False
-
- Return
True
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
if s == t:
return True
s_map = {}
for c in s:
if c in s_map:
s_map[c] += 1
else:
s_map[c] = 1
for c in t:
if c in s_map:
s_map[c] -= 1
if s_map[c] < 0:
return False
else:
return False
return True