给定一个字符串,找出不含有重复字符的最长子串的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 无重复字符的最长子串是 "abc",其
长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 无重复字符的最长子串是 "b"
,其长度为 1。
class Solution: def lengthOfLongestSubstring(self, s): max_len = 0 if len(s) == 0: return 0 str_dict = {} once_max = 0 num_left = 0 for i in range(len(s)): if s[i] in str_dict and str_dict[s[i]] >= num_left: num_left = str_dict[s[i]]+1 once_max = i - num_left + 1 str_dict[s[i]] = i max_len = max(max_len,once_max) return max_len