Given a string, find the length of the 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.

class Solution:
def lengthOfLongestSubstring(self, s):
“”\"
:type s: str
:rtype: int
“”\"
start = 0
end = 1
length = 0
for i in range(len(s)):
if s[i] in s[start:i]:
# if the current character is in the substring before
if length < len(s[start:end]):
length = len(s[start:end])
while(s[i] in s[start:i]):
start = start + 1

        end = i + 1
    
    # to make no repeat situation result no zero
    if length < len(s[start:end]):
        length = len(s[start:end])
    
    return length

###########################
start=-1
max=0
d={}
for i in range(len(s)):
if s[i] in d and d[s[i]]>start:
start=d[s[i]]
d[s[i]]=i

    else:
        d[s[i]]=i
        if i-start>max:
            max=i-start
return max
#####################
maxx = 0

substr = {}

substr = [s[i] for i in range(len(s))]

for i,a in enumerate(s):

substr[i] = s[i]

    if i < len(s)-1:
        j = i+1
        while j < len(s):
            if s[j] in substr[i]:
                j = len(s)
            else:
                substr[i] = substr[i] + s[j]
                j = j + 1
le = [len(substr[i]) for i in range(len(substr))]
if len(le) != 0:
    maxx = max(le)
return maxx
收藏 打印