-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path242.py
More file actions
39 lines (33 loc) · 947 Bytes
/
242.py
File metadata and controls
39 lines (33 loc) · 947 Bytes
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
'''
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
'''
import collections
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
#return collections.Counter(s) == collections.Counter(t)
dic1, dic2 = {}, {}
for item in s:
dic1[item] = dic1.get(item, 0) + 1
for item in t:
dic2[item] = dic2.get(item, 0) + 1
return dic1 == dic2
if __name__ == "__main__":
ss = "anagram"
ts = "nagaram"
s = "rat"
t = "car"
sol = Solution()
print(sol.isAnagram(s, t))