From e5035afe464d810c12b3301f90a7f97fe1945de1 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 11 Jan 2025 15:12:28 +0800 Subject: [PATCH] Add solution and test-cases for problem 1400 --- .../README.md | 36 +++++++++++++++++++ .../Solution.go | 27 ++++++++++++-- .../Solution_test.go | 19 +++++----- 3 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/README.md diff --git a/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/README.md b/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/README.md new file mode 100644 index 000000000..34b170e1d --- /dev/null +++ b/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/README.md @@ -0,0 +1,36 @@ +# [1400.Construct K Palindrome Strings][title] + +## Description +Given a string `s` and an integer `k`, return `true` if you can use all the characters in `s` to construct `k` palindrome strings or `false` otherwise. + +**Example 1:** + +``` +Input: s = "annabelle", k = 2 +Output: true +Explanation: You can construct two palindromes using all characters in s. +Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" +``` + +**Example 2:** + +``` +Input: s = "leetcode", k = 3 +Output: false +Explanation: It is impossible to construct 3 palindromes using all the characters of s. +``` + +**Example 3:** + +``` +Input: s = "true", k = 4 +Output: true +Explanation: The only possible solution is to put each character in a separate string. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/construct-k-palindrome-strings/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution.go b/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution.go index d115ccf5e..7e90c68c3 100755 --- a/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution.go +++ b/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution.go @@ -1,5 +1,28 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string, k int) bool { + if len(s) < k { + return false + } + count := [26]int{} + for _, b := range s { + count[b-'a']++ + } + odd, even := 0, 0 + for i, n := range count { + if n != 0 { + if n&1 == 1 { + odd++ + count[i]-- + } + even += count[i] + } + } + if odd > k { + return false + } + if k == odd { + return true + } + return k-odd <= even } diff --git a/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution_test.go b/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution_test.go index 14ff50eb4..61f3e814a 100755 --- a/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution_test.go +++ b/leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + s string + k int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "annabelle", 2, true}, + {"TestCase2", "leetcode", 3, false}, + {"TestCase3", "true", 4, true}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.s, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.s, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }