isAnagram

Medium · String Manipulations · 3 test cases

Given two strings, check if they are anagrams of each other. 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. Ignore spaces, punctuation, and capitalization.

Examples

isAnagram('rail safety', 'fairy tales') → true
isAnagram('RAIL! SAFETY!', 'fairy tales') → true
isAnagram('Hi there', 'Bye there') → false

Starter code

function isAnagram(strA, strB) {

}

Complexity of the optimal solution

Time O(n log n), space O(n). The dominant operation is sorting the characters of the strings. If a string has n characters, sorting takes O(n log n) time. The cleaning and splitting operations take O(n) time and space.

Solve isAnagram in the browser