InferenceAI

Enter at least two characters to search.

Start free

Palindrome Partitions

mediumRecursion & Backtracking25 min25 XP

Return every way to split s into substrings that are all palindromes, in lexicographic order of the lists.

Examples

EXAMPLE WALKTHROUGHtwo ways

A guided view of the published example, not a trace of your code.

INPUT["aab"]
TASKpalindrome_partitions()
EXPECTED OUTPUT[["a","a","b"],["aa","b"]]

Start with the input values shown in this example.

Example 1 of 3

Input: s = "aab"

Output: [["a","a","b"],["aa","b"]]

Explanation: two ways

Input: s = "a"

Output: [["a"]]

Explanation: single letter

Input: s = "abba"

Output: [["a","b","b","a"],["a","bb","a"],["abba"]]

Explanation: even palindrome

Hint 1

At each position, try every palindromic prefix and recurse on the rest.

Topics

DSARecursion & BacktrackingPython