๐ WAYPOINT LESSON
Recursive strings and shapes
โญโญโญ advancedโณ 10 min read๐ Lesson 199 of 256
Reverse append order, two-index palindromes, Fibonacci-shaped counting, and Euclid's progress-by-modulo.
Recursive strings and self-similar problems. The classic recursion-into-reasoning problems:
- Reverse:
rev(s) = rev(s.substring(1)) + s.charAt(0)with baselength() <= 1. The append order is the entire difficulty โ swapping it produces the input unchanged. - Palindrome check with two indexes:
isPal(s, lo, hi)โ compare ends, recurse inward. Base:lo >= hi. - Count structure: "how many ways" problems (climb 1-or-2
stairs) map to Fibonacci:
ways(n) = ways(n-1) + ways(n-2), basesways(0) = ways(1) = 1. Recognize the shape; do not simulate all paths. - GCD: Euclid's
gcd(a, b) = gcd(b, a % b), baseb == 0. The exam loves it because it is progress-by-modulo.
Trace discipline: for strings, annotate each level with the substring it receives โ level widths shrink visibly, which makes the base case obvious and wrong-index bugs visible.
โก Now practice
Ready to CodeRecursion gauntletFive recursive problems from tracing to synthesis; bases first, progress always.
5 challenges ยท ยท ~60 min