Logo
Overview
LeetCode #242 - Valid Anagram

LeetCode #242 - Valid Anagram

Jane Doe Jane Doe
August 1, 2025 3 min read
index

πŸ“ Introduction

Given two strings, determine whether they are anagrams of each other.
Two strings are anagrams if they contain the same characters with the same frequencies, regardless of the order.

Constraints:

  • Only uppercase letters (e.g., β€˜A’ to β€˜Z’) are considered.
  • Strings must be the same length to qualify as anagrams.
  • Output true if they are anagrams, otherwise false.

πŸ’‘ Approach & Key Insights

  • The core idea is to check whether the two strings contain the exact same characters with the same counts.
  • If their lengths differ, they can’t be anagrams.
  • There are multiple valid ways to compare character distributions:
    • Sort and compare
    • Count frequencies using a fixed-size array (best for character sets of known size)

πŸ› οΈ Breakdown of Approaches

1️⃣ Brute Force / Naive Approach β€” Sorting

  • Explanation:
    Sort both strings and check if they are equal. If sorted versions are identical, then the strings are anagrams.

  • Time Complexity: O(N log N) – Due to sorting of both strings.

  • Space Complexity: O(1) – Assuming in-place sorting.

  • Example/Dry Run:

Input: str1 = "INTEGER", str2 = "TEGERNI"
Step 1: Sort str1 β†’ "EEGINRT"
Step 2: Sort str2 β†’ "EEGINRT"
Step 3: Compare both β†’ Equal β†’ return true
Output: true

2️⃣ Optimized Approach β€” Frequency Count

  • Explanation:
    Use a frequency array of size 26 (for uppercase letters).

    • Increment count for characters in str1
    • Decrement for characters in str2
    • If all frequencies are 0 in the end β†’ anagrams
  • Time Complexity: O(N) – One pass for each string.

  • Space Complexity: O(1) – Fixed array size of 26.

  • Example/Dry Run:

Input: str1 = "INTEGER", str2 = "TEGERNI"
Step 1: freq[] = [0,...,0]
Step 2: For str1 β†’
I+1, N+1, T+1, E+1, G+1, E+1, R+1
β†’ freq = {G:1, E:2, I:1, N:1, R:1, T:1}
Step 3: For str2 β†’
T-1, E-1, G-1, E-1, R-1, N-1, I-1
β†’ freq = all zero
Step 4: All frequencies are zero β†’ return true
Output: true

3️⃣ Best / Final Optimized Approach

  • Explanation:
    Frequency counting is optimal for limited character sets (like uppercase/lowercase letters).

    • No sorting
    • No additional data structures like maps or sets
    • Linear time and constant space
  • Time Complexity: O(N)

  • Space Complexity: O(1)


πŸ“Š Complexity Analysis

ApproachTime ComplexitySpace Complexity
Brute ForceO(N log N)O(1)
OptimizedO(N)O(1)
Best ApproachO(N)O(1)

πŸ“‰ Optimization Ideas

  • Convert both strings to the same case (e.g., uppercase) to make comparison case-insensitive if needed.
  • If dealing with Unicode characters, use a HashMap instead of fixed-size array.
  • Early termination: While building the frequency array, if any count goes negative, return false early.

πŸ“Œ Example Walkthroughs & Dry Runs

Example 1:
Input: str1 = "CAT", str2 = "ACT"
Step 1: freq[26] = {0}
Step 2: +1 for C, A, T β†’ freq[C]=1, A=1, T=1
Step 3: -1 for A, C, T β†’ all become 0
Output: true
Example 2:
Input: str1 = "RULES", str2 = "LESRT"
Step 1: freq[26] = {0}
Step 2: +1 for R, U, L, E, S β†’ freq[R]=1, U=1, L=1, E=1, S=1
Step 3: -1 for L, E, S, R, T β†’ freq[T]=-1 (unexpected char)
Output: false

πŸ”— Additional Resources


Author: Abdul Wahab
Date: 19/07/2025

Related Posts