Longest Substring Without Repeating Characters | Sliding Windows

# 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))

``````

### 要不赞赏一下?

 微信 支付宝 PayPal Bitcoin

``https://www.emperinter.info/2022/02/26/longest-substring-without-repeating-characters-sliding-windows/``

## 优惠码

 阿里云国际版 20美元 Vultr 10美元 搬瓦工 | Bandwagon 应该有折扣吧？ Just My Socks JMS9272283 【注意手动复制去跳转】 域名 | namesilo `emperinter`(1美元) 币安 币安