私が実装した方法:スライディングウィンドウメソッド:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
start = 0
cur = 0
length = len(s)
if length == 0:
return 0
elif length == 1:
return 1
cur_res = ''
tempdict = {}
rem_res = ''
rem_len = 1
while cur != length:
#if len(tempdict) == 26:
# return cur_res
if s[cur] not in tempdict:
tempdict[s[cur]] = cur
cur_res += s[cur]
cur += 1
elif s[cur] in tempdict:
temp_len = cur - start
if temp_len > rem_len:
rem_res = s[start:cur]
rem_len = temp_len
start = tempdict[s[cur]]+1
tempdict = {}
tempdict[s[start]] = start
cur = start + 1
if start == 0:
return length
if cur - start > rem_len and s[start] != s[cur-1]:
return (cur - start )
return rem_len