You are viewing a single comment's thread. Return to all comments →
Here is how I do it using hash map so that we dont have to deal with (n*m) checks.
a = a.toLowerCase().split('').sort(); b = b.toLowerCase().split('').sort(); var Maparray = function(testArr){ var object ={}; testArr.forEach(function(arrayItem){ if(arrayItem in object){ object[arrayItem] +=1; }else{ object[arrayItem] = 1; } }); return object; }; var aObject = Maparray(a); var bObject = Maparray(b); var matchCount = 0; for( var key in aObject){ if(key in bObject){ var matches = 0; if(aObject[key] === bObject[key]){ matches = 2 * aObject[key]; }else{ matches = (aObject[key] > bObject[key]) ? bObject[key]:aObject[key]; matches = matches * 2; } matchCount += matches; } } console.log(a.length + b.length - matchCount);
Seems like cookies are disabled on this browser, please enable them to open this website
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
Here is how I do it using hash map so that we dont have to deal with (n*m) checks.