Codele / Data structures
Strings inside out
Text looks like one piece, but underneath it is a row of letters, each with a position. That lets us turn a word around and test whether it reads the same both ways.
- Text is a row of letters, and each letter has a position that starts at 0.
- In some languages text cannot be changed in place, so we copy the letters into a list first.
- Two fingers, one at each end, step inward and swap the letters they point at until they meet.
- A palindrome reads the same both ways: turn it around and it equals the original.
- To swap two things you need a spare box (temp) to hold one while you move the other.
Loading the playable lesson…