Skip to content

Commit 2a2ed98

Browse files
committed
131. Palindrome Partitioning
1 parent b788a5b commit 2a2ed98

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/// The average complexity of this algorithm will be `O(n * 2^n)`,
2+
/// where `n` is the length of the string. This is because we spend
3+
/// `O(n^2)` time to fill the `dp` array and `O(n * 2^n)` time to iterate
4+
/// over all partitions. So the total complexity would be `O(n^2 + n * 2^n)`.
5+
/// But here we can neglect `O(n^2)` because it is of lower priority.
6+
/// So the total complexity will be `O(n * 2^n)`.
7+
/// The space complexity will be `O(n^2)`, as we store all partitions
8+
/// in a list of lists (`dp`).
9+
10+
class Solution {
11+
List<List<String>> partition(String s) {
12+
// Create an empty list for the result
13+
List<List<String>> result = [];
14+
// Create a two-dimensional array for storing boolean values of palindromes
15+
int n = s.length;
16+
List<List<bool>> dp = List.generate(n, (_) => List.filled(n, false));
17+
// Fill the dp array in O(n^2) time
18+
fillDP(s, dp);
19+
// Create a stack for storing the current partition and index
20+
List<Pair> stack = [];
21+
// Add an empty list and zero index to the stack
22+
stack.add(Pair([], 0));
23+
// While the stack is not empty, continue the loop
24+
while (stack.isNotEmpty) {
25+
// Pop the last element from the stack
26+
Pair top = stack.removeLast();
27+
// Get the current list and index from the element
28+
List<String> current = top.list;
29+
int start = top.index;
30+
// If the index is equal to the length of the string, add the current list to the result
31+
if (start == n) {
32+
result.add(List.from(current));
33+
} else {
34+
// Otherwise, iterate over all substrings from the index to the end of the string
35+
for (int i = start; i < n; i++) {
36+
// If the substring is a palindrome according to the dp array, add it to a copy of the current list
37+
// and add the copy of the list and the next index to the stack
38+
if (dp[start][i]) {
39+
List<String> copy = List.from(current);
40+
copy.add(s.substring(start, i + 1));
41+
stack.add(Pair(copy, i + 1));
42+
}
43+
}
44+
}
45+
}
46+
47+
return result;
48+
}
49+
50+
void fillDP(String s, List<List<bool>> dp) {
51+
int n = s.length;
52+
// Iterate over all substring lengths from 1 to n
53+
for (int len = 1; len <= n; len++) {
54+
// Iterate over all starting indices of substrings
55+
for (int i = 0; i <= n - len; i++) {
56+
// Compute the ending index of the substring
57+
int j = i + len - 1;
58+
// Apply the rule for filling dp[i][j]
59+
if (s[i] == s[j] && (j - i < 2 || dp[i + 1][j - 1])) {
60+
dp[i][j] = true;
61+
} else {
62+
dp[i][j] = false;
63+
}
64+
}
65+
}
66+
}
67+
}
68+
69+
class Pair {
70+
List<String> list;
71+
int index;
72+
Pair(this.list, this.index);
73+
}
74+
75+
void main(List<String> args) {
76+
print(Solution().partition("aab")); // [["a","a","b"],["aa","b"]]
77+
print(Solution().partition("a")); // [["a"]]
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
131. Palindrome Partitioning
2+
https://leetcode.com/problems/palindrome-partitioning/
3+
4+
Given a string s, partition s such that every
5+
substring of the partition is a palindrome.
6+
Return all possible palindrome partitioning of s.
7+
8+
9+
Example 1:
10+
11+
Input: s = "aab"
12+
Output: [["a","a","b"],["aa","b"]]
13+
14+
Example 2:
15+
16+
Input: s = "a"
17+
Output: [["a"]]
18+
19+
20+
Constraints:
21+
22+
1 <= s.length <= 16
23+
s contains only lowercase English letters.

0 commit comments

Comments
 (0)