forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
First non-repeating character in a stream.cpp
63 lines (54 loc) · 1.7 KB
/
First non-repeating character in a stream.cpp
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
59
60
61
62
63
/*
First non-repeating character in a stream
==========================================
Given an input stream of A of n characters consisting only of lower case alphabets. The task is to find the first non repeating character, each time a character is inserted to the stream. If there is no such character then append '#' to the answer.
Example 1:
Input: A = "aabc"
Output: "a#bb"
Explanation: For every character 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" - first non-repeating character is 'b'
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(26 * n)
Expected Space Complexity: O(26)
Constraints:
1 <= n <= 105
*/
string FirstNonRepeating(string A)
{
list<char> dll, temp;
vector<int> repeated(26, 0);
vector<list<char>::iterator> ptrs(26, temp.begin());
string ans;
for (auto &i : A)
{
if (!repeated[i - 'a'] && ptrs[i - 'a'] == temp.begin())
{
dll.push_back(i);
auto it = dll.end();
it--;
ptrs[i - 'a'] = it;
}
else if (!repeated[i - 'a'] && ptrs[i - 'a'] != temp.begin())
{
dll.erase(ptrs[i - 'a']);
repeated[i - 'a'] = 1;
}
if (dll.size())
ans.push_back(dll.front());
else
ans.push_back('#');
}
return ans;
}