-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisUnique_no_chr_in_str_repeats.py
55 lines (41 loc) · 1.08 KB
/
isUnique_no_chr_in_str_repeats.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
# -*- coding: utf-8 -*-
"""
Is Unique: Implement an algorithm to determine if a string has all unique characters.
* Try implementing without additional data structure
@author: Akshita
"""
import time
from collections import defaultdict
def is_unique_1(A):
"""
@string: A
No extra data structure used
O(NlogN)
"""
B = sorted(A)
for i in range(1,len(B)):
if B[i-1] == B[i]:
return False
return True
def is_unique_2(A):
"""
@String: A
Use of extra data structure
O(N)
"""
B = defaultdict(int)
for char in A:
B[char] += 1
if B[char] > 1:
return False
return True
if __name__ == '__main__':
A = 'alskjotvmnew'
start = time.perf_counter()
print(is_unique_1(A))
end = time.perf_counter()
print('For O(NlogN) fuction, time taken is: {0:.6f}'.format(end-start))
start = time.perf_counter()
print(is_unique_1(A))
end = time.perf_counter()
print('For O(N) fuction, time taken is: {0:.6f}'.format(end-start))