Rally Health Interview Question

Write a function that determines if two strings are anagrams.

Interview Answers

Anonymous

May 15, 2019

//Big O n function isAnagram(s1, s2){ if(s1.length !== s2.length ){ return false; } const convertToMap = s => { const keyMap = Array(26).fill(0); for(const c of s){ const cAlphaCode = c.charCodeAt(0) - 97; ++keyMap[cAlphaCode]; } const key = keyMap.join('#'); return key; } const s1Key = convertToMap(s1); const s2Key = convertToMap(s2); return s1Key === s2Key; } // Big O nlogn function isAnagramV2(s1, s2){ return s1.split('').sort().join('') === s2.split('').sort().join('') } console.log(isAnagram('abc', 'cba')); console.log(isAnagramV2('abc', 'cba'));

Anonymous

May 15, 2019

change this line const cAlphaCode = c.charCodeAt(0) - 97; to const cAlphaCode = c.toLowerCase().charCodeAt(0) - 97;