LeetCode 0131. Palindrome Partitioning Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0131. 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.

 

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.

Solutions

Solution 1: Preprocessing + DFS (Backtracking)

We can use dynamic programming to preprocess whether any substring in the string is a palindrome, i.e., f[i][j] indicates whether the substring s[i..j] is a palindrome.

Next, we design a function dfs(i), which represents starting from the i-th character of the string and partitioning it into several palindromic substrings, with the current partition scheme being t.

If i = |s|, it means the partitioning is complete, and we add t to the answer array and then return.

Otherwise, we can start from i and enumerate the end position j from small to large. If s[i..j] is a palindrome, we add s[i..j] to t, then continue to recursively call dfs(j+1). When backtracking, we need to pop s[i..j].

The time complexity is O(n × 2^n), and the space complexity is O(n^2). Here, n is the length of the string.

PythonJavaC++GoTypeScriptC#
class Solution: def partition(self, s: str) -> List[List[str]]: def dfs(i: int): if i == n: ans.append(t[:]) return for j in range(i, n): if f[i][j]: t.append(s[i : j + 1]) dfs(j + 1) t.pop() n = len(s) f = [[True] * n for _ in range(n)] for i in range(n - 1, -1, -1): for j in range(i + 1, n): f[i][j] = s[i] == s[j] and f[i + 1][j - 1] ans = [] t = [] dfs(0) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !