I have code where I am trying to find non repeating longest string. To below function I pass string and I assign first character to temp string. Then I traverse original string character by character and see if this character is present in temp string.
To do this I am using string::find function and check std::String::npos to make sure I dont get valid index. But I always get std::string::npos. I am not sure what is wrong with find function used here
int lcs(string s) {
std::string str;
std::string maxstr;
int max = -1;
str+=s[0];
for(int i = 1 ; i < s.length(); i++)
{
if(str.find(to_string(s[i])) == std::string::npos)
str+=s[i];
else
{
if(str.length() > max)
max = str.length();
maxstr = str;
str.clear();
}
}
return max;
}
int main()
{
int i = lcs("abcabcbb");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire