First Non Repeating Character

PROBLEM :

Given a string s consisting of lowercase Latin Letters, find the first non repeating character in s.

Input:

The first line contains T denoting the number of testcases. Then follows description of testcases.
Each case begins with a single integer N denoting the length of string. The next line contains the string s.

Output:


 For each testcase, print the first non repeating character present in string.

 Print -1 if there is no non repeating character.

Constraints:


 1<=T<=30
 1<=N<=100


Example:


Input :

3
5
hello
12
zxvczbtxyzvy
6
xxyyzz



Output :

h
c
-1
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
char Non_Repeating(char *,int) ;
int main()
 {
int t,no ;
char str[100],ele ;
cin>>t ;
while(t--)
{
   cin>>no ;
   cin>>str ;
 
   ele=Non_Repeating(str,no) ;
   if(ele=='1')
       cout<<-1 ;
   else
       cout<<ele ;
     
   cout<<endl ;
}
return 0;
}

char Non_Repeating(char *str,int l)
{
    int i ;
   
    int hash[256]={0} ;
   
    for(i=0;i<l;i++)
        hash[str[i]]++ ;
       
    for(i=0;i<l;i++)
    {
        if(hash[str[i]]==1)
            return str[i] ;
    }
    return '1' ;
}

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

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 )