Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Longest_Sub_String_without_Repeat_Characters_Sliding
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> set;
int n = s.size();
int i=0, j=0, maxLength = 0;
while(i<n&&j<n)
{
if(set.find(s[j])!=set.end())
{
i = max(i,set[s[j]]);
set.erase(s[j]);
}
else
{
set[s[j]] = j + 1;
maxLength = max(maxLength, j-i+1);
j++;
}
}
return maxLength;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment