-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-2.py
More file actions
48 lines (33 loc) · 793 Bytes
/
Problem-2.py
File metadata and controls
48 lines (33 loc) · 793 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
38
39
40
41
42
43
44
45
46
47
48
'Problem -2 First Non Repeating character'
s = 'aaabcccdeef'
n = len(s)
'Solution-1'
def fnrc(s,n):
for i in range(0,n):
seenDuplicate = False
for j in range(0,n):
if(s[i] == s[j] and (i!=j)):
seenDuplicate = True
break;
if(not seenDuplicate):
return s[i]
return '-'
sol = fnrc(s,n)
print(sol)
'Solution-2 using hashmap'
def fnrc2(s,n):
c_counts = dict()
for i in range(0,n):
c = s[i]
if(c in c_counts):
c_counts.put(c,c_counts.get(c)+1)
else:
c_counts.put(c,1)
'For lookup'
for i in range(0,n):
c = s[i]
if(c_counts.get(c) == 1):
return (c)
return '-'
sol_2 = fnrc(s,n)
print(sol_2)