Problem: 3. 无重复字符的最长子串
滑动窗口经典题目
时间复杂度:
添加时间复杂度, 示例: O ( n ) O(n) O(n)
空间复杂度:
添加空间复杂度, 示例: O ( n ) O(n) O(n)
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
ans = 0
left = 0
length = len(s)
dic = set()
for to in range(length):
if s[to] in dic:
ans = max(ans, to - left)
while s[left] != s[to]:
dic.remove(s[left])
left += 1
left += 1
else:
dic.add(s[to])
ans = max(ans, length - left);
return ans