Skip to content

Commit 8a0992a

Browse files
add 1745
1 parent 8206625 commit 8a0992a

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1745|[Palindrome Partitioning IV](https://leetcode.com/problems/palindrome-partitioning-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1745.java) ||Hard|String, DP|
1112
|1743|[Restore the Array From Adjacent Pairs](https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1743.java) ||Medium|Greedy|
1213
|1742|[Maximum Number of Balls in a Box](https://leetcode.com/problems/maximum-number-of-balls-in-a-box/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1742.java) ||Easy|Array|
1314
|1736|[Latest Time by Replacing Hidden Digits](https://leetcode.com/problems/latest-time-by-replacing-hidden-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1736.java) ||Easy|String, Greedy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1745 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/palindrome-partitioning-iv/discuss/1042910/Java-Detailed-Explanation-DP-O(N2)
7+
*
8+
* check whether substring(i, j) is a palindrome becomes checking whether substring(i + 1, j -1) is a palindrome
9+
*
10+
* How we build the dp array:
11+
* start from the top right of this square matrix
12+
*/
13+
public boolean checkPartitioning(String s) {
14+
int n = s.length();
15+
boolean[][] dp = new boolean[n][n];
16+
for (int i = n - 1; i >= 0; i--) {
17+
for (int j = i; j < n; j++) {
18+
if (s.charAt(i) == s.charAt(j)) {
19+
dp[i][j] = (i + 1 < j - 1) ? dp[i + 1][j - 1] : true;
20+
} else {
21+
dp[i][j] = false;
22+
}
23+
}
24+
}
25+
for (int i = 1; i < n - 1; i++) {
26+
for (int j = 1; j < n - 1; j++) {
27+
if (dp[0][i - 1] && dp[i][j] && dp[j + 1][n - 1]) {
28+
return true;
29+
}
30+
}
31+
}
32+
return true;
33+
}
34+
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1745;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1745Test {
10+
private static _1745.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1745.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.checkPartitioning("abcbdd"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.checkPartitioning("bcbddxy"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.checkPartitioning("juchzcedhfesefhdeczhcujzzvbmoeombv"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(true, solution1.checkPartitioning("gbofdldvwelqiizbievfolrujxnwjmjwsjrjeqecwssgtlteltslfzkblzihcgwjnqaiqbxohcnxulxozzkanaofgoddogfoanakzzoxluxnchoxbqiaqnjwgchizlbkzflstletltgsswceqejrjswjmjwnxjurlofveibziiqlewvdldfobgxebrcrbexv"));
35+
}
36+
}

0 commit comments

Comments
 (0)