Algorithm and Data Structure 학습02-Frequency Problem

5628 단어 algorithmtech

문제.


Write a function that takes two strings and check if they have the same letters.Order doesn't matter.
Ex:
sameFrequency("abbc","aabc") false
sameFrequency("abba","abab") true
sameFrequency("aasdebasdf","adfeebed") false

코드


function sameFrequency(str1,str2){
  let arr1 = str1.split("");
  let arr2 = str2.split("");
  
  if(arr1.length !== arr2.length){
    return false
  }
  
  let counter1 = {}
  let counter2 = {}
  for(let i = 0;i < arr1.length;i++){
    if(counter1[arr1[i]]){
      counter1[arr1[i]]++;
    }else{
      counter1[arr1[i]] = 1;
    }	  
  }
  for(let property in counter1){
    if(!counter2[property]){
	return false; 
    }
    if(counter2[property] !== counter1[property]){
	return false;
    }
  }
  return ture;
}
console.log(sameFrequency("aabb","abab"))

좋은 웹페이지 즐겨찾기