131. Palindrome Partitioning

Description

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

A palindrome string is a string that reads the same backward as forward.

Example 1:

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

Example 2:

Input: s = "a"
Output: [["a"]]

Constraints:

  • 1 <= s.length <= 16

  • s contains only lowercase English letters.

Tags

Backtracking

Solution

Use the DFS template. Every time we examine the input string from the very first character to the whole string (Note: the index for traversal is from 1 to n, including n) if it is a palindrome. If it is, we append this substring to path , then DFS the remaining string. The boundary contiditon is the input string is empty, and we append path to the result 2D string slice.

Complexity

  • Time complexity: O(n×2n)O(n\times2^n), the worst case happens when the input string is the same character repeated n times. Thus it has 2n2^n possible partitioning of s.

  • Space complexity: O(n2)O(n^2), needs O(n)O(n) for path to store temporary result and stack space O(n)O(n) for recursion.

Code

Last updated

Was this helpful?