Sliding Window Two-Pointer Visualizer — Longest Substring Without Repeats

Sliding Window Two-Pointer Visualizer (Longest Substring Without Repeats) · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

What's included

Features

Two-pointer window
L and R badges on the characters.
Duplicate highlighting
The earlier copy that forces the jump.
Last-seen map
Character → most recent index.
Stale-index check
Repeats before L are ignored.
Best window tracking
Underlined and reported with its length.
Step counter
Shows O(n) work directly.
Plain-language log
Why each pointer moved.
Custom input
Try your own strings, escaped safely.

About this UI Snippet

The Sliding Window Technique — Two Pointers That Never Go Backwards

Screenshot of the Sliding Window Two-Pointer Visualizer (Longest Substring Without Repeats) snippet rendered live

"Find the longest substring without repeating characters" is one of the most common coding interview questions, and the reason it matters is the technique behind it. A brute-force solution checks every substring, O(n²) or worse. The sliding window solves it in one pass by keeping a window of characters between two pointers and only ever moving them forward.

The invariant

Everything between the left pointer L and the right pointer R has no repeated characters. Each step moves R one character to the right. If the new character isn't already inside the window, the window grows. If it is, L jumps to just past that character's previous position, which removes the duplicate and everything before it in one move.

The last-seen map

To make that jump in O(1), the algorithm stores the most recent index of every character. When a character comes back, the map says exactly where the old copy is. Note the check last[c] >= left: a character seen *before* the current window doesn't count as a repeat, which is the most common bug in this problem.

Why it's linear

R visits each index once. L only moves forward and never passes R. So the total work is proportional to the length of the string, which the step counter shows: n steps for n characters.

Reading the visual

Orange cells are inside the window, the L and R badges mark the pointers, a red cell is the earlier copy that forced L to jump, and the green underline marks the best window found so far. The map panel shows the last-seen index of each character.

The pattern generalises

The same two-pointer idea solves "smallest subarray with sum ≥ K", "longest substring with at most K distinct characters" and "minimum window substring". The shape is always: grow on the right, shrink from the left until the invariant holds again.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet into an AI assistant like Claude and ask it to walk through the default string and explain every left-pointer jump. Ask it to adapt the visualizer to "at most K distinct characters", to minimum window substring with a target multiset, or to a numeric array version for maximum-sum windows. It can also show the O(n²) brute force side by side with a step counter.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a step-by-step sliding window visualizer for "longest substring without repeating characters" in plain HTML, CSS and JavaScript.

Requirements:
- An input for a string up to 24 characters, drawn as a row of indexed character cells.
- Each Step moves the right pointer forward one character; if that character's last-seen index is inside the window, move the left pointer to one past it; update the last-seen map; record a new best window when the window is longer than the best so far.
- Show L and R pointer badges, shade cells inside the window, highlight the earlier duplicate that caused a jump, and underline the best window.
- Show the last-seen map and a plain-language log of each step.
- Count steps against the string length to show linear time, and add Run/Pause.
- When finished, report the longest substring and its length. Escape user input.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="sw">
  <div class="sw-top">
    <div>
      <h2>Sliding window: longest substring without repeating characters</h2>
      <p>The right pointer only moves forward; the left pointer only moves forward. Every character enters and leaves the window at most once.</p>
    </div>
    <form class="sw-form" id="swForm">
      <input id="swInput" value="pwwkewabcdcba" maxlength="24" aria-label="String" autocomplete="off">
      <button type="submit">Load</button>
    </form>
  </div>
  <div class="sw-strip" id="swStrip" aria-hidden="true"></div>
  <div class="sw-ctrl">
    <button type="button" id="swStep">Step</button>
    <button type="button" id="swRun">Run</button>
    <span class="sw-count" id="swCount"></span>
  </div>
  <div class="sw-panels">
    <div class="sw-panel"><h3>Last seen at</h3><div class="sw-map" id="swMap"></div></div>
    <div class="sw-panel"><h3>What happened</h3><p id="swLog" aria-live="polite"></p></div>
  </div>
</div>

Step by step

How to Use

  1. 1
    Load a stringUp to 24 characters; the default contains several repeats.
  2. 2
    StepR moves one character; watch whether L has to jump.
  3. 3
    Watch the mapThe last index of each character; the repeat is highlighted.
  4. 4
    RunAuto-step to the end and read the result.
  5. 5
    Count the stepsSteps equal the string length: linear time.

Real-world uses

Common Use Cases

Coding interview practice
One of the most asked string problems.
Learning algorithm patterns
Two pointers and sliding windows.
Teaching complexity
See why O(n²) becomes O(n).
Streaming data
The same idea underlies rolling windows.
Self-testing
Predict the pointers before each step.
Related: Big-O Growth Visualizer
Why linear beats quadratic: Big-O Complexity Growth Visualizer.
Related: Binary Search Visualizer
Another pointer-based technique: Binary Search Visualizer.

Got questions?

Frequently Asked Questions

Keeping a range between two indexes over an array or string and moving both ends forward to maintain a condition, instead of re-examining every possible range. It usually turns an O(n²) search into an O(n) pass.

Move a right pointer over the string, storing each character's last index. If the character was last seen inside the window, move the left pointer to one past that index. Track the largest right − left + 1 seen.

The map remembers characters from before the current window. Those copies are no longer in the window, so they don't create a repeat. Without the check, the left pointer could jump backwards.

O(n): the right pointer visits each character once and the left pointer only moves forward. Space is O(min(n, alphabet size)) for the map.

Minimum window substring, longest substring with at most K distinct characters, maximum sum subarray of size K, and smallest subarray with sum at least K.