by CodeChum Admin
Two strings are called anagrams, if they contain the same characters, but the order of the characters may be different.
Given a string consisting of lowercase letters and question marks, s1, and another string consisting of lowercase letters, s2, determine whether these two strings could become anagrams by replacing each ? character in s1 with a letter.
Input
1. First string
Constraints
Contains only lowercase letters and/or question marks
2. Second string
Constraints
Contains only lowercase letters
Output
The first line will contain a message prompt to input the first string.
The second line will contain a message prompt to input the second string.
The last line contains the message "anagram" if the strings could become anagrams by replacing all the ? characters of s1 with letters and "false" otherwise.
def areAnagram(str1, str2):
n1 = len(str1)
n2 = len(str2)
if n1 != n2:
return 0
str1 = sorted(str1)
str2 = sorted(str2)
for i in range(0, n1):
if str1[i] != str2[i]:
return 0
return 1
str1 = input(print("Enter a string 1 : "))
str2 = input(print("Enter string 2 : "))
if areAnagram(str1, str2):
print("The two strings are anagram of each other")
else:
print("The two strings are not anagram of each other")
Comments
Leave a comment