LeetCode 3. Longest Substring Without Repeating Characters

Description

https://leetcode.com/problems/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.

Explanation

We use two pointers technique to solve the problem. One slow pointer i, one fast pointer j.

We also add a HashSet to store the characters which have been visited by j pointer to help detect repeating characters.

We keep moving j pointer right further.

  • If current s.charAt(j) character is not in the HashSet, we add the character to the HashSet and keep moving j further.
  • If current s.charAt(j) character is in the HashSet, we remove the character i is visiting and move i forward. At this point, we found the maximum size of substrings without duplicate characters start with index i. We move pointer one step further.

When j pointer iterates all the characters of the string, we get the max length of the longest substring without repeating characters.

Java Solution

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int maxLength = 0;
        HashSet<Character> set = new HashSet<>();
        
        int i = 0;
        int j = 0;
        while (j < s.length()) {
            if (!set.contains(s.charAt(j))) {
                set.add(s.charAt(j));
                j++;
                maxLength = Math.max(maxLength, j - i);
            } else {
                set.remove(s.charAt(i));
                i++;
            }
        }
        return maxLength;
    }
}

Python Solution

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        longest = 0

        visited = set()        
        i = 0
        j = 0
        while j < len(s):                        
            if s[j] not in visited:             
                visited.add(s[j])
            else:
                longest = max(longest, len(visited))
                visited = set()        
                i += 1 
                j = i
                visited.add(s[j])
                
            j += 1
                
        return max(longest, len(visited))
  • Time complexity: O(n).
  • Space complexity: O(m). m is the size of the charset.

9 Thoughts to “LeetCode 3. Longest Substring Without Repeating Characters”

  1. Hello There,
    If we consider this use-case, “abcddefghijal” – for this string, shouldn’t the output be 6 and not 9? i.e. (efghij) and not (defghijal) as d and a are repeating. Please correct me if I am wrong. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *