-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path242. Valid Anagram.py
44 lines (39 loc) · 1.04 KB
/
242. Valid Anagram.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
ns = len(s)
nt = len(t)
if ns != nt:
return False
s_map = {}
for i in range(ns):
if not s_map.get(s[i]):
s_map[s[i]] = 1
else:
s_map[s[i]] += 1
for j in range(nt):
if s_map.get(t[j]) is None:
return False
else:
if s_map[t[j]] > 0:
s_map[t[j]] -= 1
else:
return False
return True
if __name__ == '__main__':
# Input: s = "anagram", t = "nagaram"
# Output: true
# Input: s = "rat", t = "car"
# Output: false
# s1, t1 = "anagram", "nagaram"
# s2, t2 = "rat", "car"
# print Solution().isAnagram(s1, t1)
# print Solution().isAnagram(s2, t2)
s3, t3 = "aacc", "ccac"
print Solution().isAnagram(s3, t3)