Valid Anagram
Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
Example 1:
Input: s = "racecar", t = "carrace"
Output: true Example 2:
Input: s = "jar", t = "jam"
Output: false
In [17]:
def isAnagram(s, t):
if len(s)!=len(t):
return False
s_dict={}
t_dict={}
for i in range(len(s)):
if s[i] in s_dict:
s_dict[s[i]]+=1
else:
s_dict[s[i]]=1
if t[i] in t_dict:
t_dict[t[i]]+=1
else:
t_dict[t[i]]=1
if t_dict==s_dict:
return True
else:
return False
In [18]:
isAnagram('carrae','racecar')
Out[18]:
False
In [19]:
isAnagram('carrace','racecar')
Out[19]:
True
In [20]:
a={5:1,4:1}
b={4:1,5:1}
if 5 in a:
print('match')
match
In [21]:
a={5:1,4:1}
b={4:1,5:1}
if a==b:
print('match')
match
In [ ]: