Skip to main content

Command Palette

Search for a command to run...

LeetCode 242: Valid Anagram

Easy Steps to Mastering LeetCode's Anagram Problem

Published
3 min read
LeetCode 242: Valid Anagram
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: January 14, 2026
Category: Array | HashMap
Time Taken: 15 minutes
Difficulty: Easy


Problem Statement

Given two strings, s and t, return true if t is an anagram of s, and false otherwise.

Link: Valid Anagram


My Approach

Initial thought:

  • I could compare the sorted strings and return true if they are the same or false if they are not. This approach would have a time complexity of O(N log N) and a space complexity of O(N).

  • We can also use collections.Counter on both strings and compare them. This method will have a time complexity of O(N) and a space complexity of O(N).

  • I could also create a hashmap to map the characters of a string to their frequencies and then compare it to the next string. This approach will have a time complexity of O(N) and a space complexity of O(1) [since there can only be 26 possible characters.]

Final solution:

  • First, we will compare the length of both the strings; if they are not the same, we will return false.

  • Initialize a hashmap, then use a for loop on one string to map the frequencies of each character in the hashmap.

  • Use another for loop to go through each character in the other string. If the frequency for that character is 0, we return false. If it's not, we reduce the value by 1. At the end of the loop, if it does not return false, then it is an anagram, so we return true.

Why this works:
The follow-up question for this problem is: What if the inputs contain Unicode characters? How would you adapt your solution to such a case? The hashmap solution is the best way to handle all types of characters it might have.


Solution Code (Counter)

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if len(s) != len(t):
            return False
        return Counter(s) == Counter(t)

Solution Code (HashMap)

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if len(s) != len(t):
            return False
        hashmap = {}
        for char in s:  
            hashmap[char] = hashmap.get(char, 0) + 1

        for char in t:
            if hashmap.get(char, 0) == 0:
                return False
            else:
                hashmap[char] -= 1
        return True

Complexity: Time: O(N) Space: O(N)

Key Takeaway: I can use collections.Counter to count frequencies instead of manually doing it with a hashmap each time. I can also avoid KeyError by using hashmap.get().

Pattern: Frequency Counter Pattern.

Mistakes I Made: I didn't think to compare the lengths of both strings beforehand, which caused my solution to fail even though all the other logic was correct.

Series: 90 Days of Data Engineering Progress: 4/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic