Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, 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.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
#dp[i] = dp[i-1]+1 ,if s[i] != s[j] when i-dp[i-1]<= j <i
#dp[i] = i-j ,if s[i] == s[j] when i-dp[i-1]<= j <i
dp = [1]*len(s)
for i in range(1,len(s)):
for j in range(i-dp[i-1],i):
flag = 0
if s[i] == s[j]:
dp[i] = i - j
break
elif j == i-1:
dp[i] = dp[i-1] + 1
max_len = 0
print(dp)
for i in dp:
if max_len < i:
max_len = i
return max_len