-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00131-palindrome_partitioning.java
54 lines (43 loc) · 1.38 KB
/
00131-palindrome_partitioning.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 131: Palindrome Partitioning
// https://leetcode.com/problems/palindrome-partitioning
import java.util.List;
import java.util.ArrayList;
class Solution {
private void backtrack(String s, int start, List<String> current, List<List<String>> result) {
if (start == s.length()) {
result.add(new ArrayList<>(current));
return;
}
for (int i=start; i<s.length(); i++) {
if (isPalindrome(s, start, i)) {
String str = s.substring(start, i + 1);
current.add(str);
backtrack(s, i + 1, current, result);
current.remove(current.size() - 1);
}
}
}
private boolean isPalindrome(String s, int left, int right) {
while (left < right) {
if (s.charAt(left) != s.charAt(right)) return false;
left++;
right--;
}
return true;
}
// SOLUTION
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<>();
List<String> current = new ArrayList<>();
backtrack(s, 0, current, result);
return result;
}
public static void main(String[] args) {
Solution o = new Solution();
// INPUT
String s = "aab";
// OUTPUT
var result = o.partition(s);
System.out.println(result);
}
}