-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path242.linningmii.py
40 lines (32 loc) · 1003 Bytes
/
242.linningmii.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
class Solution:
def getItemByIndex(self, targetList, index):
if len(targetList) <= index:
return None
return targetList[index]
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
s_dict = {}
t_dict = {}
if len(s) >= len(t):
iterator = len(s)
else:
iterator = len(t)
for i in range(iterator):
if s_dict.get(self.getItemByIndex(s, i)):
s_dict[self.getItemByIndex(s, i)] += 1
else:
s_dict[self.getItemByIndex(s, i)] = 1
if t_dict.get(self.getItemByIndex(t, i)):
t_dict[self.getItemByIndex(t, i)] += 1
else:
t_dict[self.getItemByIndex(t, i)] = 1
for key in s_dict.keys():
if s_dict.get(key) != t_dict.get(key):
return False
return True
s = Solution()
print(s.isAnagram("ab", "a"))