forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1745.java
36 lines (34 loc) · 1.24 KB
/
_1745.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
package com.fishercoder.solutions;
public class _1745 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/palindrome-partitioning-iv/discuss/1042910/Java-Detailed-Explanation-DP-O(N2)
*
* check whether substring(i, j) is a palindrome becomes checking whether substring(i + 1, j -1) is a palindrome
*
* How we build the dp array:
* start from the top right of this square matrix
*/
public boolean checkPartitioning(String s) {
int n = s.length();
boolean[][] dp = new boolean[n][n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = (i + 1 <= j - 1) ? dp[i + 1][j - 1] : true;
} else {
dp[i][j] = false;
}
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = i; j < n - 1; j++) {
if (dp[0][i - 1] && dp[i][j] && dp[j + 1][n - 1]) {
return true;
}
}
}
return false;
}
}
}