Command Palette

Search for a command to run...

Module 3

Strings

Characters as numbers, building strings efficiently, counting letters with an array of 26, and finding palindromes.

Beginner 4 lessons 5 problems ~45 min of lessons

A string is an array of characters with a few extra rules: it can't be changed in place in Java, characters are small numbers underneath, and many problems care only about letters and digits.

This module gives you the string toolkit, then uses it with two pointers (palindromes, reversing words), running state (common prefix), read/write pointers (compression) and expansion around a centre (longest palindrome).

Best after: Arrays

Part 1

Learn the ideas

Part 2

Solve the problems

In order of difficulty. Each one shows the pattern it teaches.

  1. Two pointers that skip characters you don't care about, without building a cleaned copy.

  2. Reversing at two levels (the whole string, then each word), plus cleaning up extra spaces.

  3. Keep a running answer (the prefix so far) and shrink it as each new word disagrees.

  4. A read pointer finds each run while a write pointer writes the compressed form into the same array.

  5. Every palindrome grows from a centre: try all 2n − 1 centres and expand, instead of checking all O(n²) substrings.