3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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
        """
        num=0
        current=0
        str=""
        i=0
        length = len(s)
        while i<length:
            idx = str.find(s[i])
            if idx==-1:
                str+=s[i]
                current+=1
            else:
                num = max(num,current)
                i = i - len(str) + idx
                str=""
                current = 0
            i+=1
        num = max(num,current)
        return num
posted @ 2018-09-27 19:05  bernieloveslife  阅读(81)  评论(0编辑  收藏  举报