Created
September 8, 2018 00:51
-
-
Save zhangxiaomu01/3efa0bf41ea4fc9435bd50ff6cc56d30 to your computer and use it in GitHub Desktop.
Longest_Sub_String_without_Repeat_Characters_Sliding
This file contains 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
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