Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Pattern Recognition Example 1
DSA

Pattern Recognition Example 1

Practice identifying the underlying algorithmic pattern from a coding problem.

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:

KeywordMeaning
LongestOptimization problem
SubstringContiguous
Without repeatingConstraint
CharactersFrequency 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:

  1. Expand right pointer
  2. Update state
  3. If constraint breaks → shrink from left
  4. 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

  1. Using nested loops → O(n²)
  2. Resetting window completely on duplicate
  3. Not using Math.max() when updating left pointer
  4. 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.

My Private Notes

Notes are auto-saved locally to this device.