Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
python3:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
longest = 0
s_set = {}
count = 0
i = 0
last_save = 0
while i < len(s):
st = s[i]
j = s_set.get(st)
if j is not None and j >= last_save:
if count > longest:
longest = count
count = i - j
s_set[st] = i
last_save = j + 1
else:
count = count + 1
s_set[st] = i
i = i + 1
if count > longest:
longest = count
return longest