Python Challenge of the day: Anagrams

Hey folks! Challenge of the day: write a Python program that checks if two given strings are anagrams. Let’s put our coding skills to the test and share our solutions. I can’t wait to see your creative solutions!"

def are_anagrams(str1, str2):
  
    str1 = str1.replace(" ", "").lower()
    str2 = str2.replace(" ", "").lower()
    return sorted(str1) == sorted(str2)

string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")

if are_anagrams(string1, string2):
    print("The two strings are anagrams.")
else:
    print("The two strings are not anagrams.")


 # An anagram is a word or phrase formed by rearranging the letters of another
1 Like
def Valid_anagrams(s,t):
    if len(s) != len(t):
        return False

    countS, countT = {}, {}

    for i in range(len(s)):
        countS[s[i]] = 1 + countS.get(s[i], 0)
        countT[t[i]] = 1 + countT.get(t[i], 0)
    return countS == countT
print(Valid_anagrams("anagrams","nagaram"))

242 LeetCode Sum

1 Like
str1  = str(input("Enter first string: "))
str2  = str(input("Enter second string: "))
print("First string: ",str1)
print("Second string: ",str2)
if sorted(str1) == sorted(str2):
    print("Strings are anagrams")
else:
    print("Strings are not anagrams")
1 Like

Well done guys, keep practising.

def is_anagram(str1, str2):
    return sorted(str1.lower()) == sorted(str2.lower())

str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")

result = is_anagram(str1, str2)

if result:
    print("The entered strings are anagrams.")
else:
    print("The entered strings are not anagrams.")