Sliding Window/Medium

Longest Substring Without Repeating CharactersFREE TRACK

Time: O(N)Space: O(min(N, M))

Initialize Sliding Window

Input string: "abcabcbb". Set left pointer L=0, right pointer R=0, maxLen=0.

Array Elements & Pointers
L
a
[0]
b
[1]
c
[2]
a
[3]
b
[4]
c
[5]
b
[6]
b
[7]
Live Variables & Invariants
s:abcabcbb
L:0
R:0
maxLen:0
windowSet:
Step 1 / 176%
Solution Code
1
function lengthOfLongestSubstring(s: string): number {
2
  const set = new Set<string>();
3
  let l = 0, max = 0;
4
  for (let r = 0; r < s.length; r++) {
5
    while (set.has(s[r])) { set.delete(s[l]); l++; }
6
    set.add(s[r]);
7
    max = Math.max(max, r - l + 1);
8
  }
9
  return max;
10
}

Custom Test Case Runner

Input your custom values and visualize step-by-step trace

Quick Presets:

AI DSA Coach

Contextual Tutor for Longest Substring Without Repeating Characters

Hello! I am your AI DSA Tutor for **Longest Substring Without Repeating Characters** (Sliding Window). Ask me anything about this algorithm, time complexity, or request a step hint!