def isAnagram(A,B):
if sorted(A) == sorted(B):
print("Yes")
else:
print("No")
isAnagram("earth","heart") #Output: Yes
#Hope this helps:)
var firstWord = "Mary";
var secondWord = "Army";
isAnagram(firstWord, secondWord); // true
function isAnagram(first, second) {
// For case insensitivity, change both words to lowercase.
var a = first.toLowerCase();
var b = second.toLowerCase();
// Sort the strings, and join the resulting array to a string. Compare the results
a = a.split("").sort().join("");
b = b.split("").sort().join("");
return a === b;
}
import java.util.Arrays;
public class AnagramString {
static void isAnagram(String str1, String str2) {
String s1 = str1.replaceAll("s", "");
String s2 = str2.replaceAll("s", "");
boolean status = true;
if (s1.length() != s2.length()) {
status = false;
} else {
char[] ArrayS1 = s1.toLowerCase().toCharArray();
char[] ArrayS2 = s2.toLowerCase().toCharArray();
Arrays.sort(ArrayS1);
Arrays.sort(ArrayS2);
status = Arrays.equals(ArrayS1, ArrayS2);
}
if (status) {
System.out.println(s1 + " and " + s2 + " are anagrams");
} else {
System.out.println(s1 + " and " + s2 + " are not anagrams");
}
}
public static void main(String[] args) {
isAnagram("Keep", "Peek");
isAnagram("Mother In Law", "Hitler Woman");
}
}
def anagram(a, b):
return True if sorted(a) == sorted(b) else False
# Based on the answer below :D
// for 2 strings s1 and s2 to be anagrams
// both conditions should be True
// 1 their length is same or
len(s1)==len(s2)
// 2 rearranging chars alphabetically make them equal or
sorted(s1)==sorted(s2)
const anagram = (str1, str2) => {
return str1.toLowerCase().split('').sort().join('') === str2.toLowerCase().split('').sort().join('');
};
let test = anagram('Regallager', 'Lagerregal');
console.log(test);
//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));