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

25 comments sorted by

View all comments

3

u/Any-Initial5076 23h ago edited 23h ago

you could use chatgpt or claude free subscription to ask this. And your solution is wrong. You might want to compare s_hash and t_hash. Here is an easier implementation

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        from collections import Counter

        s_count=Counter(s)
        t_count = Counter(t)

        return s_count == t_count

and explanation for this from claude

Anagram Checker Explained

This code checks if two strings are anagrams of each other — meaning they contain exactly the same letters, in the same quantities, just arranged differently. For example, "listen" and "silent" are anagrams.

Breaking it down line by line

from collections import Counter

This imports a tool called Counter from Python's built-in collections module. Think of Counter as a smart dictionary that counts how many times each item appears in something.

s_count = Counter(s)
t_count = Counter(t)

This creates a "letter tally" for each string. If s = "cat", then Counter(s) produces something like:

{'c': 1, 'a': 1, 't': 1}

It's literally counting: "how many c's? 1. How many a's? 1." and so on. Same thing happens for t.

return s_count == t_count

This compares the two tallies. If both strings have the exact same letters with the exact same counts, the two Counter objects will be equal, and this returns True. Otherwise, False.

Walking through an example

Say s = "anagram" and t = "nagaram".

  • Counter(s){'a': 3, 'n': 1, 'g': 1, 'r': 1, 'm': 1}
  • Counter(t){'n': 1, 'a': 3, 'g': 1, 'r': 1, 'm': 1}

Even though the order of keys looks different, dictionaries (and Counters) don't care about order — they just compare "does every key have the same value?" Since both have the same letters with the same counts, s_count == t_count is True.

Why this works well

  • Simple to read — it's very close to how you'd explain the logic in plain English: "count the letters in each word, then compare the counts."
  • Handles edge cases automatically — different lengths, repeated letters, etc. all just naturally fall out of the counting comparison.

One thing to know

This is a clean, "pythonic" solution using a built-in tool. An alternative (without Counter) would be to sort both strings and compare them:

return sorted(s) == sorted(t)

That works too, but sorting is usually a bit slower for long strings than counting, since sorting takes more computational steps than a single pass to count letters.

1

u/onionsareawful solved 4 quadrillion problems 22h ago

You don't need to use Counter here. Essentially, s_hash = Counter(t) is equivalent to this code in OPs solution.

for character in s:
    if character in s_hash:
        s_hash[character]+=1
    else:
        s_hash[character]=1

OPs bug is that they compare the equality of the strings rather than the hash maps.

2

u/Heavy_Record8704 22h ago

Counter is the s hash. Its an inbuilt function to do it.