-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_non_repeating_character.py
58 lines (45 loc) · 1.94 KB
/
first_non_repeating_character.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
56
57
58
"""
Given an input stream A of n characters consisting only of lower case alphabets. While reading characters from the stream, you have to tell which character has appeared only once in the stream upto that point. If there are many characters that have appeared only once, you have to tell which one of them was the first one to appear. If there is no such character then append '#' to the answer.
NOTE:
1. You need to find the answer for every i (0 <= i < n)
2. In order to find the solution for every i you need to consider the string from starting position till ith position.
Example 1:
Input: A = "aabc"
Output: "a#bb"
Explanation: For every ith character we will
consider the string from index 0 till index i first non
repeating character is as follow-
"a" - first non-repeating character is 'a'
"aa" - no non-repeating character so '#'
"aab" - first non-repeating character is 'b'
"aabc" - there are two non repeating characters 'b' and 'c',
first non-repeating character is 'b' because 'b' comes before
'c' in the stream.
Example 2:
Input: A = "zz"
Output: "z#"
Explanation: For every character first non
repeating character is as follow-
"z" - first non-repeating character is 'z'
"zz" - no non-repeating character so '#'
Your Task:
You don't need to read or print anything. Your task is to complete the function FirstNonRepeating() which takes A as input parameter and returns a string after processing the input stream.
Expected Time Complexity: O(n)
Expected Space Complexity: O(n)
"""
class Solution:
def FirstNonRepeating(self, A):
# Code here
list = []
mp = {}
ans = ''
for ch in A:
if ch not in mp: # new character visited first time
list.append(ch)
mp[ch] = 1
else:
# any repeated character encountered
if ch in list:
list.remove(ch)
ans = ans + (list[0] if list else '#')
return ans