Skip to content

Instantly share code, notes, and snippets.

@archi144
Last active October 1, 2019 22:40
Show Gist options
  • Save archi144/373122eaf66a03a4d7ced47f7094e82e to your computer and use it in GitHub Desktop.
Save archi144/373122eaf66a03a4d7ced47f7094e82e to your computer and use it in GitHub Desktop.
#include <vector>
#include <iterator>
int lengthOfLongestSubstring(string s) {
vector <char> alph;
char ch{};
int count{};
int max_count{};
for(size_t i=0; i<s.size(); ++i)
{
ch = s[i];
auto it = find(alph.begin(),alph.end(),ch);
if( it != alph.end())
{
count=count-distance(alph.begin(),it);
alph.erase(alph.begin(),++it);
alph.push_back(ch);
}
else
{
alph.push_back(ch);
count++;
if(max_count < count) max_count=count;
}
}
return max_count;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment