Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
DimkaGorhover committed Jul 14, 2020
1 parent 5083d1f commit 81095d6
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
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));
}
}

0 comments on commit 81095d6

Please sign in to comment.