|
1 | 1 | // https://leetcode.com/problems/palindrome-partitioning
|
2 | 2 | // T: O(n * 2^n)
|
3 |
| -// S: O(n * 2^n) |
| 3 | +// S: O(n) for recursion call stack |
4 | 4 |
|
5 | 5 | import java.util.ArrayList;
|
6 | 6 | import java.util.LinkedList;
|
7 | 7 | import java.util.List;
|
8 | 8 |
|
9 | 9 | public class PalindromePartitioning {
|
10 |
| - private record Range(int start, int end) { } |
11 |
| - |
12 |
| - public static List<List<String>> partition(String s) { |
13 |
| - final List<List<String>> palindromicPartitions = new ArrayList<>(); |
14 |
| - addValidPartitionsToResult(s, palindromicPartitions); |
15 |
| - return palindromicPartitions; |
16 |
| - } |
17 |
| - |
18 |
| - private static void addValidPartitionsToResult(String s, List<List<String>> result) { |
19 |
| - if (isPalindrome(s)) result.add(List.of(s)); |
20 |
| - for (int i = 1 ; i < s.length() ; i++) { |
21 |
| - LinkedList<Range> segments = new LinkedList<>(); |
22 |
| - segments.add(new Range(0, i)); |
23 |
| - segments.add(new Range(i, s.length())); |
24 |
| - addValidPartitionsToResult(s, result, segments); |
25 |
| - } |
26 |
| - } |
27 |
| - |
28 |
| - private static void addValidPartitionsToResult(String s, List<List<String>> result, LinkedList<Range> segments) { |
29 |
| - if (!isPalindrome(s, getSecondLast(segments))) return; |
30 |
| - final Range lastSegment = lastSegment(segments); |
31 |
| - if (isPalindrome(s, lastSegment)) { |
32 |
| - result.add(getPartitions(s, segments)); |
33 |
| - } |
34 |
| - if (lastSegment.start + 1 < s.length()) { |
35 |
| - segments.pollLast(); |
36 |
| - } |
37 |
| - for (int i = lastSegment.start + 1 ; i < s.length() ; i++) { |
38 |
| - segments.addLast(new Range(lastSegment.start, i)); |
39 |
| - segments.addLast(new Range(i, lastSegment.end)); |
40 |
| - addValidPartitionsToResult(s, result, segments); |
41 |
| - segments.pollLast(); |
42 |
| - segments.pollLast(); |
43 |
| - } |
44 |
| - if (lastSegment.start + 1 < s.length()) { |
45 |
| - segments.add(lastSegment); |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 |
| - private static List<String> getPartitions(String s, List<Range> segments) { |
50 |
| - final List<String> result = new LinkedList<>(); |
51 |
| - for (Range range : segments) { |
52 |
| - result.add(s.substring(range.start, range.end)); |
53 |
| - } |
| 10 | + public List<List<String>> partition(String s) { |
| 11 | + final int length = s.length(); |
| 12 | + final boolean[][] dp = new boolean[length][length]; |
| 13 | + final List<List<String>> result = new ArrayList<>(); |
| 14 | + addValidPartitions(s, 0, dp, result, new LinkedList<>()); |
54 | 15 | return result;
|
55 | 16 | }
|
56 | 17 |
|
57 |
| - private static Range getSecondLast(List<Range> segments) { |
58 |
| - return segments.get(segments.size() - 2); |
59 |
| - } |
60 |
| - |
61 |
| - private static Range lastSegment(List<Range> segments) { |
62 |
| - return segments.get(segments.size() - 1); |
63 |
| - } |
64 |
| - |
65 |
| - public static boolean isPalindrome(String s) { |
66 |
| - return isPalindrome(s, 0, s.length()); |
67 |
| - } |
68 |
| - |
69 |
| - private static boolean isPalindrome(String s, Range range) { |
70 |
| - return isPalindrome(s, range.start, range.end); |
71 |
| - } |
| 18 | + private void addValidPartitions(String s, int start, boolean[][] dp, List<List<String>> result, LinkedList<String> current) { |
| 19 | + if (start == s.length()) result.add(new ArrayList<>(current)); |
72 | 20 |
|
73 |
| - public static boolean isPalindrome(String s, int start, int end) { |
74 |
| - for (int i = start ; i < start + (end - start) / 2 ; i++) { |
75 |
| - if (s.charAt(i) != s.charAt(end - i + start - 1)) return false; |
| 21 | + for (int end = start ; end < s.length() ; end++) { |
| 22 | + if (s.charAt(start) == s.charAt(end) && (end - start <= 2 || dp[start + 1][end - 1])) { |
| 23 | + dp[start][end] = true; |
| 24 | + current.addLast(s.substring(start, end + 1)); |
| 25 | + addValidPartitions(s, end + 1, dp, result, current); |
| 26 | + current.pollLast(); |
| 27 | + } |
76 | 28 | }
|
77 |
| - return true; |
78 | 29 | }
|
79 | 30 | }
|
0 commit comments