Skip to content

Commit aa4aabc

Browse files
committed
dev
1 parent 14d3602 commit aa4aabc

5 files changed

Lines changed: 120 additions & 2 deletions

File tree

src/main/java/org/gd/leetcode/common/LeetCode.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@
1919

2020
Tags[] tags() default {};
2121

22-
enum State {TODO, FIXME, DONE, UNKNOWN}
22+
enum State {
23+
24+
TODO,
25+
FIXME,
26+
TIME_LIMIT_EXCEEDED,
27+
DONE,
28+
UNKNOWN;
29+
30+
public boolean isDone() { return this == DONE; }
31+
}
2332

2433
enum Level {EASY, MEDIUM, HARD}
2534

src/main/java/org/gd/leetcode/p0131/Solution.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
/**
1111
* @author Horkhover D.
12-
* @since 2020-07-13.07.2020
12+
* @see org.gd.leetcode.p0132.Solution
13+
* @since 2020-07-13
1314
*/
15+
@SuppressWarnings("JavadocReference")
1416
@LeetCode(
1517
name = "Palindrome Partitioning",
1618
difficulty = LeetCode.Level.MEDIUM,
@@ -23,6 +25,7 @@ class Solution {
2325

2426
private List<List<String>> result;
2527

28+
@SuppressWarnings("DuplicatedCode")
2629
private static boolean isPalindrome(String word) {
2730
final int length = word.length();
2831
if (length == 0)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.gd.leetcode.p0132;
2+
3+
import org.gd.leetcode.common.LeetCode;
4+
5+
/**
6+
* TODO: https://leetcode.com/problems/palindrome-partitioning-ii/
7+
*
8+
* @author Horkhover D.
9+
* @see org.gd.leetcode.p0131.Solution
10+
* @since 2020-07-13.07.2020
11+
*/
12+
@SuppressWarnings("JavadocReference")
13+
@LeetCode(
14+
name = "Palindrome Partitioning II",
15+
difficulty = LeetCode.Level.HARD,
16+
state = LeetCode.State.TIME_LIMIT_EXCEEDED,
17+
tags = {
18+
LeetCode.Tags.DYNAMIC_PROGRAMMING
19+
}
20+
)
21+
class Solution {
22+
23+
private int min;
24+
25+
@SuppressWarnings("DuplicatedCode")
26+
private static boolean isPalindrome(String word) {
27+
final int length = word.length();
28+
if (length == 0)
29+
return false;
30+
if (length == 1)
31+
return true;
32+
int mid = length >> 1;
33+
for (int i = 0; i <= mid; i++) {
34+
int j = length - 1 - i;
35+
if (word.charAt(i) != word.charAt(j))
36+
return false;
37+
}
38+
return true;
39+
}
40+
41+
private void partition(final int cuts,
42+
final String word) {
43+
44+
final int wordLength = word.length();
45+
if (wordLength == 0) {
46+
min = Math.min(min, cuts - 1);
47+
return;
48+
}
49+
if (wordLength == 1) {
50+
min = Math.min(min, cuts);
51+
return;
52+
}
53+
54+
for (int i = 1; i <= wordLength; i++) {
55+
String ss = word.substring(0, i);
56+
if (isPalindrome(ss)) {
57+
partition(cuts + 1, word.substring(i, wordLength));
58+
}
59+
}
60+
}
61+
62+
public int minCut(String word) {
63+
min = Integer.MAX_VALUE;
64+
partition(0, word);
65+
return min;
66+
}
67+
}

src/test/java/org/gd/leetcode/p0131/SolutionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
* Test for {@link Solution}
1515
*
1616
* @author Horkhover D.
17+
* @see org.gd.leetcode.p0132.Solution
1718
* @since 2020-07-13
1819
*/
20+
@SuppressWarnings("JavadocReference")
1921
class SolutionTest {
2022

2123
private static Stream<Arguments> args() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.gd.leetcode.p0132;
2+
3+
import org.junit.jupiter.api.*;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
8+
import java.util.List;
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
/**
14+
* Test for {@link Solution}
15+
*
16+
* @author Horkhover D.
17+
* @see org.gd.leetcode.p0131.SolutionTest
18+
* @since 2020-07-13
19+
*/
20+
@SuppressWarnings("JavadocReference")
21+
@DisplayName("LeetCode 132: Palindrome Partitioning II")
22+
class SolutionTest {
23+
24+
private static Stream<Arguments> args() {
25+
return Stream.of(
26+
Arguments.of("caba", 1),
27+
Arguments.of("aab", 1)
28+
);
29+
}
30+
31+
@ParameterizedTest
32+
@MethodSource("args")
33+
@DisplayName("Partition")
34+
void test_MinCut(String word, int expected) {
35+
assertEquals(expected, new Solution().minCut(word));
36+
}
37+
}

0 commit comments

Comments
 (0)