javac StringPatterns.java && ./run --mode=interview-prep

String Algorithm Patterns

Learn string manipulation techniques with animated visualizations. Watch sliding windows, frequency counts, two-pointer normalization, and prefix matching in action — step-by-step.

01_longest_substring.java
LC 3 — Longest Substring Without Repeating Characters. Use a sliding window: expand the right pointer, then shrink the left pointer until the current window has no duplicates. This is the classic “window with frequency map” pattern.
General Definition

Maintain a window of characters that have no repeats, expanding to the right and shrinking from the left whenever a duplicate appears. The longest valid window is the answer.

Professional Definition — say this in an interview

The sliding-window strategy keeps a frequency count for the current window and moves the left boundary only when the right character causes a duplicate. Because each position enters and leaves the window at most once, the time complexity is O(n) and the extra space is O(k), where k is the size of the character set.

Interview keywords — if you hear these, think of this pattern
sliding windowduplicate charactersfrequency maplongest unique substringtwo pointers
SOURCE
Java
public int lengthOfLongestSubstring(String s) {
    int[] freq = new int[128];
    int left = 0, best = 0;
    for (int right = 0; right < s.length(); right++) {
        freq[s.charAt(right)]++;
        while (freq[s.charAt(right)] > 1) {
            freq[s.charAt(left)]--;
            left++;
        }
        best = Math.max(best, right - left + 1);
    }
    return best;
}
DEBUGGER — live watch0 / 8
a
0
b
1
c
2
a
3
b
4
c
5
b
6
b
7
Not started
DEBUG STEPS0
Press ▶ Play or Next to start stepping through the algorithm.