Skip to content

level 1 · Foundations

Strings

A string is an array of characters. Everything else follows from that.

8 min 55 XP

what is it

Start here

There is no such thing as a string, really. There's an array, and the boxes happen to hold characters instead of numbers. s[0] is the first character exactly the way a[0] is the first number.

Which means every single thing you already know transfers, untouched. Index it. Walk it. Put two pointers on it and squeeze inward — that's precisely what a palindrome check is, and it's the same algorithm from the Two Pointers lesson, running on letters.

One thing does bite, though: in many languages (Java, Python, JavaScript, C#) strings are **immutable**. You cannot change a character in place. Every 'edit' quietly builds a whole new string. Do that inside a loop and you've written accidental O(n²) code that looks completely innocent.

That's why the fix — collect the pieces in a list and join once at the end — is one of the most common performance corrections in real code.

real-life analogy

Picture it

A word spelled out on fridge magnets

The word isn't a single object — it's a row of separate letter magnets. You can point at the third one, swap two of them, or read them from both ends inward. It was always an array; the word is just what you call it when you stand back.

interactive visualization

Watch it run

Pick a word: 1 = racecar, 2 = level, 3 = algorithm, 4 = rotator, 5 = banana.

  1. l
    0
  2. e
    1
  3. v
    2
  4. e
    3
  5. l
    4

"level". A string is nothing but an array of characters — s[0] is 'l', exactly like a[0] would be a number. Every array skill you have works here unchanged.

step 01/5

  • comparing
  • in final place
  • found it
1// A string is an array of characters. That's the whole secret.
2function isPalindrome(s) {
3 let lo = 0;
4 let hi = s.length - 1;
5
6 while (lo < hi) {
7 if (s[lo] !== s[hi]) return false; // mismatch — done
8 lo++;
9 hi--;
10 }
11
12 return true; // the pointers met without ever disagreeing
13}

variables right now

word
level
length
5
comparisons 0moves 0

the dry run · every step, in words

5 steps

complexity

What it costs

best case
O(1) index
average
O(n) to scan
worst case
O(n) to scan
extra memory
O(1)

Indexing a character is O(1), exactly like an array. Scanning is O(n). The trap is concatenation: building a string by += inside a loop copies the whole string each time — that's a hidden O(n²).

  • O(1)
  • O(log n)
  • O(n) · this one
  • O(n log n)
  • O(n²)
input size →work →

common mistakes

Common traps

  • Building a string with += inside a loop.

    Strings are immutable in most languages, so each += copies everything. That's O(n²). Push the pieces into an array and join() once at the end.

  • Comparing strings with == in Java.

    That compares references, not contents — so two identical strings can come back 'not equal'. Use .equals(). It has caught out everyone at least once.

  • Assuming one character is one byte.

    Emoji and many scripts take more than one code unit. 'length' can disagree with what a human would count. It only matters until it suddenly really matters.

quiz

Check yourself

Three questions. Get them all right to finish the lesson.

+55 XP

01What is a string, underneath?

02Why is building a string with += in a loop slow?

03How do you check if a string is a palindrome?

practice

Solve it on LeetCode

You've seen it run — now write it yourself. These are real LeetCode problems that use exactly this idea, from gentlest to toughest.