美文网首页
最长子串

最长子串

作者: 水瓶鱼 | 来源:发表于2017-04-08 23:14 被阅读20次

题目

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.

Subscribe to see which companies asked this question.

python

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        return self.longest(s,0);
    def longest(self,s,position):
        result_str={}
        if len(s)==position:
            return 0
        issame=False
        next_position=position
        for i in range(position,len(s)):
            if s[i] in result_str:
                issame=True
                next_position=result_str[s[i]]+1
            else:
                if issame:
                    tem=self.longest(s,next_position)
                    return tem if tem>len(result_str) else len(result_str)
                result_str[s[i]]=i
        return len(result_str)
        

相关文章

网友评论

      本文标题:最长子串

      本文链接:https://www.haomeiwen.com/subject/ecedottx.html