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
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
is_anagram = lambda x1, x2: sorted(x1) == sorted(x2)
print(is_anagram('silent','listen')) # True
print(is_anagram('elivs', 'dead')) # False
xxxxxxxxxx
s='ccac' #output: false
t='aacc'
if set(s)==set(t) and len(s)==len(t):
d={}
e={}
for i in s:
if i not in d:
d[i]=1
else:
d[i]+=1
for i in t:
if i not in e:
e[i]=1
else:
e[i]+=1
if d != e:
print('false')
else:
print('true')
else:
print('false')
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)