First non-repeating character in a stream

PROBLEM :
Given an input stream of n characters consisting only of small case alphabets the task is to find the first non repeating character each time a character is inserted to the stream.

Example

Flow in stream : a, a, b, c
a goes to stream : 1st non repeating element a (a)
a goes to stream : no non repeating element -1 (5, 15)
b goes to stream : 1st non repeating element is b (a, a, b)
c goes to stream : 1st non repeating element is b (a, a, b, c)

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the stream. Then in the next line are x characters which are inserted to the stream.

Output:
For each test case in a new line print the first non repeating elements separated by spaces present in the stream at every instinct when a character is added to the stream, if no such element is present print -1.

Constraints:
1<=T<=200
1<=N<=500

Example:
Input:
2
4
a a b c
3
a a c

Output:
a -1 b b
a -1 c

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;
#define CHAR 256

typedef struct NODE
{
    char data ;
    struct NODE *prev ;
    struct NODE *next ;
}node ;

void appendNode(node **head,node **tail,char ch)
{
    node *temp ;
    temp=new node() ;
   
    temp->data=ch ;
    temp->prev=NULL ;
    temp->next=NULL ;
   
    if((*head)==NULL)
    {
        (*head)=temp ;
        (*tail)=temp ;
        return ;
    }
   
    (*tail)->next=temp ;
    temp->prev=(*tail) ;
    (*tail)=temp ;
}

void removeNode(node **head,node **tail,node *temp)
{
    if(!(*head))
        return ;
       
    if((*head)==temp)
        (*head)=(*head)->next ;
       
    if((*tail)==temp)
        (*tail)=(*tail)->prev ;
       
    if(temp->next!=NULL)
        temp->next->prev=temp->prev ;
       
    if(temp->prev!=NULL)
        temp->prev->next=temp->next ;
       
    delete(temp) ;
}

int main()
{
int t ;
cin>>t ;
while(t--)
{
   int no ;
   cin>>no ;
 
   node *dll[CHAR] ;
   bool repeat[CHAR] ;
 
   for(int i=0;i<CHAR;i++)
   {
       dll[i]=NULL ;
       repeat[i]=false ;
   }
 
   node *head,*tail ;
   head=NULL ;
   tail=NULL ;
 
   while(no--)
   {
       char ch ;
       cin>>ch ;
     
       if(!repeat[ch])
       {
           if(dll[ch]==NULL)
           {
               appendNode(&head,&tail,ch) ;
               dll[ch]=tail ;
           }
           else
           {
               removeNode(&head,&tail,dll[ch]) ;
               dll[ch]=NULL ;
               repeat[ch]=true ;
           }
       }
     
       if(head)
           cout<<head->data<<" " ;
       else
           cout<<-1<<" " ;
   }
 
   cout<<endl ;
}
return 0;
}

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (another solution) : Queue
--------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;
#define CHAR 256

int main()
{
int t ;
cin>>t ;
while(t--)
{
   int no ;
   cin>>no ;
 
   queue<char> que ;
   int charcount[CHAR]={0} ;
 
   while(no--)
   {
       char ch ;
       cin>>ch ;
     
       que.push(ch) ;
       charcount[ch]++ ;
     
       while(!que.empty())
       {
           if(charcount[que.front()]>1)
               que.pop() ;
           else
           {
               cout<<que.front()<<" " ;
               break ;
           }
       }
     
       if(que.empty())
           cout<<-1<<" " ;
   }
 
   cout<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )