题目链接:https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/
难度:中等

描述:
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。

题解

  1. class Solution:
  2. def lengthOfLongestSubstring(self, s: str) -> int:
  3. char_set = set()
  4. left = 0
  5. ret = 0
  6. for right in range(len(s)):
  7. if s[right] in char_set:
  8. while s[right] in char_set:
  9. char_set.remove(s[left])
  10. left += 1
  11. char_set.add(s[right])
  12. ret = max(ret, right - left + 1)
  13. return ret