Skip to contentKEEP THE CRAFTTHE SANCTUARY

Palindrome Partitioning

Cut a string in every way that leaves only palindromes.

The problem

Implement Solution.partition(text). Return every way to cut text into consecutive pieces so that every piece is a palindrome, meaning it reads the same forwards and backwards. Each way is a list of pieces in their original order, and together the pieces must spell text. For example, "aab" can be cut as ["a", "a", "b"] or ["aa", "b"]. Return the ways in any order. Constraints: text has 1 to 16 lowercase English letters.

Starter code

class Solution:
    def partition(self, text):
        pass

Public examples

  1. text = "aab"gives[["a", "a", "b"], ["aa", "b"]]
  2. text = "aa"gives[["a", "a"], ["aa"]]
  3. text = "abc"gives[["a", "b", "c"]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.