-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathp1_2.py
More file actions
37 lines (25 loc) · 718 Bytes
/
p1_2.py
File metadata and controls
37 lines (25 loc) · 718 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
from collections import Counter
# Method: Sort and compare
# Time: O(n*log(n))
# Space: O(n)
def check_perm(a: str, b: str):
sa = sorted(a)
sb = sorted(b)
return sb == sa
def check_perm_ctr(a: str, b: str):
ca = Counter(a)
cb = Counter(b)
for k, v in ca.items():
if k not in cb:
return False
elif v != cb[k]:
return False
if any(k not in ca for k in cb):
return False
return True
if __name__ == "__main__":
inputs = [("aa", "ac"), ("", ""), ("saodk", "dkoas"), ("cab", "baac")]
for a, b in inputs:
print(
f"Bot input {a} and {b}, result {check_perm(a,b)}"
f" == {check_perm_ctr(a,b)}")