|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +
|
| 5 | +-* 131. Palindrome Partitioning *- |
| 6 | +
|
| 7 | +Given a string s, partition s such that every |
| 8 | +substring |
| 9 | + of the partition is a |
| 10 | +palindrome |
| 11 | +. Return all possible palindrome partitioning of s. |
| 12 | +
|
| 13 | + |
| 14 | +
|
| 15 | +Example 1: |
| 16 | +
|
| 17 | +Input: s = "aab" |
| 18 | +Output: [["a","a","b"],["aa","b"]] |
| 19 | +Example 2: |
| 20 | +
|
| 21 | +Input: s = "a" |
| 22 | +Output: [["a"]] |
| 23 | + |
| 24 | +
|
| 25 | +Constraints: |
| 26 | +
|
| 27 | +1 <= s.length <= 16 |
| 28 | +s contains only lowercase English letters. |
| 29 | +
|
| 30 | +
|
| 31 | +*/ |
| 32 | +class A { |
| 33 | + bool checkPalindrome(String str, int startIndex, int lastIndex) { |
| 34 | + while (startIndex <= lastIndex) { |
| 35 | + if (str.codeUnitAt(startIndex) != str.codeUnitAt(lastIndex)) return false; |
| 36 | + startIndex++; |
| 37 | + lastIndex--; |
| 38 | + } |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + void palindromePartition( |
| 43 | + int index, List<String> ds, List<List<String>> output, String str) { |
| 44 | + if (index == str.length) { |
| 45 | + // output.add([...ds]..toList()); |
| 46 | + output.add([...ds]); |
| 47 | + return; |
| 48 | + } |
| 49 | + for (int i = index; i < str.length; i++) { |
| 50 | + if (checkPalindrome(str, index, i)) { |
| 51 | + ds.add(str.substring(index, i + 1)); |
| 52 | + palindromePartition(i + 1, ds, output, str); |
| 53 | + ds.remove(ds.length - 1); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + List<List<String>> partition(String s) { |
| 59 | + List<List<String>> output = <String>[].map((e) => <String>[]).toList(); |
| 60 | + List<String> ds = []; |
| 61 | + palindromePartition(0, ds, output, s); |
| 62 | + return output; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class B { |
| 67 | + bool isPalindrome(String s) { |
| 68 | + int left = 0, right = s.length - 1; |
| 69 | + while (left <= right) { |
| 70 | + if (s[left] != s[right]) return false; |
| 71 | + left++; |
| 72 | + right--; |
| 73 | + } |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + void helper(String s, List<String> step, List<List<String>> result) { |
| 78 | + // Base case |
| 79 | + if (s.length == 0) { |
| 80 | + result.add(step.toList()); |
| 81 | + return; |
| 82 | + } |
| 83 | + for (int i = 1; i <= s.length; i++) { |
| 84 | + String temp = s.substring(0, i); |
| 85 | + if (!isPalindrome(temp)) |
| 86 | + continue; // only do backtracking when current string is palindrome |
| 87 | + |
| 88 | + step.add(temp); // choose |
| 89 | + helper(s.substring(i, s.length), step, result); // explore |
| 90 | + step.remove(step.length - 1); // un-choose |
| 91 | + } |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + List<List<String>> partition(String s) { |
| 96 | + // Backtracking |
| 97 | + // Edge case |
| 98 | + if (s.length == 0) return []; |
| 99 | + |
| 100 | + List<List<String>> result = []; |
| 101 | + helper(s, [], result); |
| 102 | + return result; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +class C { |
| 107 | + bool checkPalindrome(String str, int startIndex, int lastIndex) { |
| 108 | + while (startIndex <= lastIndex) { |
| 109 | + if (str[startIndex] != str[lastIndex]) return false; |
| 110 | + startIndex++; |
| 111 | + lastIndex--; |
| 112 | + } |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + void palindromePartition( |
| 117 | + int index, List<String> ds, List<List<String>> output, String str) { |
| 118 | + if (index == str.length) { |
| 119 | + output.add(List.filled(ds.length - 1, "")); |
| 120 | + return; |
| 121 | + } |
| 122 | + for (int i = index; i < str.length; i++) { |
| 123 | + if (checkPalindrome(str, index, i)) { |
| 124 | + ds.add(str.substring(index, i - index + 1)); |
| 125 | + palindromePartition(i + 1, ds, output, str); |
| 126 | + ds.removeLast(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + List<List<String>> partition(String s) { |
| 132 | + List<List<String>> output = []; |
| 133 | + List<String> ds = []; |
| 134 | + palindromePartition(0, ds, output, s); |
| 135 | + return output; |
| 136 | + } |
| 137 | +} |
0 commit comments