-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5083d1f
commit 81095d6
Showing
5 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |