Skip to content

Instantly share code, notes, and snippets.

@TheSithPadawan
Created February 5, 2022 01:56
Show Gist options
  • Save TheSithPadawan/cabe62486da25ff7fd84781a5865a041 to your computer and use it in GitHub Desktop.
Save TheSithPadawan/cabe62486da25ff7fd84781a5865a041 to your computer and use it in GitHub Desktop.
// 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