forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
49 lines (38 loc) · 1.41 KB
/
Solution.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* @author Oleg Cherednik
* @since 29.05.2019
*/
public class Solution {
public static void main(String... args) {
System.out.println(Arrays.toString(split("racecarannakayak").toArray(new String[0]))); // ["racecar", "anna", "kayak"]
System.out.println(Arrays.toString(split("abc").toArray(new String[0]))); // ["a", "b", "c"]
}
public static List<String> split(String str) {
return Optional.ofNullable(findPalindromes(str, 0, str.length() - 1)).orElse(Collections.emptyList());
}
private static List<String> findPalindromes(String str, int left, int right) {
for (int i = right; i >= left; i--) {
if (!isPalindrome(str, left, i))
continue;
List<String> tail = i + 1 <= right ? findPalindromes(str, i + 1, right) : Collections.emptyList();
if (tail == null)
continue;
List<String> res = new ArrayList<>(tail.size() + 1);
res.add(str.substring(left, i + 1));
res.addAll(tail);
return res;
}
return null;
}
private static boolean isPalindrome(String str, int i, int j) {
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
}