Backtracking
Palindrome Partitioning
medium
Count: 10
DESCRIPTION (inspired by Leetcode.com)
Given a string s, split it into segments where each segment is a palindrome (reads the same forwards and backwards).
Return all possible ways to partition the string into palindromic segments.
Constraints:
- 1 <= s.length <= 16
- s contains only lowercase English letters
Example 1:
Input:
s = "noon"
Output:
[["n","o","o","n"], ["n","oo","n"], ["noon"]]
Explanation:
- ["n","o","o","n"] - All single characters are palindromes
- ["n","oo","n"] - "oo" is a palindrome in the middle
- ["noon"] - The entire word is a palindrome
Example 2:
Input:
s = "civic"
Output:
[["c","i","v","i","c"], ["c","ivi","c"], ["civic"]]
Example 3:
Input:
s = "aab"
Output:
[["a","a","b"], ["aa","b"]]
The input itself does not need to be a palindrome.
This page is Premium and only partly public. The statement and the outline are here; the walkthrough prose and solution code stay behind the login on the original.
Open the original page →
Open the original page →
This page is Premium and only partly public. The statement and the outline are here; the walkthrough prose and solution code stay behind the login on the original.
Open the original page →
Open the original page →
Building Intuition
Finding All Partitions
The Solution-Space Tree
The Algorithm
Checking for Palindromes
Step-by-Step Walkthrough
Solution
Optimization: Precompute Palindromes
Purchase Premium to Keep Reading
Unlock this article and so much more with Hello Interview Premium