mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
19 lines
458 B
C++
19 lines
458 B
C++
class Solution {
|
|
public:
|
|
int lengthOfLongestSubstring(string s) {
|
|
int n =s.length();
|
|
vector<int>charIndex(128,-1);
|
|
int left = 0;
|
|
int maxlen =0;
|
|
for(int right =0;right<n;right++){
|
|
if(charIndex[s[right]]>=left){
|
|
left = charIndex[s[right]]+1;
|
|
}
|
|
charIndex[s[right]] = right;
|
|
maxlen = max(maxlen, right-left+1);
|
|
}
|
|
return maxlen;
|
|
|
|
}
|
|
};
|