xxxxxxxxxx
if sorted(s1) == sorted(s2):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
xxxxxxxxxx
def anagram_checker(first_value, second_value):
if sorted(first_string) == sorted(second_string):
print("The two strings are anagrams of each other.")
out = True
else:
print("The two strings are not anagrams of each other.")
out = False
return out
first_string = input("Provide the first string: ")
second_string = input("Provide the second string: ")
anagram_checker(first_string, second_string)
Print(anagram_checker("python", "typhon"))
True
xxxxxxxxxx
// 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)
xxxxxxxxxx
s1=rare
s2=care
if sorted(s1) == sorted(s2):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
xxxxxxxxxx
private static final int EXTENDED_ASCII_CODES = 256;
public static boolean isAnagram(String str1, String str2) {
int[] chCounts = new int[EXTENDED_ASCII_CODES];
char[] chStr1 = str1.replaceAll("\\s",
"").toLowerCase().toCharArray();
char[] chStr2 = str2.replaceAll("\\s",
"").toLowerCase().toCharArray();
if (chStr1.length != chStr2.length) {
return false;
}
for (int i = 0; i < chStr1.length; i++) {
chCounts[chStr1[i]]++;
chCounts[chStr2[i]]--;
}
for (int i = 0; i < chCounts.length; i++) {
if (chCounts[i] != 0) {
return false;
}
}
return true;
}
xxxxxxxxxx
// anagrams('Heart!', 'EARTH') --> True
// anagrams('lol', 'lolc') --> False
function anagrams(stringA, stringB) {
stringA =stringA.toLowerCase().replace(/[\W_]+/g,"")
stringB =stringB.toLowerCase().replace(/[\W_]+/g,"")
const charObj = {};
for(let i=0 ;i<stringA.length;i++){
charObj[stringA[i]]= charObj[stringA[i]]+1 || 1;
}
for (let i = 0; i < stringB.length; i++) {
if(!charObj[stringB[i]]){
return false
}else {
charObj[stringA[i]]=charObj[stringA[i]]--
}
}
return true
}
xxxxxxxxxx
function anagrams(stringA, stringB) {
return stringA.toLowerCase().replace(/[\W_]+/g,"").split("").sort().join("")
=== stringB.toLowerCase().replace(/[\W_]+/g,"").split("").sort().join("")
}