MediumBacktracking
Palindrome Partitioning
amazongooglemetabloombergmicrosoftuber
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitionings of s.
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Explanation: Both ["a","a","b"] and ["aa","b"] are valid partitions where every part is a palindrome.
Example 2:
Input: s = "a"
Output: [["a"]]
Explanation: A single character is always a palindrome.
Examples
Example 1
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Explanation: "a", "a", "b" are all palindromes. "aa" and "b" are also both palindromes.
Example 2
Input: s = "a"
Output: [["a"]]
Explanation: A single character string has only one partition, and it is a palindrome.
Example 3
Input: s = "aba"
Output: [["a","b","a"],["aba"]]
Explanation: Both individual characters and the full string "aba" are palindromes.
Constraints
- -1 <= s.length <= 16
- -s contains only lowercase English letters
Optimal Complexity
Time
O(n * 2^n)
Space
O(n)
One problem, two ways to prep
Choose between solo practice and interview simulation
Practice Mode keeps things simple with code + tests. AI Interview Mode adds voice, pressure, and a post-round score summary.