🏷️ Category: Strings / HashMap  |  🎯 Difficulty: Easy  |  πŸ”₯ Frequency: β˜…β˜…β˜…β˜…β˜…


πŸ“‹ Problem Statement

Given two strings, return True if they are anagrams of each other (same characters, same frequency, different order). This process is often referred to as an anagram check.

πŸ“₯ Input  β†’ "listen", "silent"    πŸ“€ Output β†’ True
πŸ“₯ Input  β†’ "triangle", "integral" πŸ“€ Output β†’ True
πŸ“₯ Input  β†’ "hello", "world"      πŸ“€ Output β†’ False

🧠 What the Interviewer is Testing

ConceptWhy It Matters
Counter comparisonPython library knowledge
Sorting approachAlternative thinking
Edge casesDifferent lengths, empty strings

πŸ’‘ Approach

Two strings are anagrams if:
β†’ Same length AND
β†’ Same character frequencies

Methods:
  βœ… Counter comparison β€” O(n)
  βœ… Sorted comparison  β€” O(n log n)

βœ… Optimal Solution β€” Counter

from collections import Counter

def is_anagram(s1: str, s2: str) -> bool:
    return Counter(s1.lower()) == Counter(s2.lower())

# ── Test Cases ─────────────────────────────────
print(is_anagram("listen", "silent"))      # βœ… True
print(is_anagram("triangle", "integral"))  # βœ… True
print(is_anagram("hello", "world"))        # βœ… False
print(is_anagram("", ""))                  # βœ… True

πŸ”„ Sorting Approach

def is_anagram_sort(s1: str, s2: str) -> bool:
    return sorted(s1.lower()) == sorted(s2.lower())

πŸ“Š Complexity Analysis

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Approach         β”‚ Time       β”‚ Space                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Counter βœ…       β”‚ O(n)       β”‚ O(k) k=unique chars      β”‚
β”‚ Sorted           β”‚ O(n log n) β”‚ O(n)                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎀 How to Explain in the Interview

β€œTwo strings are anagrams if they have the same characters with the same frequencies. Counter creates a character frequency map for each string β€” if they’re equal, it’s an anagram. This is O(n). Alternatively, sorting both strings and comparing is O(n log n) but simpler to reason about.”

β€œTwo strings are anagrams if they have the same characters with the same frequencies. Counter creates a character frequency map for each string β€” if they’re equal, it’s an anagram. This is O(n). Alternatively, sorting both strings and comparing is O(n log n) but simpler to reason about.”


πŸ” Common Follow-up Questions

Q1 β†’ Group all anagrams from a list?

from collections import defaultdict

def group_anagrams(words: list) -> list:
    groups = defaultdict(list)
    for word in words:
        key = tuple(sorted(word))
        groups[key].append(word)
    return list(groups.values())
# ["eat","tea","tan","ate","nat","bat"]
# β†’ [["eat","tea","ate"], ["tan","nat"], ["bat"]]

πŸ“ Set 1 of 4 β€” Python Interview Prep Series β€” devprepguide.com

Scroll to Top