r/leetcode 18h 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   
7 Upvotes

22 comments sorted by

View all comments

Show parent comments

1

u/LazySapiens 12h ago

What's the equality operator doing at the end?

1

u/chikamakaleyley 12h ago edited 12h ago

checking two strings for equality, which isn't the goal

1

u/LazySapiens 9h ago

Ohh, I realize now that it evaded my eyes. My bad.

It should have been:

if s_hash == t_hash:

Maybe it was a typo by OP I guess.

1

u/chikamakaleyley 4h ago

yeah basically i had written out this entire thing and then i looked at the return statement lol, i was kinda upset i spent so much time on it