-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_4.py
More file actions
55 lines (44 loc) · 1.19 KB
/
question_4.py
File metadata and controls
55 lines (44 loc) · 1.19 KB
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
54
55
#!/usr/bin/env python
# @Time : 2018/5/3 下午11:25
# @Author : cancan
# @File : question_4.py
# @Function : 有效的字母异位词
"""
Question:
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
Example:
s = "anagram",t = "nagaram",返回 true
s = "rat",t = "car",返回 false
Note:
假定字符串只包含小写字母。
Follow up:
输入的字符串包含 unicode 字符怎么办?你能能否调整你的解法来适应这种情况?
"""
class Solution:
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if set(s) != set(t) or len(s) != len(t):
return False
for i, v in enumerate(s):
if v == t[i]:
continue
elif v not in t:
return False
else:
c_s = s.count(v)
c_t = t.count(v)
if c_s != c_t:
return False
else:
return True
else:
return True
if __name__ == "__main__":
a = Solution()
s = ""
t = ""
print(a.isAnagram(s, t))