First Unique Character in a String

You are given a string A consisting of lower case English letters. You have to find the first non-repeating character from each stream of characters.

For Example: If the given string is 'bbaca', then the operations are done as:

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’. 
Input Format:
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.
Output Format:
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.
Note:
You do not need to print anything; it has already been taken care of.
Constraints:
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.
CodingNinjas
author
2y

1. Make hash_map which will map the character to there frequencies.
2. Traverse the string.
3. Increase the count of current character.
4. Traverse the string again and check whether the current characte...read more

CodingNinjas
author
2y
Count array and two string traversals

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 s...read more

CodingNinjas
author
2y
Count array and single string traversal

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-repea...read more

CodingNinjas
author
2y
Optimized Approach

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...read more

Add answer anonymously...
Amdocs Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter