-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerkleAssign.java
More file actions
65 lines (53 loc) · 2.33 KB
/
MerkleAssign.java
File metadata and controls
65 lines (53 loc) · 2.33 KB
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
import java.util.*;
import java.io.*;
public class MerkleAssign {
public static void main(String[] args) {
List<List<String>> totalPartitions = new ArrayList<>();
//To take input from console, uncomment these two line
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// String s = br.readLine();
//Comment this line, if you are taking input from console
String s = "BorrowOrRob";
//Pass the string to the functions, it will return List<List<String>>
// with all the palindromic partitions of the string
totalPartitions = doPartitions(s);
// Printing the palindromic partitions
for (List<String> partitions : totalPartitions) {
StringBuilder word = new StringBuilder();
for (String p : partitions) {
word.append(p+" ");
}
System.out.println(word.toString());
}
}
// it return List of List of string containing all palindrome partitions.
private static List<List<String>> doPartitions(String s) {
List<List<String>> result = new ArrayList<>();
backtrack(s, 0, new ArrayList<>(), result);
return result;
}
// It checks if current paritition from start is palindrome or not.
// If it is a palindrome then, it will add it to the list and again recur for remaining string
private static void backtrack(String s, int start, List<String> tempList, List<List<String>> result) {
if (start == s.length()) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = start; i < s.length(); i++) {
if (isPalindrome(s, start, i)) { // Checking if current partition is Palindrome or not.
tempList.add(s.substring(start, i + 1));
backtrack(s, i + 1, tempList, result);
tempList.remove(tempList.size() - 1);
}
}
}
}
// If string is palindrome then return True otherwise False
private static boolean isPalindrome(String s, int left, int right) {
while (left < right) {
if (Character.toLowerCase(s.charAt(left++)) != Character.toLowerCase(s.charAt(right--))) {
return false;
}
}
return true;
}
}