forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram_problem.py
53 lines (35 loc) · 1.34 KB
/
anagram_problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Problem Statement : To find out if two given string is anagram strings or not.
What is anagram? The string is anagram if it is formed by changing the positions of the characters.
Problem Link:- https://en.wikipedia.org/wiki/Anagram
Intution: Sort the characters in both given strings.
After sorting, if two strings are similar, they are an anagram of each other.
Return : A string which tells if the given two strings are Anagram or not.
"""
def checkAnagram(str1, str2):
#Checking if lenght of the strings are same
if len(str1) == len(str2):
#Sorting both the strings
sorted_str1 = sorted(str1)
sorted_str2 = sorted(str2)
#Checking if both sorted strings are same or not
if sorted_str1 == sorted_str2:
return "The two given strings are Anagram."
else:
return "The two given strings are not Anagram."
def main():
#User input for both the strings
str1 = input("Enter 1st string: ")
str2 = input("Enter 2nd srring: ")
#function call for checking if strings are anagram
print(checkAnagram(str1, str2))
main()
"""
Sample Input / Output:
Enter 1st string: lofty
Enter 2nd srring: folty
The two given strings are Anagram.
Enter 1st string: bhuj
Enter 2nd srring: ghuj
The two given strings are not Anagram.
"""