Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode 132: Palindrome Partitioning II #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/org/gd/leetcode/common/LeetCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@

Tags[] tags() default {};

enum State {TODO, FIXME, DONE, UNKNOWN}
enum State {

TODO,
FIXME,
TIME_LIMIT_EXCEEDED,
DONE,
UNKNOWN;

public boolean isDone() { return this == DONE; }
}

enum Level {EASY, MEDIUM, HARD}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/gd/leetcode/p0131/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

/**
* @author Horkhover D.
* @since 2020-07-13.07.2020
* @see org.gd.leetcode.p0132.Solution
* @since 2020-07-13
*/
@SuppressWarnings("JavadocReference")
@LeetCode(
name = "Palindrome Partitioning",
difficulty = LeetCode.Level.MEDIUM,
Expand All @@ -23,6 +25,7 @@ class Solution {

private List<List<String>> result;

@SuppressWarnings("DuplicatedCode")
private static boolean isPalindrome(String word) {
final int length = word.length();
if (length == 0)
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/gd/leetcode/p0132/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.gd.leetcode.p0132;

import org.gd.leetcode.common.LeetCode;

/**
* TODO: https://leetcode.com/problems/palindrome-partitioning-ii/
*
* @author Horkhover D.
* @see org.gd.leetcode.p0131.Solution
* @since 2020-07-13.07.2020
*/
@SuppressWarnings("JavadocReference")
@LeetCode(
name = "Palindrome Partitioning II",
difficulty = LeetCode.Level.HARD,
state = LeetCode.State.TIME_LIMIT_EXCEEDED,
tags = {
LeetCode.Tags.DYNAMIC_PROGRAMMING
}
)
class Solution {

private int min;

@SuppressWarnings("DuplicatedCode")
private static boolean isPalindrome(String word) {
final int length = word.length();
if (length == 0)
return false;
if (length == 1)
return true;
int mid = length >> 1;
for (int i = 0; i <= mid; i++) {
int j = length - 1 - i;
if (word.charAt(i) != word.charAt(j))
return false;
}
return true;
}

private void partition(final int cuts,
final String word) {

final int wordLength = word.length();
if (wordLength == 0) {
min = Math.min(min, cuts - 1);
return;
}
if (wordLength == 1) {
min = Math.min(min, cuts);
return;
}

for (int i = 1; i <= wordLength; i++) {
String ss = word.substring(0, i);
if (isPalindrome(ss)) {
partition(cuts + 1, word.substring(i, wordLength));
}
}
}

public int minCut(String word) {
min = Integer.MAX_VALUE;
partition(0, word);
return min;
}
}
2 changes: 2 additions & 0 deletions src/test/java/org/gd/leetcode/p0131/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* Test for {@link Solution}
*
* @author Horkhover D.
* @see org.gd.leetcode.p0132.Solution
* @since 2020-07-13
*/
@SuppressWarnings("JavadocReference")
class SolutionTest {

private static Stream<Arguments> args() {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/gd/leetcode/p0132/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.gd.leetcode.p0132;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

/**
* Test for {@link Solution}
*
* @author Horkhover D.
* @see org.gd.leetcode.p0131.SolutionTest
* @since 2020-07-13
*/
@SuppressWarnings("JavadocReference")
@DisplayName("LeetCode 132: Palindrome Partitioning II")
class SolutionTest {

private static Stream<Arguments> args() {
return Stream.of(
Arguments.of("caba", 1),
Arguments.of("aab", 1)
);
}

@ParameterizedTest
@MethodSource("args")
@DisplayName("Partition")
void test_MinCut(String word, int expected) {
assertEquals(expected, new Solution().minCut(word));
}
}
1 change: 1 addition & 0 deletions tasks.todo
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ LeetCode:
☐ https://leetcode.com/problems/ugly-number/
☐ https://leetcode.com/problems/happy-number/
☐ https://leetcode.com/problems/number-of-islands/
☐ https://leetcode.com/problems/repeated-substring-pattern/