|
| 1 | +// https://leetcode.com/problems/palindrome-partitioning |
| 2 | +// T: O(n * 2^n) |
| 3 | +// S: O(n * 2^n) |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.List; |
| 8 | + |
| 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 | + } |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 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 | + } |
| 72 | + |
| 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; |
| 76 | + } |
| 77 | + return true; |
| 78 | + } |
| 79 | +} |
0 commit comments