最长子串

2017-04-08  本文已影响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)
        
上一篇 下一篇

猜你喜欢

热点阅读