Coders Crushby Napplied AI
Back to DSA Problems
EasyLeetCode #242Arrays & Hashing

Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase.

Constraints
1 <= s.length, t.length <= 5 * 10^4, s and t consist of lowercase English letters
Coders Crushby Napplied AI

The ultimate interview preparation platform. Master System Design, DSA, and tackle community challenges to crush your FAANG interviews.

System Design

  • All Problems
  • Easy
  • Hard

DSA

  • All Problems
  • Dynamic Programming
  • Graphs

More

  • Problems Arena
  • Growth Paths
  • AI Discovery

Coders Crush by Napplied AI - Built for engineers preparing for FAANG/MAANG interviews

Examples
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
Solution

Approach

Count character frequencies in both strings and compare. Use a hash map or array of size 26 for lowercase letters.

def isAnagram(s, t):
    if len(s) != len(t):
        return False
    return Counter(s) == Counter(t)
Complexity
Time:O(n)
Space:O(1)
Hints
  • 1.What makes two strings anagrams?
  • 2.Count character frequencies
  • 3.Can you do it with a single pass?
Asked at
GoogleAmazonMetaMicrosoft