Last active
October 1, 2019 22:40
-
-
Save archi144/373122eaf66a03a4d7ced47f7094e82e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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