Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/3efa0bf41ea4fc9435bd50ff6cc56d30 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/3efa0bf41ea4fc9435bd50ff6cc56d30 to your computer and use it in GitHub Desktop.
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