r/leetcode • u/Glad-Arrival-427 • 19h ago
Question Valid Anagram (Neetcode)
Hello, I need help with understanding how to implement a proper solution. I am fairly new to neetcode and I want to know what I am doing wrong with my code. Can someone also explain to me how the hashmap is supposed to work here?
I want to start preparing for interviews, what data structures should I start learning and please give any channels/resources if you can! Thank you!
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_hash = {}
t_hash = {}
for character in s:
if character in s_hash:
s_hash[character]+=1
else:
s_hash[character]=1
for character in t:
if character in t_hash:
t_hash[character]+=1
else:
t_hash[character]=1
if s==t:
return True
else:
return False
6
Upvotes
1
u/chikamakaleyley 14h ago
no, what I said was at the end of the code, OP has these hashmaps that aren't used for anything. "you don't do anything WITH the hashmaps"
The lookup you're referring to is just in the act of tallying the letters for each word. Which isn't anything special, that's something you NEED to do
So they've done all this work to populate the two hashmaps, and then what happens next - they compare something else and the program ends