Created
June 11, 2022 18:35
-
-
Save headquarters/ac056635cd44671b0a449bf2ca73e87e to your computer and use it in GitHub Desktop.
Sliding window in TypeScript
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
// Find the longest length of a substring without repeating characters, e.g. "abcabc" => 3 | |
function lengthOfLongestSubstring(s: string): number { | |
let n = s.length; | |
let longest = 0; | |
let nextIndex = []; | |
for (let right = 0, left = 0; right < n; right++) { | |
left = Math.max(nextIndex[s.charAt(right)] || 0, left); | |
longest = Math.max(longest, right - left + 1); | |
nextIndex[s.charAt(right)] = right + 1; | |
} | |
return longest; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment