-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathp1_1.py
More file actions
37 lines (26 loc) · 712 Bytes
/
p1_1.py
File metadata and controls
37 lines (26 loc) · 712 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: Character map
# Time: O(n)
# Space: O(n)
# Follow-up
# Method: Sort and compare
# Time: O(n*log(n))
# Space: O(n)
def all_uniqs(input_str: str):
ctr = Counter(input_str)
for k, v in ctr.items():
if v > 1:
return False
return True
def all_uniqs_no_datastruct(x: str):
sorted_x = sorted(x)
n = len(x)
for i in range(1, n):
if sorted_x[i-1] == sorted_x[i]:
return False
return True
if __name__ == "__main__":
inputs = ["", "aaasodkc", "abbccdcss", "aaa", "b", "bdes"]
for x in inputs:
print(
f"Input {x} result: {all_uniqs(x)} same as {all_uniqs_no_datastruct(x)}")