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
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
xxxxxxxxxx
if sorted(s1) == sorted(s2):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
xxxxxxxxxx
def are_anagrams(first, second):
return len(first) == len(second) and sorted(first) == sorted(second)
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
// 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.")