#Taking inputsStr_a = input()Str_b = input()#If lengths of both string doesn't equal then they can't be anagramif len(Str_a) != len(Str_b) :return 0#Initilising empty dictionary for Str_ad_a = {}# Loop through every character in Str_a and count no. of times each character appearedfor ch in Str_a:if ch in d_a:d_a[ch] = d_a[ch]+1else :d_a[ch] = 1#Initilising empty dictionary for Str_bd_b = {}# Loop through every character in Str_bfor ch_b in Str_b:if ch_b in d_b:d_b[ch_b] = d_b[ch_b]+1else :d_b[ch_b] = 1#Comparing both dictionariesif(d_a == d_b):return "YES,it is an anagram"else :return "NO"
def areAnagram(str1, str2):# Get lengths of both stringsn1 = len(str1)n2 = len(str2)# If length of both strings is not the same, then# they cannot be an anagramif n1 != n2:return 0# Sort both stringsstr1 = sorted(str1)str2 = sorted(str2)# Compare sorted stringsfor i in range(0, n1):if str1[i] != str2[i]:return 0return 1# Driver codestr1 = "cppsecrets"str2 = "srectespcp"# Function Callif areAnagram(str1, str2):print("The two strings are an anagram of each other")else:print("The two strings are not an anagram of each other")
Comments