From 665c07a3f5fd25266d4c7c01874696c2eadf22f5 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 4 Jan 2025 19:23:23 +0800 Subject: [PATCH] Add solution and test-cases for problem 132 --- .../0132.Palindrome-Partitioning-II/README.md | 35 ++++++++++++++++ .../Solution.go | 42 ++++++++++++++++++- .../Solution_test.go | 14 +++---- 3 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 leetcode/101-200/0132.Palindrome-Partitioning-II/README.md diff --git a/leetcode/101-200/0132.Palindrome-Partitioning-II/README.md b/leetcode/101-200/0132.Palindrome-Partitioning-II/README.md new file mode 100644 index 000000000..f16b5c723 --- /dev/null +++ b/leetcode/101-200/0132.Palindrome-Partitioning-II/README.md @@ -0,0 +1,35 @@ +# [132.Palindrome Partitioning II][title] + +## Description +Given a string `s`, partition `s` such that every `substring` of the partition is a `palindrome`. + +Return the **minimum** cuts needed for a palindrome partitioning of `s`. + +**Example 1:** + +``` +Input: s = "aab" +Output: 1 +Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut. +``` + +**Example 2:** + +``` +Input: s = "a" +Output: 0 +``` + +**Example 3:** + +``` +Input: s = "ab" +Output: 1 +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/palindrome-partitioning-ii/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution.go b/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution.go index d115ccf5e..57da51f74 100644 --- a/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution.go +++ b/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution.go @@ -1,5 +1,43 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) int { + cache := map[string]int{} + var ( + dfs func(string) int + isPalindrome func(string) bool + ) + isPalindrome = func(s string) bool { + l, r := 0, len(s)-1 + for ; l < r; l, r = l+1, r-1 { + if s[l] != s[r] { + return false + } + } + return true + } + dfs = func(cur string) int { + l := len(cur) + if l == 0 || l == 1 { + return 0 + } + if v, ok := cache[cur]; ok { + return v + } + if isPalindrome(cur) { + cache[cur] = 0 + return 0 + } + m := -1 + for end := 1; end < len(cur); end++ { + if isPalindrome(cur[:end]) { + r := dfs(cur[end:]) + 1 + if m == -1 || m > r { + m = r + } + } + } + cache[cur] = m + return m + } + return dfs(s) } diff --git a/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution_test.go b/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution_test.go index 14ff50eb4..b5ddf2aa0 100644 --- a/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution_test.go +++ b/leetcode/101-200/0132.Palindrome-Partitioning-II/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "aab", 1}, + {"TestCase2", "a", 0}, + {"TestCase3", "ab", 1}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }