moonlightshadow123
6/19/2017 - 10:20 AM

3. Longest Substring Without Repeating Characters

  1. Longest Substring Without Repeating Characters
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        recd = [-1] * 256
        start = 0; max_len = 0
        for end in range(len(s)):
            if recd[ord(s[end])] >= start:
                start = recd[ord(s[end])] + 1
            recd[ord(s[end])] = end
            max_len = max(end - start + 1, max_len)
        return max_len
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        recd = [False] * 256
        start = 0; max_len = 0
        for end in range(len(s)):
            if recd[ord(s[end])] == True:
                while s[start] != s[end]:
                    recd[ord(s[start])] = False
                    start += 1
                start += 1
            recd[ord(s[end])] = True
            max_len = max(end - start + 1, max_len)
        return max_len

https://leetcode.com/problems/longest-substring-without-repeating-characters/#/description

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.