Skip to content

Commit 2a1606b

Browse files
authored
Create Palindrome Partitioning.java
1 parent 9b00748 commit 2a1606b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public List<List<String>> partition(String s) {
3+
List<List<String>> res = new ArrayList<>();
4+
List<String> path = new ArrayList<>();
5+
funC(0, s, path, res);
6+
return res;
7+
}
8+
public void funC(int ind, String s, List<String> path, List<List<String>> res) {
9+
if (ind == s.length()) {
10+
res.add(new ArrayList<>(path));
11+
return;
12+
}
13+
for (int i = ind; i < s.length(); i++) {
14+
if (isPalindrome(s, ind, i)) {
15+
path.add(s.substring(ind, i+1));
16+
funC(i+1, s, path, res);
17+
path.remove(path.size()-1);
18+
}
19+
}
20+
}
21+
public boolean isPalindrome(String s, int start, int end) {
22+
while (start <= end) {
23+
if (s.charAt(start++) != s.charAt(end--)) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
}

0 commit comments

Comments
 (0)