String Manipulation Patterns in DSA — The Definitive Guide
- Pattern recognition before coding: identify whether the problem needs a fixed or variable window, symmetric comparison, or frequency fingerprint before touching the keyboard — the right pattern choice eliminates 80% of implementation bugs.
- The sliding window left pointer must only ever move forward — the missing Math.max guard is the single most common sliding window bug in interview settings and production code alike.
- int[26] is strictly better than HashMap for lowercase-only string problems: no autoboxing, no collision handling, constant-time comparison via Arrays.equals — always state this trade-off in an interview.
- Sliding window: contiguous substring problems (longest/shortest/min window)
- Two pointers: symmetry problems (palindrome, reverse, compare ends)
- Frequency map: character count problems (anagram, permutation, unique chars)
- String hashing: grouping/dedup problems (group anagrams, Rabin-Karp search)
- Build-then-join: always use StringBuilder, never + in a loop
- Search engines, autocomplete, log dedup, and content fingerprinting all rely on these patterns.
- The missing Math.max guard in sliding window is the #1 silent bug.
- String concatenation with + inside a loop. O(n^2) on 10K-char strings. Use StringBuilder.
Sliding window gives wrong answer on 'abba' or 'tmmzuxt'.
Add trace: System.out.println("before max: " + windowStart + " lastSeen: " + lastSeenAt.get(ch))Verify windowStart = Math.max(windowStart, lastSeenAt.get(ch) + 1)String processing is 100x slower than expected on 10K-char input.
Profile with -XX:+PrintGCDetails — look for excessive GC from char[] allocationsSearch code for: result += or result = result +Frequency map throws ArrayIndexOutOfBoundsException.
Print the character that crashes: System.out.println("char=" + ch + " code=" + (int)ch)If code > 127 or char is uppercase, the int[26] assumption is wrongAnagram detection is O(n log n) and TLE on large inputs.
Count key-building operations: is it O(k) or O(k log k) per word?If sorting: switch to frequency-count key (int[26] → string key)Production Incident
matches.size() * 15) to avoid resize overhead.
5. Added a unit test that builds a 10,000-element string and asserts no GC pressure.Production Debug GuideSymptom-first investigation path for string pattern bugs.
Character.toLowerCase() before indexing, or switch to HashMap<Character, Integer>.s1.length() != s2.length(), they cannot be anagrams. Return false immediately.Strings are the most common data type in production systems. Search engines scan billions of words per second. Autocomplete predicts the next word from a prefix. Log deduplication identifies duplicate entries by content fingerprinting. Each of these relies on a small set of string manipulation patterns.
The core problem with naive string processing is O(n^2) concatenation and O(n^2) substring enumeration. A 10,000-character string processed naively can require 100 million comparisons. The patterns covered here — sliding window, two pointers, frequency maps, and hashing — reduce most string problems to O(n) or O(n log n).
The common misconception is that 'string problems are just array problems with characters.' While the algorithmic patterns overlap, strings introduce unique concerns: immutable concatenation cost, character encoding (ASCII vs Unicode), case sensitivity, and the choice between int[26] and HashMap for frequency counting. Understanding these distinctions is what separates a correct solution from a production-grade one.
Worked Example — Reverse Words in a String
Input: s = ' the sky is blue '
- Split by whitespace (ignore empty tokens): words = ['the','sky','is','blue'].
- Reverse the list: ['blue','is','sky','the'].
- Join with single space: 'blue is sky the'.
Now trace character-by-character palindrome check on 'racecar': 1. left=0 (r), right=6 (r). Match. 2. left=1 (a), right=5 (a). Match. 3. left=2 (c), right=4 (c). Match. 4. left=3 (e) = right=3 (e). Left >= right, stop. Result: palindrome.
Anagram check 'listen' vs 'silent': sort both → 'eilnst' == 'eilnst'. True. Or use frequency count: count each char in both strings; if all counts match, they are anagrams. O(n) time with hash map vs O(n log n) with sorting.
package io.thecodeforge.algo; import java.util.Arrays; public class StringWorkedExamples { /** * Reverses the order of words in a string. * Handles multiple spaces between words. */ public static String reverseWords(String s) { // Split on whitespace, trim leading/trailing spaces first String[] words = s.trim().split("\\s+"); // Reverse in-place using two pointers int left = 0, right = words.length - 1; while (left < right) { String temp = words[left]; words[left] = words[right]; words[right] = temp; left++; right--; } // Join with single space — never concatenate in a loop return String.join(" ", words); } /** * Checks if a string is a palindrome (alphanumeric only, case-insensitive). */ public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++; while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--; if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { return false; } left++; right--; } return true; } /** * Checks if two strings are anagrams using int[26] frequency map. O(n) time, O(1) space. */ public static boolean areAnagrams(String s1, String s2) { if (s1.length() != s2.length()) return false; int[] freqint i = 0; i < s1.length(); = new int[26]; for ( i++) { freq[Character.toLowerCase(s1.charAt(i)) - 'a']++; freq[Character.toLowerCase(s2.charAt(i)) - 'a']--; } for (int count : freq) { if (count != 0) return false; } return true; } public static void main(String[] args) { System.out.println(reverseWords(" the sky is blue ")); // "blue is sky the" System.out.println(isPalindrome("A man, a plan, a canal: Panama")); // true System.out.println(areAnagrams("listen", "silent")); // true System.out.println(areAnagrams("hello", "world")); // false } }
true
true
false
- Compare characters: two pointers moving inward. Palindrome, reverse, symmetry.
- Count characters: frequency map (int[26] or HashMap). Anagram, permutation, unique.
- Track region: sliding window with two boundary pointers. Longest/shortest substring.
- Combine patterns: minimum window substring = sliding window + frequency map.
- Build output: always StringBuilder or String.join, never + in a loop.
Key String Manipulation Patterns — Plain English
String problems cluster into a handful of recurring patterns. Recognising the pattern determines the approach.
Pattern 1 — Frequency map (anagram, permutation check): Build a character count dict. Two strings are anagrams when their counts are equal.
Pattern 2 — Two pointers (palindrome, reverse): left=0, right=n-1. While left<right: compare or swap. O(n), O(1) space.
Pattern 3 — Sliding window (minimum window substring, longest without repeating): Expand right; shrink left when constraint violated.
Pattern 4 — Build output in a list, join once: Appending to a list is O(1); string concatenation with '+' is O(n). Always join at the end.
Step-by-step — is 'listen' an anagram of 'silent'? 1. Both length 6: ok. 2. Count 'listen': {l:1,i:1,s:1,t:1,e:1,n:1}. 3. Count 'silent': {s:1,i:1,l:1,e:1,n:1,t:1}. 4. Maps equal. Answer: True.
Step-by-step — reverse 'hello' in-place: left=0,right=4: swap h↔o → 'oellh'. left=1,right=3: swap e↔l → 'olleh'. Done.
package io.thecodeforge.algo; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class StringPatterns { /** * Pattern 1: Frequency map anagram check. O(n) time, O(1) space. */ public static boolean areAnagrams(String s1, String s2) { if (s1.length() != s2.length()) return false; int[] freq = new int[26]; for (int i = 0; i < s1.length(); i++) { freq[s1.charAt(i) - 'a']++; freq[s2.charAt(i) - 'a']--; } for (int count : freq) { if (count != 0) return false; } return true; } /** * Pattern 2: Two-pointer reverse. O(n) time, O(n) space (char array). */ public static String reverseString(String s) { char[] chars = s.toCharArray(); int left = 0, right = chars.length - 1; while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } return new String(chars); } /** * Pattern 3: Sliding window — longest substring without repeating chars. */ public static int longestUniqueSubstring(String s) { Map<Character, Integer> lastSeen = new HashMap<>(); int maxLen = 0, windowStart = 0; for (int windowEnd = 0; windowEnd < s.length(); windowEnd++) { char ch = s.charAt(windowEnd); if (lastSeen.containsKey(ch)) { windowStart = Math.max(windowStart, lastSeen.get(ch)); } lastSeen.put(ch, windowEnd + 1); maxLen = Math.max(maxLen, windowEnd - windowStart + 1); } return maxLen; } /** * Pattern 4: Build output with StringBuilder, join at end. */ public static String reverseWords(String s) { String[] words = s.trim().split("\\s+"); StringBuilder sb = new StringBuilder(); for (int i = words.length - 1; i >= 0; i--) { sb.append(words[i]); if (i > 0) sb.append(' '); } return sb.toString(); } public static void main(String[] args) { System.out.println(areAnagrams("listen", "silent")); // true System.out.println(reverseString("hello")); // olleh System.out.println(longestUniqueSubstring("abcabcbb")); // 3 System.out.println(reverseWords(" the sky is blue ")); // "blue is sky the" } }
olleh
3
blue is sky the
- Contiguous substring → sliding window (fixed or variable size).
- Symmetry/comparison from ends → two pointers.
- Character counting/frequency → frequency map (int[26] or HashMap).
- Grouping by content → string hashing (sorted key or frequency key).
- Building output → StringBuilder or String.join, never + in a loop.
The Sliding Window Pattern — Scan Once, Answer Fast
package io.thecodeforge.algo; import java.util.HashMap; import java.util.Map; public class LongestUniqueSubstring { /** * Finds the length of the longest substring without repeating characters. * Classic variable-size sliding window problem. * * Time: O(n) — each character is visited at most twice (once by right, once by left) * Space: O(min(n, alphabet)) — the map holds at most one entry per unique character */ public static int findLongestUniqueSubstring(String input) { // Maps each character to the index AFTER its last known position. // Storing index+1 lets us jump the left pointer forward in one step. Map<Character, Integer> lastSeenAt = new HashMap<>(); int maxLength = 0; int windowStart = 0; // left edge of our sliding window for (int windowEnd = 0; windowEnd < input.length(); windowEnd++) { char currentChar = input.charAt(windowEnd); // If this character already exists inside our current window, // move the left edge just past its previous position so the // window no longer contains the duplicate. if (lastSeenAt.containsKey(currentChar)) { // Math.max prevents the window from moving BACKWARD // if the duplicate was outside the current window. windowStart = Math.max(windowStart, lastSeenAt.get(currentChar)); } // Record this character's "next safe position" for the left pointer lastSeenAt.put(currentChar, windowEnd + 1); // Current window length = windowEnd - windowStart + 1 maxLength = Math.max(maxLength, windowEnd - windowStart + 1); } return maxLength; } public static void main(String[] args) { System.out.println(findLongestUniqueSubstring("abcabcbb")); // 3 → "abc" System.out.println(findLongestUniqueSubstring("bbbbb")); // 1 → "b" System.out.println(findLongestUniqueSubstring("pwwkew")); // 3 → "wke" System.out.println(findLongestUniqueSubstring("")); // 0 → empty string System.out.println(findLongestUniqueSubstring("abcdefg")); // 7 → whole string } }
1
3
0
7
Frequency Maps — When You Care About Character Counts, Not Positions
A frequency map (also called a character count array or histogram) is a data structure that counts how many times each character appears. It transforms a string into a numerical fingerprint. Two strings with identical fingerprints are anagrams of each other. That's powerful.
For strings restricted to lowercase English letters, you can use an int[26] array instead of a HashMap. Array access is O(1) with no hashing overhead, and comparing two int[26] arrays takes exactly 26 comparisons — constant time regardless of string length. This is a significant practical speedup that interviewers love to hear you mention.
Frequency maps unlock the fixed-size sliding window approach for anagram problems. You precompute the frequency map of the pattern, then slide a same-length window across the text. Each slide, you add one character and remove one character from your running frequency map. When the running map matches the pattern map, you've found an anagram. One pass. O(n) time.
package io.thecodeforge.algo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class AnagramFinder { /** * Returns all starting indices where an anagram of 'pattern' begins in 'text'. * Uses a fixed-size sliding window with an int[26] frequency map. * * Time: O(n) where n = text.length() * Space: O(1) — the two int[26] arrays are constant size regardless of input */ public static List<Integer> findAnagramStartIndices(String text, String pattern) { List<Integer> resultIndices = new ArrayList<>(); // Edge case: pattern can't fit inside text if (pattern.length() > text.length()) { return resultIndices; } int windowSize = pattern.length(); // Build the frequency fingerprint for the target pattern int[] patternFrequency = new int[26]; for (char ch : pattern.toCharArray()) { patternFrequency[ch - 'a']++; // 'a'=0, 'b'=1, ..., 'z'=25 } // Build the frequency fingerprint for the first window in text int[] windowFrequency = new int[26]; for (int i = 0; i < windowSize; i++) { windowFrequency[text.charAt(i) - 'a']++; } // Check the first window before we start sliding if (Arrays.equals(patternFrequency, windowFrequency)) { resultIndices.add(0); } // Slide the window one character at a time for (int windowEnd = windowSize; windowEnd < text.length(); windowEnd++) { // Add the new character entering the right side of the window windowFrequency[text.charAt(windowEnd) - 'a']++; // Remove the character leaving the left side of the window int windowStart = windowEnd - windowSize; windowFrequency[text.charAt(windowStart) - 'a']--; // Arrays.equals on two int[26] arrays is 26 comparisons — O(1) if (Arrays.equals(patternFrequency, windowFrequency)) { resultIndices.add(windowStart + 1); // +1 because new window starts one ahead } } return resultIndices; } public static void main(String[] args) { System.out.println(findAnagramStartIndices("cbaebabacd", "abc")); // [0, 6] System.out.println(findAnagramStartIndices("abab", "ab")); // [0, 1, 2] System.out.println(findAnagramStartIndices("af", "be")); // [] } }
[0, 1, 2]
[]
- int[26]: O(1) access, O(1) comparison (26 comparisons), no autoboxing.
- HashMap: O(1) amortized access, O(k) comparison, autoboxing overhead.
- Use int[26] when input is lowercase English only.
- Use HashMap when input includes uppercase, digits, or Unicode.
- Benchmark: int[26] is 3-5x faster than HashMap for anagram detection on lowercase strings.
Two Pointers on Strings — The Palindrome and Reverse Toolkit
Two pointers on a string means placing one pointer at the start and one at the end, then marching them toward each other. It's perfect for problems involving symmetry (palindromes), reversals, or comparisons from both ends simultaneously.
The reason this pattern exists is that many string properties are inherently symmetric. A palindrome reads the same forwards and backwards. The minimum number of deletions to make a string a palindrome depends on mismatches at mirrored positions. Both of these are naturally expressed as comparisons between a left and right pointer moving inward.
Two pointers also pairs beautifully with other patterns. You can use two pointers on the result of a frequency map to reconstruct strings. You can use them inside a sliding window as the window boundaries themselves. Once you're comfortable with each pattern in isolation, start noticing when problems require you to combine two of them — that's where intermediate-level solutions start to look elegant rather than brute-force.
package io.thecodeforge.algo; public class PalindromeChecker { /** * Checks if a string is a valid palindrome, considering only * alphanumeric characters and ignoring case. * * Time: O(n) * Space: O(1) — no extra data structures, just two integer pointers */ public static boolean isValidPalindrome(String sentence) { int leftPointer = 0; int rightPointer = sentence.length() - 1; while (leftPointer < rightPointer) { // Skip non-alphanumeric characters from the left while (leftPointer < rightPointer && !Character.isLetterOrDigit(sentence.charAt(leftPointer))) { leftPointer++; } // Skip non-alphanumeric characters from the right while (leftPointer < rightPointer && !Character.isLetterOrDigit(sentence.charAt(rightPointer))) { rightPointer--; } // Compare characters at both pointers, case-insensitive char leftChar = Character.toLowerCase(sentence.charAt(leftPointer)); char rightChar = Character.toLowerCase(sentence.charAt(rightPointer)); if (leftChar != rightChar) { return false; // Mismatch — definitely not a palindrome } // Both characters matched — move both pointers inward leftPointer++; rightPointer--; } // All mirrored pairs matched return true; } /** * Finds the length of the longest palindromic substring using * the "expand around center" technique. * * Time: O(n²) — each of the 2n-1 centers expands up to n/2 times * Space: O(1) */ public static String longestPalindromicSubstring(String word) { if (word == null || word.isEmpty()) return ""; int bestStart = 0; int bestLength = 1; for (int centerIndex = 0; centerIndex < word.length(); centerIndex++) { int oddLength = expandFromCenter(word, centerIndex, centerIndex); int evenLength = expandFromCenter(word, centerIndex, centerIndex + 1); int longerExpansion = Math.max(oddLength, evenLength); if (longerExpansion > bestLength) { bestLength = longerExpansion; bestStart = centerIndex - (longerExpansion - 1) / 2; } } return word.substring(bestStart, bestStart + bestLength); } private static int expandFromCenter(String word, int left, int right) { while (left >= 0 && right < word.length() && word.charAt(left) == word.charAt(right)) { left--; right++; } return right - left - 1; } public static void main(String[] args) { System.out.println(isValidPalindrome("A man, a plan, a canal: Panama")); // true System.out.println(isValidPalindrome("race a car")); // false System.out.println(isValidPalindrome(" ")); // true System.out.println(longestPalindromicSubstring("babad")); // "bab" or "aba" System.out.println(longestPalindromicSubstring("cbbd")); // "bb" System.out.println(longestPalindromicSubstring("racecar")); // "racecar" } }
false
true
bab
bb
racecar
- Expand around center: simplest, O(1) space. Best for interviews.
- DP table: O(n²) space. Useful when you need to answer many palindrome queries on the same string.
- Manacher's: O(n) time. Complex to implement. Mention it exists, don't implement unless asked.
- Two-pointer palindrome check: O(n) time, O(1) space. Different from longest palindromic substring.
- For 'valid palindrome' check: two pointers. For 'longest palindromic substring': expand around center.
String Hashing — Catching Patterns You Can't See by Eye
Hashing a string means converting it into a number so you can compare strings in O(1) instead of O(n). The canonical application is the Rabin-Karp rolling hash algorithm for substring search, but the concept shows up any time you need to detect duplicate substrings, group anagrams, or find repeated patterns at scale.
The key idea is a rolling hash: when your window slides one character to the right, you don't recompute the entire hash from scratch. You mathematically remove the contribution of the outgoing character and add the incoming character. This keeps each slide at O(1), making the full scan O(n) regardless of pattern length.
Grouping anagrams is a softer but very common application. The trick: sort each word's characters to produce a canonical key. All anagrams of 'eat' sort to 'aet'. Store them in a HashMap<String, List<String>> keyed by the sorted form. This is O(n * k log k) where k is the average word length — entirely practical for real word lists and comes up frequently in interview problems involving dictionaries.
package io.thecodeforge.algo; import java.util.*; public class AnagramGrouper { /** * Groups a list of words so that anagrams appear together. * Uses a sorted-character string as a canonical hash key. * * Time: O(n * k log k) where n = number of words, k = average word length * Space: O(n * k) to store all words in the result map */ public static List<List<String>> groupAnagrams(String[] words) { Map<String, List<String>> anagramBuckets = new HashMap<>(); for (String word : words) { char[] wordChars = word.toCharArray(); Arrays.sort(wordChars); String canonicalKey = new String(wordChars); anagramBuckets.computeIfAbsent(canonicalKey, k -> new ArrayList<>()).add(word); } return new ArrayList<>(anagramBuckets.values()); } /** * Alternative fingerprint approach using a frequency-count key. * Avoids sorting entirely — O(n * k) overall. */ public static List<List<String>> groupAnagramsLinear(String[] words) { Map<String, List<String>> anagramBuckets = new HashMap<>(); for (String word : words) { int[] charCounts = new int[26]; for (char ch : word.toCharArray()) { charCounts[ch - 'a']++; } StringBuilder keyBuilder = new StringBuilder(); for (int count : charCounts) { keyBuilder.append('#').append(count); } String frequencyKey = keyBuilder.toString(); anagramBuckets.computeIfAbsent(frequencyKey, k -> new ArrayList<>()).add(word); } return new ArrayList<>(anagramBuckets.values()); } public static void main(String[] args) { String[] wordList = {"eat", "tea", "tan", "ate", "nat", "bat"}; List<List<String>> grouped = groupAnagrams(wordList); System.out.println("Sorted-key approach:"); for (List<String> group : grouped) { System.out.println(" " + group); } System.out.println("\nFrequency-key approach:"); List<List<String>> groupedLinear = groupAnagramsLinear(wordList); for (List<String> group : groupedLinear) { System.out.println(" " + group); } } }
[eat, tea, ate]
[tan, nat]
[bat]
Frequency-key approach:
[eat, tea, ate]
[tan, nat]
[bat]
- Sorted key: simple, O(k log k) per word. Best for short words (k < 50).
- Frequency key: O(k) per word. Best for long words (k > 100).
- Frequency key uses int[26] → string conversion: "#2#0#1#..." as the hash key.
- Both produce the same grouping result. The difference is per-word key-building cost.
- Decision rule: if k is unknown, use sorted key for simplicity. If k can be large, use frequency key.
StringBuilder and String Concatenation — The Hidden O(n^2)
String concatenation with + inside a loop is the most common performance anti-pattern in string-heavy code. In Java, Strings are immutable. Each + creates a new String object, copying all previous characters. For a loop of n iterations building a string of final length L, total work is O(L^2) — not O(L).
StringBuilder solves this by maintaining a mutable char[] buffer. Appends are O(1) amortized (occasional resize is O(L) but amortized over n appends). The final toString() call allocates one String of length L. Total work: O(L).
The Java compiler can optimize simple string + into StringBuilder for cases like String s = a + b + c. But it fails for complex patterns like result += match + ", " inside a loop. Never rely on the compiler — use StringBuilder explicitly for any string assembly inside a loop.
package io.thecodeforge.algo; public class StringBuilderBenchmark { /** Demonstrates the O(n^2) cost of string concatenation vs O(n) StringBuilder. */ public static void main(String[] args) { int n = 100_000; // BAD: O(n^2) — each + copies all previous characters long start = System.nanoTime(); String bad = ""; for (int i = 0; i < n; i++) { bad += "a"; // new String object each iteration } long badTime = System.nanoTime() - start; // GOOD: O(n) — StringBuilder appends in-place start = System.nanoTime(); StringBuilder sb = new StringBuilder(n); // pre-sized for (int i = 0; i < n; i++) { sb.append('a'); } String good = sb.toString(); long goodTime = System.nanoTime() - start; System.out.println("String +000) StringBuilder: 1 ms (length=100000) Speedup: 84 loop: " + badTime / 1_000_000 + " ms (length=" + bad.length() + ")"); System.out.println("StringBuilder: " + goodTime / 1_000_000 + " ms (length=" + good.length() + ")"); System.out.println("Speedup: " + (badTime / Math.max(goodTime, 1)) + "x"); } }
| Pattern | Best For | Time Complexity | Space Complexity | Key Signal in Problem |
|---|---|---|---|---|
| Sliding Window (variable) | Longest/shortest substring with constraint | O(n) | O(alphabet size) | Problem says 'longest/shortest/minimum window' |
| Sliding Window (fixed) | Anagram detection, fixed-length pattern match | O(n) | O(1) with int[26] | Problem gives a fixed pattern length |
| Two Pointers (inward) | Palindrome check, symmetry comparison | O(n) | O(1) | Problem involves mirroring or reading both ends |
| Frequency Map | Character count comparison, anagram grouping | O(n) | O(alphabet size) | Problem asks 'same characters?' or 'rearrangement?' |
| String Hashing / Sorted Key | Grouping anagrams, duplicate substring detection | O(n * k log k) | O(n * k) | Problem asks to group or deduplicate by content |
| StringBuilder | Building output strings from parts | O(n) | O(n) | Problem requires assembling a string from components |
| Expand Around Center | Longest palindromic substring | O(n^2) | O(1) | Problem asks for longest palindrome (not just check) |
| Rabin-Karp Rolling Hash | Substring search, duplicate detection at scale | O(n + m) avg | O(1) | Problem requires finding pattern occurrences in large text |
🎯 Key Takeaways
- Pattern recognition before coding: identify whether the problem needs a fixed or variable window, symmetric comparison, or frequency fingerprint before touching the keyboard — the right pattern choice eliminates 80% of implementation bugs.
- The sliding window left pointer must only ever move forward — the missing Math.max guard is the single most common sliding window bug in interview settings and production code alike.
- int[26] is strictly better than HashMap for lowercase-only string problems: no autoboxing, no collision handling, constant-time comparison via Arrays.equals — always state this trade-off in an interview.
- Sorting characters is the easiest anagram key but costs O(k log k) per word — the frequency-count key approach cuts it to O(k) and matters significantly when processing long strings at scale.
- String + in a loop is O(n^2). StringBuilder is O(n). This is the single most impactful one-line fix in string-heavy code. Pre-size with new StringBuilder(estimatedLength).
- Expand-around-center beats DP for longest palindromic substring: same O(n^2) time but O(1) space instead of O(n^2) space.
- Always test sliding window with 'abba' and 'tmmzuxt' to catch the Math.max bug. Always test anagram with different-length strings.
- The five patterns — sliding window, two pointers, frequency map, string hashing, StringBuilder — cover 95% of string manipulation problems in interviews and production.
⚠ Common Mistakes to Avoid
Interview Questions on This Topic
- QGiven a string s and a pattern p, find the minimum window substring of s that contains all characters of p, including duplicates. Walk me through your approach before writing any code.
- QHow would you determine if two strings are anagrams of each other in O(n) time and O(1) space? What constraint on the input makes O(1) space possible, and when does that assumption break?
- QYou've solved 'longest substring without repeating characters' using a HashMap. Your interviewer asks you to do it with O(1) space. Is that possible? What trade-off would you make, and what constraint on the input would you need?
- QHow would you group a list of 100,000 words by anagram families? Compare the sorted-key approach (O(k log k) per word) with the frequency-key approach (O(k) per word). When does each approach win?
- QExplain the expand-around-center technique for finding the longest palindromic substring. Why is it preferred over dynamic programming for this problem? What is the space complexity difference?
- QHow would you implement Rabin-Karp rolling hash for substring search? What is the role of the modulo operation, and what happens if you skip it?
- QYour autocomplete service builds suggestion strings by concatenating matches with + in a loop. It crashes with OOM every 3 hours. What is the root cause and how do you fix it?
- QHow would you check if a string is a valid palindrome, considering only alphanumeric characters and ignoring case? What edge cases would you test?
- QDescribe a production scenario where string manipulation patterns are critical. What pattern did you use, what edge cases did you encounter, and what was the performance impact?
- QHow would you find all anagram occurrences of a pattern in a text using a fixed-size sliding window? What is the time and space complexity?
Frequently Asked Questions
What is the sliding window technique in string problems?
The sliding window technique maintains a contiguous substring defined by two pointers (left and right). Instead of restarting from scratch for each position, you slide the window forward by adding one character on the right and optionally removing one on the left, updating your state incrementally. This reduces most O(n^2) substring problems to O(n).
How do I check if two strings are anagrams in Java?
The fastest O(n) approach for lowercase-only strings is to build an int[26] frequency array for each string — increment for the first string, decrement for the second — then check that all 26 values are zero. If any entry is non-zero, the strings aren't anagrams. Alternatively, sort both strings and compare with equals(), but that's O(n log n).
When should I use two pointers vs sliding window for string problems?
Use two pointers moving inward from both ends when the problem involves symmetry, palindromes, or comparing characters from opposite sides of the string. Use a sliding window when the problem involves a contiguous segment (substring) and asks you to find the longest, shortest, or all such segments satisfying a constraint.
What is the fastest anagram check?
int[26] frequency comparison is O(n) time and O(1) space for lowercase English strings. Increment for string A, decrement for string B, check all 26 values are zero. Sorting both and comparing is O(n log n). int[26] is strictly faster.
How do you check if two strings are anagrams in O(n) time?
Count character frequencies using a fixed-size array (for lowercase ASCII, size 26) or a HashMap. Increment counts for string A, decrement for string B. If all counts are zero at the end, the strings are anagrams. This avoids sorting and runs in O(n) time, O(1) space for fixed character sets.
Why is String concatenation with + slow in loops?
Java Strings are immutable. Each + creates a new String object, copying all previous characters. For n iterations, total work is 1 + 2 + 3 + ... + n = n(n+1)/2 = O(n^2). StringBuilder maintains a mutable buffer and appends in-place: O(n) total. Always use StringBuilder for string assembly inside loops.
How does the Math.max guard work in sliding window?
When a duplicate character is found, lastSeenAt.get(ch) gives the index after its previous occurrence. Without Math.max, this index could be less than the current windowStart (if the duplicate was before the current window), causing the left pointer to jump backward. Math.max(windowStart, lastSeenAt.get(ch)) ensures the left pointer only moves forward, preserving the window invariant.
What is the difference between expand-around-center and DP for longest palindromic substring?
Both are O(n^2) time. Expand-around-center uses O(1) space by expanding from each possible center. DP uses O(n^2) space for a table storing whether each substring is a palindrome. Expand-around-center is preferred unless you need to answer many palindrome queries on the same string (in which case the DP table amortizes its cost).
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.