taesik

check if two strings are ANAGRAM (using frequencyCounter) 본문

functions and trick/typescript_javascript

check if two strings are ANAGRAM (using frequencyCounter)

taesikk 2022. 4. 29. 09:39

An anagram is a word, phrase, or name formed by rearranging the letters of another.

 

 

 

function validAnagram(first,second){
  // add whatever parameters you deem necessary - good luck!
  if(first.length !== second.length) return false;
  
  let lookup = {};
  
  for (let val of first.split('')) {
      lookup[val]? lookup[val] += 1 : lookup[val] = 1;
  }
  for(let val of second.split('')) {
  	//can't find letter or letter is zero then it's not an anagram
      if(!lookup[val]) return false;
      else {
      	lookup[val] -= 1; 
      }
  }
  return true;
}