🎃
Algorithm and Data Structure勉強 02-Frequency Problem
問題
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"))
Discussion