Longest Substring Without Repeating Characters

Problem: Longest Substring Without Repeating Characters

To solve this problem, we use a sliding window with two pointers to slide through the string. Once the window met a duplicate character, store the length of current window as longest substring without repeating characters.

Code in Python:

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        m, i = 0, 0
        for j in xrange(len(s)):
            if s[j] in s[i:j]:
                m = max(j-i, m)
                i = s[i:j].index(s[j])+1+i
        m = max(len(s)-i, m)
        return m

results matching ""

    No results matching ""