文章目录[隐藏]
Descreption
Given a string s, find the length of the longest substring without repeating characters.
Solution
sliding windows, The most import things is how to get The max.
Code
- submit code
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
left = 0
right = 0
window_dict = {}
max = 0
tmp = ""
while left < len(s):
INPUT = s[left]
left += 1
tmp += INPUT
window_dict[INPUT] = window_dict.get(INPUT,0) + 1
if (max < len(tmp) and window_dict.get(INPUT) == 1):
max = len(tmp)
while(window_dict.get(INPUT) > 1):
OUTPUT = s[right]
right += 1
tmp = tmp[1:]
if(max < len(tmp)):
max = len(tmp) - 1
window_dict[OUTPUT] = window_dict.get(OUTPUT) - 1
return max
- full code
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# if(len(s) == 1):
# return 1
left = 0
right = 0
window_dict = {}
max = 0
tmp = ""
while left < len(s):
INPUT = s[left]
left += 1
tmp += INPUT
# if (max < len(tmp)):
# max = len(tmp)
print("tmp:" + tmp)
window_dict[INPUT] = window_dict.get(INPUT,0) + 1
if (max < len(tmp) and window_dict.get(INPUT) == 1):
max = len(tmp)
while(window_dict.get(INPUT) > 1):
print("__________________desec__________________")
OUTPUT = s[right]
right += 1
if( max < len(tmp)):
max = len(tmp) - 1
tmp = tmp[1:]
window_dict[OUTPUT] = window_dict.get(OUTPUT) - 1
return max
S = Solution()
s = ""
s = "a"
# s = " a"
# s = "abcabcbb"
print(S.lengthOfLongestSubstring(s))