-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC242-Valid-Anagram.py
68 lines (55 loc) · 1.68 KB
/
LC242-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: 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?
"""
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
"""
Check if two words are anagrams
Time complexity: O(n + m), where len(s) = n and len(t) = m
Space complexity: O(n + m)
"""
return sorted(s) == sorted(t)
def is_anagram(self, s: str, t: str) -> bool:
"""
Check if strings s and t are anagrams
Time complexity: O(n + m)
Space complexity: O(n + m)
"""
if len(s) != len(t):
return False
counter = dict()
for char in s:
counter[char] = counter.get(char, 0) + 1
for char in t:
if counter.get(char, 0) < 1:
return False
counter[char] -= 1
return True
if __name__ == '__main__':
from run_tests import run_tests
correct_answers = [
['abc', 'cab', True],
['abcd', '', False],
['abc', 'abd', False],
['qweryt', 'ytrewq', True],
['12;', ';12', True],
['', '', True],
['avaa', 'aava', True],
['avaa', 'avva', False],
['.,;', ';.,', True],
['.,;', '..,', False]
]
methods = ['isAnagram', 'is_anagram']
for method in methods:
print(f'Running tests for {method}')
run_tests(getattr(Solution(), method), correct_answers)