hashing · Problem 2 of 5

Valid Anagram

easy
Amazon logoAmazon
Google logoGoogle
Meta logoMeta
Microsoft logoMicrosoft
Uber logoUber

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, typically using all the original letters exactly once.

isAnagram("anagram", "nagaram") -> true
isAnagram("rat", "car")         -> false
isAnagram("a", "ab")            -> false

Your solution

Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.

Running is free — Submit is what records it. Or press ⌘↩

Tests

5 cases, 1 hidden
calltypeexpectedresult
isAnagram("anagram", "nagaram")valid anagramtrue
isAnagram("rat", "car")different charactersfalse
isAnagram("a", "ab")different lengthsfalse
isAnagram("listen", "silent")identical stringstrue
withheldhiddenwithheld

Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.

Complexity

target time
O(n)
target space
O(1) auxiliary space (bounded by character set size: 26 lowercase English letters)

Single pass counting through both strings with constant space table lookup.

Hints

Stuck? Hints open one at a time, each giving a little more away.

3 hints left