Given a stream of characters and we have to find first non repeating character each time a character is inserted to the stream.
Input : a a b c Output : a -1 b b Input : a a c Output : a -1 c
We have already discussed a Doubly linked list based approach in the previous post.
- Create a count array of size 26(assuming only lower case characters are present) and initialize it with zero.
- Create a queue of char datatype.
- Store each character in queue and increase its frequency in the hash array.
- For every character of stream, we check front of the queue.
- If the frequency of character at the front of queue is one, then that will be the first non repeating character.
- Else if frequency is more than 1, then we pop that element.
- If queue became empty that means there are no non repeating character so we will print -1.
Below is the implementation of above approach:
a -1 b b
This article is contributed by Niteesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find the first non-repeating character from a stream of characters
- Count occurrence of a given character in a string using Stream API in Java
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- Find a string such that every character is lexicographically greater than its immediate next character
- Longest Common Prefix using Character by Character Matching
- Count substrings that starts with character X and ends with character Y
- Replace every character of string by character whose ASCII value is K times more than it
- Map every character of one string to another such that all occurrences are mapped to the same character
- Shortest distance to every other character from given character
- Replace every character of a string by a different character
- DFA based division
- FIFO (First-In-First-Out) approach in Programming
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- A Programmer's approach of looking at Array vs. Linked List