diff --git a/src/main/java/org/gd/leetcode/common/LeetCode.java b/src/main/java/org/gd/leetcode/common/LeetCode.java index a0a01f78..77eaa6f6 100644 --- a/src/main/java/org/gd/leetcode/common/LeetCode.java +++ b/src/main/java/org/gd/leetcode/common/LeetCode.java @@ -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} diff --git a/src/main/java/org/gd/leetcode/p0131/Solution.java b/src/main/java/org/gd/leetcode/p0131/Solution.java index 5b345351..8beddc1c 100644 --- a/src/main/java/org/gd/leetcode/p0131/Solution.java +++ b/src/main/java/org/gd/leetcode/p0131/Solution.java @@ -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, @@ -23,6 +25,7 @@ class Solution { private List> result; + @SuppressWarnings("DuplicatedCode") private static boolean isPalindrome(String word) { final int length = word.length(); if (length == 0) diff --git a/src/main/java/org/gd/leetcode/p0132/Solution.java b/src/main/java/org/gd/leetcode/p0132/Solution.java new file mode 100644 index 00000000..7272bbf3 --- /dev/null +++ b/src/main/java/org/gd/leetcode/p0132/Solution.java @@ -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; + } +} diff --git a/src/test/java/org/gd/leetcode/p0131/SolutionTest.java b/src/test/java/org/gd/leetcode/p0131/SolutionTest.java index 4d05110a..8dd20a80 100644 --- a/src/test/java/org/gd/leetcode/p0131/SolutionTest.java +++ b/src/test/java/org/gd/leetcode/p0131/SolutionTest.java @@ -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 args() { diff --git a/src/test/java/org/gd/leetcode/p0132/SolutionTest.java b/src/test/java/org/gd/leetcode/p0132/SolutionTest.java new file mode 100644 index 00000000..77a466c9 --- /dev/null +++ b/src/test/java/org/gd/leetcode/p0132/SolutionTest.java @@ -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 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)); + } +} \ No newline at end of file diff --git a/tasks.todo b/tasks.todo index 798dc692..afac3691 100644 --- a/tasks.todo +++ b/tasks.todo @@ -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/