DSA · Patterns, not puzzles

What patterns help with string manipulation problems?

Mediumstringsfrequency-mappatternsUpdated Oct 2026
Quiz this track

Short answer

Most string problems reduce to frequency counting, two pointers, sliding windows, or prefix structures. Choose based on whether you need order, a contiguous range, or fast prefix queries.

Strings are arrays of characters with extra language rules. Before writing code, classify the problem: are you comparing multisets, finding a contiguous substring, matching a pattern, or answering repeated prefix queries? Each classification points to a different technique.

The common techniques

  • Frequency map for anagrams, character counts and duplicates
  • Two pointers for palindromes and reversing in place
  • Sliding window for the longest or shortest valid substring
  • Trie for prefix search across many words
  • Dynamic programming for edit distance and subsequences
js
function isAnagram(a, b) {
  if (a.length !== b.length) return false;
  const counts = new Map();
  for (const ch of a) counts.set(ch, (counts.get(ch) || 0) + 1);
  for (const ch of b) {
    if (!counts.has(ch)) return false;
    const next = counts.get(ch) - 1;
    if (next === 0) counts.delete(ch);
    else counts.set(ch, next);
  }
  return counts.size === 0;
}
Counting characters avoids sorting and runs in O(n) time with O(k) space for the alphabet.

Be explicit about Unicode. JavaScript string indexing works in UTF-16 code units, so characters outside the basic plane can occupy two code units. For interview answers, mention the assumption and use Array.from when code points matter.

Concatenating in a loop can be O(n^2) in some languages. In JavaScript engines often optimize it, but building an array and joining is a safer habit for large outputs.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How would you check whether two strings are anagrams?
  2. 2What changes if the alphabet is fixed lowercase English?
  3. 3How do you handle Unicode characters correctly?
  4. 4When is sorting a string an acceptable approach?

Say it in the interview

I start by classifying the problem, because order, contiguity and prefix queries point to different techniques. If order does not matter I count characters with a hash map, for a contiguous substring I use a sliding window, and for palindromes or prefix queries I use two pointers or a trie. I also clarify the character set and whether Unicode code points matter, because JavaScript indexes UTF-16 code units.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live DSA mock interview and get scored feedback on depth, clarity and confidence.