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.
# -*- coding: utf-8 -*-
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
sublist = self.getSubStrs(s)
return self.getMaxLen(sublist)
def getSubStrs(self, instr):
sublist =[]
length = len(instr)
for i in range(length):
subStr = ""
for j in range(i, length):
val = instr[j]
if not val in subStr:
subStr += val
else:
break
sublist.append(subStr)
return sublist
def getMaxLen(self, subStrs):
mli = len(subStrs) - 1
for i in reversed(range(len(subStrs))):
if len(subStrs[i]) >= len(subStrs[mli]): mli = i
return len(subStrs[mli])
解析: