Skip to content
BASA DSA

Palindrome Partitioning

MediumBacktracking

Return all ways to split s into contiguous nonempty palindromes. Preserve substring order within each partition; result order is unrestricted.

Example 1

Input
s = "aab"
Output
[["a","a","b"],["aa","b"]]

Explanation

aab splits as a/a/b or aa/b; the entire string is not palindromic.

Example 2

Input
s = "aba"
Output
[["a","b","a"],["aba"]]

Explanation

aba stays whole or splits into single letters; ab and ba are invalid pieces.

Example 3

Input
s = "x"
Output
[["x"]]

Explanation

A one-letter string has one partition.

Constraints

  • 1 <= len(s) <= 16
  • s contains lowercase English letters

Function contractpartitionArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…