Logo for ammarahmed.ca
Medium
Jun 14, 2025
#sliding-window

3. Longest Substring Without Repeating Characters

Problem

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

Example 1:

1Input: s = "abcabcbb" 2Output: 3 3Explanation: The answer is "abc", with the length of 3. 4

Example 2:

1Input: s = "bbbbb" 2Output: 1 3Explanation: The answer is "b", with the length of 1. 4

Example 3:

1Input: s = "pwwkew" 2Output: 3 3Explanation: The answer is "wke", with the length of 3. 4Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. 5

Constraints:

  • 0 <= s.length <= 5 * 104
    • s consists of English letters, digits, symbols and spaces.

      Approach

      To solve this problem, we can use a sliding window approach.

      The idea is that we keep increasing our window as long as the characters are unique, once we hit a non-unique character, we decrease our window size from the other side (effectively sliding it) until our window is unique again.

      In order to do this, we can use a map of character counts with all values initialized to zero. As in, we create hash map of character's as the key's and the integers as the values. We iterate over the characters in the string and set each character in the map to zero.

      After this, we initialize our left and right pointers for the window to zero as well as our answer value to zero (we'll keep track of the max length we see in this value). We iterate while the right pointer is less than the length of the string. On each iteration, we check if the count in the map for the character at the right pointer is zero, if it is, we increase our window size and increment the count value. If it is not zero, we want to decrease our window size and slide it by increment the left pointer and decrementing the count at the left pointer because it is being removed from our substring.

      After these two checks, we update our max substring length with r - l.

      Complexity

      Time: O(n)

      We iterate over the string once.

      Space: O(n)

      We create a hashmap containing all the characters of the string.

      Solution

      1func lengthOfLongestSubstring(s string) int { 2 count := make(map[byte]int) 3 for _, c := range s { 4 count[byte(c)] = 0 5 } 6 7 var ( 8 l, r, res int 9 ) 10 for r < len(s) { 11 rCount := count[s[r]] 12 if rCount == 0 { 13 count[s[r]]++ 14 r++ 15 } else { 16 count[s[l]]-- 17 l++ 18 } 19 res = max(res, r-l) 20 } 21 return res 22} 23