An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode.
How to check given pait of word anagram
function IsAnagram(string11,string22)
{
//remove white spaces & make both word in same case
string1 = string11.replace(/ /g,'').toUpperCase();
string2 = string22.replace(/ /g,'').toUpperCase();
if(string1.length != string2.length)
{
return false;
}else{
let str1 = [...string1];
let str2 = [...string2];
str1 = str1.sort();
str2 = str2.sort()
string1 = str1.join('');
string2 = str2.join('');
if(string1 == string2)
{
return true;
}else{
return false;
}
}
}
console.log('"STOP" & "POST" combination is anagram :' + IsAnagram('STOP','POST'));
console.log('"FIRE" & "RIDE" combination is anagram :' + IsAnagram('FIRE','RIDE'));
console.log('"New York Times" & "monkeys write" combination is anagram :' + IsAnagram('New York Times','monkeys write'));
Output:
"STOP" & "POST" combination is anagram :true
"FIRE" & "RIDE" combination is anagram :false
"New York Times" & "monkeys write" combination is anagram :true
Search This Blog
2021/01/28
How to check pair of word are anagram in Javascript
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment