I was asked to implement the same question as this in an interview recently:
https://stackoverflow.com/questions/8447222/anagram-of-a-palindrome
I could not provide an answer but am interested to know the JavaScript solution.
I was asked to implement the same question as this in an interview recently:
https://stackoverflow.com/questions/8447222/anagram-of-a-palindrome
I could not provide an answer but am interested to know the JavaScript solution.
那应该可以工作..但是我只用很少的输入对其进行了测试:)至少它背后的理论应该没问题..
String.prototype.count=function(char) {
return this.split(char).length-1;
}
function isAnagramOfPalyndrom(string){
string.replace(" ", "");
var even = string.length % 2 == 0;
var flag = false;
for(var i = 0; i < string.length; i++){
if(string.count(string.charAt(i)) % 2 != 0){
if(even) return false;
else{
if(flag) return false;
flag = true;
}
}
}
return true;
}
从理论上讲,如果除 1 之外的每个字母都有偶数,则它是回文的字谜。
参见:“皮划艇”k:2, a:2, y:1 参见:“SAAS” s:2, a:2