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:
- 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 characters so we will print -1.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
void firstnonrepeating( char str[])
{
queue< char > q;
int charCount[MAX_CHAR] = { 0 };
for ( int i = 0; str[i]; i++) {
q.push(str[i]);
charCount[str[i] - 'a' ]++;
while (!q.empty()) {
if (charCount[q.front() - 'a' ] > 1)
q.pop();
else {
cout << q.front() << " " ;
break ;
}
}
if (q.empty())
cout << -1 << " " ;
}
cout << endl;
}
int main()
{
char str[] = "aabc" ;
firstnonrepeating(str);
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
public class NonReapatingCQueue {
final static int MAX_CHAR = 26 ;
static void firstNonRepeating(String str)
{
int [] charCount = new int [MAX_CHAR];
Queue<Character> q = new LinkedList<Character>();
for ( int i = 0 ; i < str.length(); i++) {
char ch = str.charAt(i);
q.add(ch);
charCount[ch - 'a' ]++;
while (!q.isEmpty()) {
if (charCount[q.peek() - 'a' ] > 1 )
q.remove();
else {
System.out.print(q.peek() + " " );
break ;
}
}
if (q.isEmpty())
System.out.print(- 1 + " " );
}
System.out.println();
}
public static void main(String[] args)
{
String str = "aabc" ;
firstNonRepeating(str);
}
}
|
Python3
from queue import Queue
def firstnonrepeating( Str ):
global MAX_CHAR
q = Queue()
charCount = [ 0 ] * MAX_CHAR
for i in range ( len ( Str )):
q.put( Str [i])
charCount[ ord ( Str [i]) -
ord ( 'a' )] + = 1
while ( not q.empty()):
if (charCount[ ord (q.queue[ 0 ]) -
ord ( 'a' )] > 1 ):
q.get()
else :
print (q.queue[ 0 ], end = " " )
break
if (q.empty()):
print ( - 1 , end = " " )
print ()
MAX_CHAR = 26
Str = "aabc"
firstnonrepeating( Str )
|
C#
using System;
using System.Collections.Generic;
public class NonReapatingCQueue
{
public const int MAX_CHAR = 26;
public static void firstNonRepeating( string str)
{
int [] charCount = new int [MAX_CHAR];
LinkedList< char > q = new LinkedList< char >();
for ( int i = 0; i < str.Length; i++)
{
char ch = str[i];
q.AddLast(ch);
charCount[ch - 'a' ]++;
while (q.Count > 0)
{
if (charCount[q.First.Value - 'a' ] > 1)
{
q.RemoveFirst();
}
else
{
Console.Write(q.First.Value + " " );
break ;
}
}
if (q.Count == 0)
{
Console.Write(-1 + " " );
}
}
Console.WriteLine();
}
public static void Main( string [] args)
{
string str = "aabc" ;
firstNonRepeating(str);
}
}
|
Javascript
<script>
const MAX_CHAR = 26;
function firstNonRepeating(str) {
var charCount = new Array(MAX_CHAR).fill(0);
var q = [];
for ( var i = 0; i < str.length; i++) {
var ch = str[i];
q.push(ch);
charCount[ch.charCodeAt(0) - "a" .charCodeAt(0)]++;
while (q.length > 0) {
if (charCount[q[0].charCodeAt(0) -
"a" .charCodeAt(0)] > 1) {
q.shift();
} else {
document.write(q[0] + " " );
break ;
}
}
if (q.length == 0) {
document.write(-1 + " " );
}
}
document.write( "<br>" );
}
var str = "aabc" ;
firstNonRepeating(str);
</script>
|
Time complexity : O(n)
Auxiliary Space : O(n)
This article is contributed by Niteesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.