需求: 一个字符串洗牌,随机打乱顺序,与之前比较
比如: 'welcome' == 'elmecow' -> true
'aab' == 'bab' -> false
<script>
function find(str){
var json={};
for(var i=0; i<str.length; i++){
if(json[str.charAt(i)]){
json[str.charAt(i)]++;
}else{
json[str.charAt(i)]=1;
}
}
return json;
}
function compare(obj,obj2){
for(var name in obj){
if(obj[name]!=obj2[name]){
return false;
}
}
for(var name in obj2){
if(obj[name]!=obj2[name]){
return false;
}
}
return true;
}
function isAnagram(string1,string2){
var json=find(string1);
var json2=find(string2);
return compare(json,json2);
}
console.log(isAnagram('something','emosignth'));
console.log(isAnagram('abb','aba'));
console.log(isAnagram('aaab','aaa'));
console.log(isAnagram('some','meos'));
</script>