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
Post a Comment