

Given a string ‘STR’ consisting of lower case English letters, the task is to find the first non-repeating character in the string and return it. If it doesn’t exist, return ‘#’.
For example:
For the input string 'abcab', the first non-repeating character is ‘c’. As depicted the character ‘a’ repeats at index 3 and character ‘b’ repeats at index 4. Hence we return the character ‘c’ present at index 2.
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 ‘STR’ consisting of only lowercase English letters.
Output Format:
For each test case, print a single character representing the first non-repeating character in the given string.
The output for each test case is in a separate line.
Note:
You do not need to print anything; it has already been taken care of.
1 <= T <= 100
1 <= N <= 10 ^ 4
Where ‘T’ is the number of test cases, and ‘N’ is the length of the string.
Time Limit: 1 sec
2
bbabcbcb
babaabea
a
e
For the first test case,
the first non-repeating character is ‘a’. As depicted the character ‘b’ repeats at index 1, 3, 5, 7, and character ‘c’ repeats at index 6. Hence we return the character ‘a’ present at index 2.
For the second test case,
the character ‘e’ is the first non-repeating character. As depicted the character ‘b’ repeats at index 2, 5, and character ‘a’ repeats at index 3, 4, and 7. Hence we return the character ‘e’ present at index 6.
3
cbbd
bebeeed
abcd
c
d
a
Find the frequency of all characters in the string and check which character has a unit frequency.
A character is said to be non-repeating if its frequency in the string is equal to 1. 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 an 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 number of distinct characters in the ASCII system is 256. So the 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:
O(N), where N is the length of the given string.
Since, we are traversing the string to store and count the frequencies of the characters, which takes O(N) time. Thus, the overall time complexity is O(N).
O(1).
Since no extra memory is used except a count array of constant size 256.