Skip to content

Commit e80cf18

Browse files
author
Prakhar Agarwal
authored
Create Palindrome Partitioning.java
1 parent 7e1dd9a commit e80cf18

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Palindrome Partitioning.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public List<List<String>> partition(String s) {
5+
List<List<String>> list = new ArrayList<>();
6+
util(0, new ArrayList<String>(), s, list);
7+
return list;
8+
}
9+
10+
private void util(int start, List<String> temp, String s, List<List<String>> list) {
11+
if (start == s.length()) {
12+
list.add(new ArrayList<>(temp));
13+
return;
14+
}
15+
for (int i = start; i < s.length(); i++) {
16+
if (isPalindrome(s.substring(start, i + 1))) {
17+
temp.add(s.substring(start, i + 1));
18+
util(i + 1, temp, s, list);
19+
temp.remove(temp.size() - 1);
20+
}
21+
}
22+
}
23+
24+
private boolean isPalindrome(String s) {
25+
int i = 0;
26+
int j = s.length() - 1;
27+
while (i <= j) {
28+
if (s.charAt(i) != s.charAt(j)) {
29+
return false;
30+
}
31+
i++;
32+
j--;
33+
}
34+
return true;
35+
}
36+
}

0 commit comments

Comments
 (0)