最长子串

题目

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)
        

你可能感兴趣的:(最长子串)