const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Patterns

LeetCode String Problems: 6 Patterns to Solve Any Question

String problems are the most common Easy/Medium category in coding interviews — master these 6 patterns and you can handle any string question thrown at you.

10 min read|

String problems appear in over 30% of coding interviews

6 patterns that cover every string question: two pointers, sliding window, hash maps, and more

Why LeetCode String Problems Matter in Interviews

String problems appear in over 30% of coding interviews, making them the single most common category you will face. Companies love string questions because they test fundamental manipulation skills — indexing, slicing, character comparison — alongside deeper pattern recognition abilities.

The good news is that leetcode string problems follow a small number of repeatable patterns. Once you learn to recognize which pattern fits a problem, the solution becomes almost mechanical. You stop guessing and start matching.

This guide covers 6 core patterns that handle virtually every string question you will encounter: two pointers, sliding window, hash maps for frequency counting, string building techniques, palindrome-specific patterns, and general pattern matching strategies. Each pattern maps to specific problem types with clear signals that tell you when to apply it.

Whether you are preparing for your first technical interview or brushing up before an on-site, these string algorithms form the foundation of your problem-solving toolkit.

Two Pointers on Strings — Palindromes and Reversal

The two-pointer technique is your first tool for any string problem that involves comparing characters from both ends. You place one pointer at the start and another at the end, then move them toward each other while checking conditions. This pattern is the backbone of palindrome problems and in-place string reversal.

Valid Palindrome (#125) is the classic entry point. You skip non-alphanumeric characters and compare lowercase versions of characters at each pointer. When the pointers meet in the middle without a mismatch, the string is a palindrome. The time complexity is O(n) with O(1) space — exactly what interviewers want to see.

Valid Palindrome II (#680) adds a twist: you can remove at most one character. When you find a mismatch, you try skipping either the left or right character and check if the remaining substring is a palindrome. This teaches you how to extend the basic two-pointer template with branching logic.

The signal to use two pointers on strings is clear: if the problem asks about palindromes, reversal, or comparing characters from opposite ends, start with two pointers. It runs in linear time, uses constant space, and handles most Easy-level string manipulation interview questions.

  • Valid Palindrome (#125) — skip non-alphanumeric, compare from both ends, O(n) time O(1) space
  • Valid Palindrome II (#680) — allow one deletion, branch on mismatch, still O(n)
  • Reverse String (#344) — swap characters at left and right pointers moving inward
  • Signal: problem mentions "palindrome", "reverse", or "compare from both ends"
ℹ️

Interview Insight

String problems are the most common Easy-level interview questions — companies use them as warm-up rounds to assess basic coding fluency before moving to harder problems.

Sliding Window on Strings — Substring and Frequency Problems

Sliding window is the most powerful pattern for substring problems on leetcode. You maintain a window defined by two pointers (left and right) and expand or contract it based on constraints. Combined with a hash map for character frequencies, this pattern solves a huge class of string problems efficiently.

Longest Substring Without Repeating Characters (#3) is the gateway problem. You expand the window by moving the right pointer, tracking characters in a set. When you hit a duplicate, you shrink the window from the left until the duplicate is removed. The maximum window size at any point is your answer — O(n) time.

Minimum Window Substring (#76) is the hard-level evolution. You need the smallest window in string s that contains all characters of string t. You expand until all characters are covered, then shrink from the left to minimize the window. This problem teaches the full expand-then-contract sliding window template that applies to dozens of substring problems.

The key insight is that sliding window converts what looks like an O(n^2) brute force — checking every possible substring — into O(n) by reusing work. Each character enters and leaves the window at most once, giving you amortized linear time.

  • Longest Substring Without Repeating Characters (#3) — expand right, shrink left on duplicate, O(n)
  • Minimum Window Substring (#76) — expand to cover all chars, shrink to minimize, O(n)
  • Permutation in String (#567) — fixed-size sliding window with character frequency match
  • Template: expand right pointer, check constraint, shrink left pointer if violated

Hash Map for Anagram Problems and Character Frequency

Hash maps are your go-to tool when a string problem involves character counting, anagram detection, or grouping strings by some property. The pattern is simple: build a frequency map of characters, then use it to compare, group, or validate.

Valid Anagram (#242) is the foundation. You count characters in both strings using a hash map (or a fixed-size array of 26 for lowercase letters) and compare the counts. If they match, the strings are anagrams. This runs in O(n) time and teaches the core frequency-counting pattern.

Group Anagrams (#49) extends this by using the sorted version of each string (or a character count tuple) as a hash map key. All anagrams produce the same key, so they land in the same group. This is a Medium-level problem that tests your ability to choose the right hash key.

Permutation in String (#567) combines hash maps with sliding window. You maintain a frequency map of the target pattern and slide a fixed-size window across the source string, comparing frequency maps at each position. When the maps match, you have found a permutation. This problem sits at the intersection of two patterns and appears frequently in interviews.

  • Valid Anagram (#242) — frequency count comparison, O(n) time, O(1) space with fixed array
  • Group Anagrams (#49) — sorted string or count tuple as hash key for grouping
  • Permutation in String (#567) — sliding window + frequency map comparison
  • Ransom Note (#383) — character availability check using frequency counting
  • Signal: problem mentions "anagram", "permutation", "frequency", or "character count"
💡

Pro Tip

For any problem involving "substring with certain characters", reach for sliding window + hash map. This combo solves Longest Substring (#3), Minimum Window Substring (#76), and Permutation in String (#567).

String Building and In-Place Manipulation

Many string problems require you to construct a new string or modify one in place. The key pattern here is using an array or StringBuilder to accumulate characters, then joining at the end. This avoids the O(n^2) trap of string concatenation in a loop, which creates a new string object on every iteration.

String to Integer (atoi) (#8) tests careful string parsing: skip whitespace, handle an optional sign, read digits until a non-digit appears, and clamp to 32-bit integer range. It is less about algorithms and more about handling edge cases precisely — which is exactly what interviewers evaluate.

Encode and Decode Strings (#271) is a classic design problem. You need a format that can encode a list of strings into a single string and decode it back without ambiguity. The standard approach uses a length prefix followed by a delimiter (like "4#code") so you always know exactly how many characters to read. This teaches delimiter design thinking.

Reverse Words in a String (#151) combines string splitting with reversal. You split by whitespace, filter empty strings, reverse the array of words, and join with single spaces. In languages with mutable strings, you can do this in-place with a two-pass approach: reverse the entire string, then reverse each word individually.

  • String to Integer (#8) — parsing with edge case handling: whitespace, signs, overflow
  • Encode and Decode Strings (#271) — length-prefix encoding for unambiguous serialization
  • Reverse Words in a String (#151) — split, reverse, join or in-place two-pass reversal
  • Longest Common Prefix (#14) — vertical scanning or binary search on prefix length

Palindrome Patterns — Expand Around Center and Dynamic Programming

Palindrome problems beyond simple checking require specialized techniques. The two most important are expand-around-center for substrings and dynamic programming for subsequences. Knowing which to use depends on whether the problem asks for contiguous substrings or non-contiguous subsequences.

Longest Palindromic Substring (#5) is solved optimally with expand-around-center. For each index (and each pair of adjacent indices), you expand outward while characters match. This runs in O(n^2) time with O(1) space and is simpler than the DP approach. It tests your ability to enumerate all possible palindrome centers efficiently.

Palindromic Substrings (#647) uses the same expand-around-center technique but counts all palindromes instead of tracking the longest. For each center, every successful expansion adds one more palindromic substring to your count. The pattern is identical to problem #5 — just change what you track.

Longest Palindromic Subsequence (#516) requires dynamic programming because subsequences are not contiguous. You build a 2D DP table where dp[i][j] represents the longest palindromic subsequence in s[i..j]. If the characters at i and j match, dp[i][j] = dp[i+1][j-1] + 2. Otherwise, dp[i][j] = max(dp[i+1][j], dp[i][j-1]). This is O(n^2) time and space.

  • Longest Palindromic Substring (#5) — expand around center, O(n^2) time, O(1) space
  • Palindromic Substrings (#647) — count all palindromes using expand-around-center
  • Longest Palindromic Subsequence (#516) — 2D DP table, O(n^2) time and space
  • Key distinction: substrings are contiguous (expand-around-center), subsequences are not (DP)
⚠️

Performance Trap

String concatenation in a loop is O(n^2) in Java and Python — always use StringBuilder (Java), ''.join() (Python), or array push (JS) instead. This is a common interview performance trap that interviewers specifically watch for.

Practice Strategy for Mastering String Problems

Start with the two foundational problems: Valid Palindrome (#125) and Valid Anagram (#242). These are Easy-level problems that teach the core two-pointer and hash-map patterns. Solve them until the approaches are automatic — you should be able to code either one in under 5 minutes.

Next, move to sliding window strings with Longest Substring Without Repeating Characters (#3). This problem is asked so frequently that interviewers expect you to know the pattern cold. Once you have it down, tackle Minimum Window Substring (#76) to learn the full expand-contract template.

For palindrome problems, master Longest Palindromic Substring (#5) with expand-around-center before attempting the DP-based Longest Palindromic Subsequence (#516). The expand technique is simpler, more space-efficient, and covers the majority of palindrome interview questions.

Use YeetCode flashcards to drill pattern recognition across all 6 categories. The key to interview success with string problems is not memorizing solutions — it is recognizing which pattern applies and executing the template quickly. Spaced repetition builds that recognition into long-term memory so it is available under interview pressure.

  1. 1Week 1: Solve Valid Palindrome (#125) and Valid Anagram (#242) — master two pointers and hash maps
  2. 2Week 2: Tackle Longest Substring Without Repeating Characters (#3) and Permutation in String (#567) — learn sliding window on strings
  3. 3Week 3: Work through Longest Palindromic Substring (#5) and Group Anagrams (#49) — combine expand-around-center with hash map grouping
  4. 4Week 4: Attempt Minimum Window Substring (#76) and Encode and Decode Strings (#271) — advanced sliding window and string design
  5. 5Ongoing: Review all patterns with YeetCode flashcards using spaced repetition to maintain recall

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now