- Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathfirst_non_repeating.py
54 lines (40 loc) · 1.12 KB
/
first_non_repeating.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
# Given an input string, it gives the
# first non repeating character in it
# There are two implementations below
# 1. has less space complexity
# 2. has less time complexity
deffirst_non_repeating(input_string):
frequency=dict()
flag=None
forcharininput_string:
ifcharinfrequency.keys():
frequency[char] +=1
else:
frequency[char] =0
forcharininput_string:
iffrequency[char] ==0:
flag=char
break
returnflag
# lesser time complexity
# more space complexity
# obvious space-time trade-off
deffirst_non_repeating_v2(input_string):
flag=None
repeating= []
non_repeating= []
forcharininput_string:
ifcharinnon_repeating:
non_repeating.remove(char)
repeating.append(char)
else:
non_repeating.append(char)
iflen(non_repeating) ==0:
pass
else:
flag=non_repeating[0]
returnflag
result=first_non_repeating("djebdedbekfrnkfnduwbdwkd")
print(result) # j
result=first_non_repeating("aabbcc")
print(result) # None