-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromePartitioning.java
60 lines (49 loc) · 1.79 KB
/
PalindromePartitioning.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
package Algorithms.Backtracking.Medium;
// LeetCode: 131. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/
// Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
//
// A palindrome string is a string that reads the same backward as forward
// Example 1:
//
// Input: s = "aab"
// Output: [["a","a","b"],["aa","b"]]
// Example 2:
//
// Input: s = "a"
// Output: [["a"]]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PalindromePartitioning {
List<List<String>> partitionsList = new ArrayList<>();
public List<List<String>> partition(String s) {
partitionHelper(s, 0, new ArrayList<>());
return partitionsList;
}
public void partitionHelper(String str, int start, List<String> partitions){
if(str.length() == start) partitionsList.add(new ArrayList<>(partitions));
for (int end= start; end< str.length(); end++){
String temp = str.substring(start, end+1);
if(isPalindrome(temp)){
partitions.add(temp);
partitionHelper(str, end+1, partitions);
partitions.remove(partitions.size()-1);
}
}
}
private boolean isPalindrome(String str){
int i =0, j=str.length()-1;
while (i<=j){
if(str.charAt(i) != str.charAt(j)) return false;
i++;
j--;
}
return true;
}
public static void main(String[] args){
PalindromePartitioning palindromePartitioning = new PalindromePartitioning();
palindromePartitioning.partition("aab").forEach(partition -> {
partition.forEach(System.out::println);
});
}
}