- categories: Code, Interview Question, leetcode, Medium
- source: https://leetcode.com/problems/longest-substring-without-repeating-characters
- topics: Sliding Window
Given an array of positive integers nums
and a positive integer target
, return the minimal length of a subarray whose sum is greater than or equal to target
. If there is no such subarray, return 0
instead.
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
current_window_size = 0
used_chars = set()
ans = 0
for i in range(len(s)):
while s[i] in used_chars:
used_chars.remove(s[i - current_window_size])
current_window_size -= 1
used_chars.add(s[i])
current_window_size += 1
ans = max(ans, current_window_size)
return ans