First Non Repeating Character in a Stream of Characters

Find the first non-repeating character in a stream of characters.

Given a string A denoting a stream of lowercase alphabets. We have to write a code to make a new string B.

Here is the rule to form string B.

i) We have to find the first non-repeating character each time a character is inserted into the stream and append it at the end to B.

ii) If no non-repeating character is found then append ‘#’ at the end of B.

For example – 

Example 1 –

Input   = “abadbc”

Output  = “aabbdd”

Explanation:

“a”      –   From the stream, the first character is a, So at this point, the first non-repeating character is ‘a’.

“ab”     –   When b comes, Still the first non-repeating character is ‘a’.

“aba”    –   This time the character is a. Character a is repeated twice so the first non repeating character is ‘b’

“abad”   –   When character d comes, the first non-repeating character is ‘b’.

“abadb”  –   When character b comes, its count is 2 now. So the first non-repeating character at this point is ‘d’.

“abadbc” –   Next character is c. At this point, the first non-repeating character is ‘d’.