Let’s train pattern recognition properly.
We will not jump to code.
We will diagnose the problem like a senior engineer.
Problem (Classic LeetCode)
Given a string
s, find the length of the longest substring without repeating characters.
Step 1 — Identify Input Type
Input:
- String
Strings usually map to:
- Sliding Window
- Hashing
- Two Pointers
- DP on Strings
- Trie (if prefix-related)
Step 2 — Look for Keywords
Important words:
- Longest
- Substring
- Without repeating
- Characters
Let’s decode them:
| Keyword | Meaning |
|---|---|
| Longest | Optimization problem |
| Substring | Contiguous |
| Without repeating | Constraint |
| Characters | Frequency tracking needed |
Immediately this screams:
Sliding Window + HashMap
Step 3 — Constraint Type
Substring = contiguous.
Whenever you see:
- Longest subarray
- Smallest window
- At most k
- Without repeating
Think:
Expand window → Violation? → Shrink window.
This is classic sliding window.
Step 4 — Identify State
We need to maintain:
- A left pointer
- A right pointer
- A data structure to track duplicates
Which structure?
We need:
- Fast lookup
- Track presence
So:
- HashSet (if only checking existence)
- HashMap (if tracking frequency or index)
Pattern Identified
Pattern = Variable Size Sliding Window
Template:
- Expand right pointer
- Update state
- If constraint breaks → shrink from left
- Update answer
Clean Java Implementation
public int findLongestSubstringWithoutRepeating(final String input) {
if (input == null || input.length() == 0) {
return 0;
}
Map<Character, Integer> characterToIndex = new HashMap<>();
int leftPointer = 0;
int maximumLength = 0;
for (int rightPointer = 0; rightPointer < input.length(); rightPointer++) {
char currentCharacter = input.charAt(rightPointer);
if (characterToIndex.containsKey(currentCharacter)) {
leftPointer = Math.max(
leftPointer,
characterToIndex.get(currentCharacter) + 1
);
}
characterToIndex.put(currentCharacter, rightPointer);
int currentWindowLength = rightPointer - leftPointer + 1;
maximumLength = Math.max(maximumLength, currentWindowLength);
}
return maximumLength;
}
Complexity
Time Complexity: O(n) — each character processed at most twice.
Space Complexity: O(min(n, charset)) — map stores characters.
Why This Pattern Works
Because:
- We need contiguous segment → window
- We need constraint enforcement → shrink
- We need optimal length → track max
Sliding window is optimal for contiguous constraint problems.
Common Mistakes
- Using nested loops → O(n²)
- Resetting window completely on duplicate
- Not using
Math.max()when updating left pointer - Confusing substring with subsequence
Recognition Summary
If you see:
- Longest substring
- Without repeating
- At most k distinct
- Smallest window containing
You should immediately think:
Sliding Window.
Pattern recognition reduces 15 minutes of confusion into 30 seconds of clarity.
Next, we can do:
- Example 2 → Binary Search on Answer
- Example 3 → Graph Hidden in Grid
- Example 4 → DP disguised as Greedy
- Example 5 → Monotonic Stack trap
Choose the next training case.
Premium Content
Unlock Pattern Recognition Example 1 and all premium lessons with a subscription.
From ₹199.99/year — See plans