Created
February 5, 2022 01:56
-
-
Save TheSithPadawan/cabe62486da25ff7fd84781a5865a041 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
// https://leetcode.com/problems/longest-substring-without-repeating-characters/ | |
#define um unordered_map | |
int solve (string s) { | |
um <char, int> ump; | |
int left = 0, right = 0; | |
int maxlen = 0; | |
int cnt = 0; | |
while (right < s.length()) { | |
ump[s[right]]++; | |
if (ump[s[right]] > 1) { | |
cnt++; | |
} | |
right++; | |
if (cnt == 0 && right - left > maxlen) { | |
maxlen = right - left; | |
} | |
while (cnt > 0 && left < right) { | |
ump[s[left]]--; | |
if (ump[s[left]] == 1) { | |
cnt--; | |
} | |
left++; | |
} | |
} | |
return maxlen; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment