Code Lab

Timer: 10 min, like in an interview. Hints and autocomplete are off while it runs

Anagram check

MediumStrings10 min

Write a function isAnagram(a, b) that returns true when the string b is an anagram of a: it has exactly the same characters, the same number of times each, in any order. Otherwise return false.

  • Letter case does not matter: 'Listen' and 'Silent' are anagrams.
  • Spaces do not count: 'dormitory' and 'dirty room' are anagrams.
  • Every other character counts: digits, punctuation, letters such as é, and emoji.
  • The number of times matters: 'aab' and 'abb' are not anagrams, even though they use the same letters.
  • Two empty strings are anagrams.
  • A string can be long, up to 300 000 characters. Your code must finish in time.

Examples

isAnagram('Listen', 'Silent')

Returns true

isAnagram('dormitory', 'dirty room')

Returns true

isAnagram('aab', 'abb')

Returns false

Source

Loading the editor...
  1. InputisAnagram('Listen', 'Silent')
    Expectedtrue
  2. InputisAnagram('dormitory', 'dirty room')
    Expectedtrue
  3. InputisAnagram('aab', 'abb')
    Expectedfalse

Hidden tests run on Submit