

The first stream is “b” and the first non-repeating character is ‘b’ itself, so print ‘b’.
The next stream is “bb” and there are no non-repeating characters, so print nothing.
The next stream is “bba” and the first non-repeating character is ‘a’, so print ‘a’.
The next stream is “bbac” and the first non-repeating character is ‘a’, so print ‘a’.
The next stream is “bbaca” and the first non-repeating character is ‘c’, so print ‘c’.
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.
The only line of each test case contains a single string A consisting of only lowercase English letters.
For each test case, print a single character representing the first non-repeating character for each stream of characters.
The output for each test case is in a separate line.
You do not need to print anything; it has already been taken care of.
1 <= T <= 100
1 <= N <= 10000
Where ‘T’ is the number of test cases, and ‘N’ is the length of the string.
Time Limit: 1 sec.
A character is said to be non-repeating if its frequency in the string is unit. To find such characters, one needs to find the frequency of all characters in the string and check which character has a unit frequency. This task could be done efficiently using a count array to map the character to their respective frequencies. We can simultaneously update the frequency of any character we come across in constant time. The maximum distinct characters in the ASCII system are 256. So the count array has a maximum size of 256. Now read the string again, and the first character we find has a frequency as unity is the answer.
Below is the algorithm:
Make a count array to store the index of the character if it appears only once; else, it stores a negative value. So when it comes to finding the first non-repeating character, we have to scan the count array instead of the string.
Below is the algorithm:
We can use a Doubly Linked List to get the first non-repeating character from a stream efficiently. This can be done by storing all non-repeating characters in DLL. We will keep them in order, i.e., the head node of DLL contains the first non-repeating character, and the next node contains the next non-repeating character, and so on. We will also maintain two ArrayLists: the first one maintains characters that are already visited two or more times, the second one is stores pointers to linked list nodes.
Following is the algorithm to implement the above logic: