problem no 58:(problem on string):Non Repeating Character .

 QUESTION:

Given a string S consisting of lowercase Latin Letters. Find the first non-repeating character in S.

Example 1:

Input:
S = hello
Output: h
Explanation: In the given string, the
first character which is non-repeating
is h, as it appears first and there is
no other 'h' in the string.

Example 2:

Input:
S = zxvczbtxyzvy
Output: c
Explanation: In the given string, 'c' is
the character which is non-repeating.  
 
ANSWER: 
 
 char nonrepeatingCharacter(string s)
{
map<char ,int> m;
for(int i=0;i<s.size();i++)
{
m[s[i]]++;
}

int foundwithsingleexistence=0;
for(int i=0;i<s.size();i++)
{
char a=s[i];
int num=m[a];
if(num==1)
{
foundwithsingleexistence=1;
return a;
}
}
if(foundwithsingleexistence==0)
{
return '$';
}

}
 

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.