最长子字符串-无重复字符

来源:互联网 发布:中文安卓编程 编辑:程序博客网 时间:2024/05/21 06:37

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.


class Solution(object):    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        ilen = 0        iStart = iEnd = 0        for a in s:            pos = s[iStart:iEnd].find(a)            if pos >= 0:                if ilen < iEnd - iStart:                    ilen = iEnd - iStart                iStart += pos + 1            iEnd += 1        if ilen < iEnd - iStart:            ilen = iEnd - iStart        return  ilen


空字符串开始,遇到重复调整字符串