from collections import Counter
def is_anagram(s1, s2):
return Counter(s1) == Counter(s2)
s1 = 'listen'
s2 = 'silent'
s3 = 'runner'
s4 = 'neuron'
print(''listen' is an anagram of 'silent' -> {}'.format(is_anagram(s1, s2)))
print(''runner' is an anagram of 'neuron' -> {}'.format(is_anagram(s3, s4)))
# Output
# 'listen' an anagram of 'silent' -> True
# 'runner' an anagram of 'neuron' -> False
function isAnagram(stringA, stringB) {
// Sanitizing
stringA = stringA.toLowerCase().replace(/[W_]+/g, "");
stringB = stringB.toLowerCase().replace(/[W_]+/g, "");
// sorting
const stringASorted = stringA.split("").sort().join("");
const stringBSorted = stringB.split("").sort().join("");
return stringASorted === stringBSorted;
}
//find the anagram if two word has the same characters for example word 1: listen, word 2: silent, if the characters are the same return true otherwise return false
//str1="eat" str2="ate"
//str1="bar " str2="bare"
//str1="bba" str2="aab"
function areAnagram(str1,str2)
{
// Get lengths of both strings
let n1 = str1.length;
let n2 = str2.length;
// If length of both strings is not same,
// then they cannot be anagram
if (n1 != n2)
return false;
// Sort both strings
let x =str1.split("").sort();
let y =str2.split("").sort()
console.log(x ,y);
// Compare sorted strings
for (let i = 0; i < n1; i++){
if (x[i] != y[i])
return false;
}
return true;
}
let str1="netsil";
let str2="listen";
console.log( areAnagram(str1,str2));