VOOZH about

URL: https://www.geeksforgeeks.org/dsa/queue-based-approach-for-first-non-repeating-character-in-a-stream/

⇱ Queue based approach for first non-repeating character in a stream - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Queue based approach for first non-repeating character in a stream

Last Updated : 10 Jan, 2023

Given a stream of characters and we have to find first non repeating character each time a character is inserted to the stream. 

Examples: 

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

Approach:

  1. Create a count array of size 26(assuming only lower case characters are present) and initialize it with zero.
  2. Create a queue of char datatype.
  3. Store each character in queue and increase its frequency in the hash array.
  4. For every character of stream, we check front of the queue.
  5. If the frequency of character at the front of queue is one, then that will be the first non-repeating character.
  6. Else if frequency is more than 1, then we pop that element.
  7. If queue became empty that means there are no non-repeating characters so we will print -1.


Below is the implementation of above approach:


Output: 
a -1 b b

 

Time complexity : O(n) 
Auxiliary Space : O(n)

Comment