Stop 21 · Data Structures & AlgorithmsTopics · 2/17

Strings

Part of the Backend Developer Roadmap

In short

Anagrams, palindromes, longest substring: string questions are everywhere. Most are array tricks in disguise, plus one good habit: counting characters.

Text-processing problems — often array problems in disguise, but with their own common patterns around characters, substrings and comparisons.

The purple blob threads colourful beads one by one onto a long string.

Think of it like this

Beads on a garland

A string is beads on a thread, one character per bead, in a fixed order. You can look at any bead, but you can't swap one out without making a new garland. That's why strings are called immutable.

Watch it happen

Is "racecar" a palindrome?

r
0
L
a
1
c
2
e
3
c
4
a
5
r
6
R

1/4Put one pointer at each end. Both are "r", so far so good.

The big ideas

What you need to know

Strings are immutable

s[0] = "x" never changes a string. Methods like replace or toUpperCase return a brand new string instead.

Count the characters

Anagram, "first unique character" and many more are solved by counting how often each character appears with a Map or a 26-slot array.

Two pointers work here too

Palindromes and reversing are classic two-pointer problems, one from each end, moving inwards.

Slicing has a cost

s.slice(i, j) copies characters. Slicing inside a loop adds hidden work, so pass indexes around when you can.

See it in code

Walk through the code

ts
1function isAnagram(a: string, b: string): boolean {2  if (a.length !== b.length) return false;3 4  const count = new Map<string, number>();5  for (const ch of a) count.set(ch, (count.get(ch) ?? 0) + 1);6 7  for (const ch of b) {8    const left = (count.get(ch) ?? 0) - 1;9    if (left < 0) return false;   // b has a character a didn't10    count.set(ch, left);11  }12  return true;13}14 15console.log(isAnagram("listen", "silent")); // true

1/3Different lengths can never be anagrams. Cheap early exit.

Counting is O(n). Sorting both strings and comparing also works, but costs O(n log n).

Save yourself the pain

Common mistakes

Oops

Trying to change a character with s[i] = "x".

Do this instead

Convert to an array with [...s], change it, then join("") it back into a string.

Oops

Forgetting about case and spaces in palindrome questions.

Do this instead

Ask the interviewer first, then normalise with toLowerCase() and remove non-letters if needed.

Your mission

Three string classics

~35 min

Quick check

You run const s = "chai"; s.toUpperCase(); console.log(s). What prints?

Go deeper

  1. 1Solve classic string problems: palindrome check, anagram check, longest substring.
  2. 2Learn common patterns: sliding window, character frequency maps.
  3. 3Practice both the brute-force and optimized solution for a few problems.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge