-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131.palindrome-partitioning.java
70 lines (65 loc) · 1.57 KB
/
131.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* @lc app=leetcode id=131 lang=java
*
* [131] Palindrome Partitioning
*
* https://leetcode.com/problems/palindrome-partitioning/description/
*
* algorithms
* Medium (45.42%)
* Likes: 1574
* Dislikes: 58
* Total Accepted: 212.8K
* Total Submissions: 466K
* Testcase Example: '"aab"'
*
* Given a string s, partition s such that every substring of the partition is
* a palindrome.
*
* Return all possible palindrome partitioning of s.
*
* Example:
*
*
* Input: "aab"
* Output:
* [
* ["aa","b"],
* ["a","a","b"]
* ]
*
*
*/
// @lc code=start
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<>();
dfs(s, 0, new ArrayList<String>(), result);
return result;
}
private void dfs(String s, int start, List<String> path, List<List<String>> result) {
if (start >= s.length()) {
result.add(new ArrayList<>(path));
return;
}
for (int i = start; i < s.length(); i++) {
String curr = s.substring(start, i+1);
if (isPalindrome(curr)) {
path.add(curr);
dfs(s, i + 1, path, result);
path.remove(path.size() - 1);
}
}
}
private boolean isPalindrome(String s) {
if (s.length() <= 1) return true;
int l = 0, r = s.length() - 1;
while (l < r) {
if (s.charAt(l) != s.charAt(r)) return false;
l++;
r--;
}
return true;
}
}
// @lc code=end