-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path242_valid_anagram.py
More file actions
43 lines (40 loc) · 1.12 KB
/
Copy path242_valid_anagram.py
File metadata and controls
43 lines (40 loc) · 1.12 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
# Given two strings s and t, return true
# if t is an anagram of s, and false
# otherwise.
#
# Constraints:
# 1 <= s.length, t.length <= 5 * 104
# s and t consist of lowercase English
# letters.
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t):
return False
freqs = [0] * 26
for ch1, ch2 in zip(s,t):
freqs[ord(ch1) - ord('a')] += 1
freqs[ord(ch2) - ord('a')] -= 1
for i in range(len(freqs)):
if freqs[i] != 0:
return False
# 25ms beats 37%
return True
vectors = [
"ggii", "eekk", False,
"rat", "car", False,
"anagram", "margana", True,
"anagram", "gnaarma", True,
"anagram", "nagaram", True
]
for i in range(0, len(vectors), 3):
s = vectors[i]
t = vectors[i+1]
print(f'{s} {t}')
expected = vectors[i+2]
returned = Solution().isAnagram(s, t)
assert expected == returned, f'isAnagram(\'{s}\',\'{t}\') = {expected}, but returned {returned}!'