#131

Palindrome Partitioning

Medium Backtracking on cut positions

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Split a string into pieces so that every piece is a palindrome. Return all such splits.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: every way to cut a string into palindrome pieces.

Why this works: choose where the first piece ends (it must be a palindrome), then solve the rest the same way.

Example: "aab"

"a" + "a" + "b""aa" + "b"

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Calling substring before checking the palindrome — check with indexes first to avoid making useless strings.

Solve on LeetCode ↗
Complexity
  • Time: O(n · 2^n).
  • Space: O(n).
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon