How I solved Leetcode 242. Valid Anagram in JavaScript

preview_player
Показать описание
Hey! My name is Tomas. Thanks for checking out my channel.

Step 1: Check lengths of both strings
Step 2: Create a map to keep track of letters
Step 3: Add letter amounts to map for first string
Step 4: Subtract letter amounts from map for second string
Answer: If there is more of an amount for a certain letter in the second string, return false. If it gets through both loops, return true.

Happy Coding!
Рекомендации по теме
Комментарии
Автор

At the end, you could return `newS === newT`, since that evaluates to true/false!

tannercomes
Автор

Would it also work to just sort the strings and compare them?

def is_anagram(s1, anagram_of_s1):
if len(s1) != len(anagram_of_s1):
return False
else:
ss1 = sorted(s1)
ss2 = sorted(anagram_of_s1)
if ss1 != ss2:
return False
return True

print(is_anagram(s1, anagram_of_s1))

FerrumCorde
welcome to shbcf.ru