Skip to content

Commit 2856c94

Browse files
committed
Add solution and test-cases for problem 1400
1 parent d569e0c commit 2856c94

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [1400.Construct K Palindrome Strings][title]
2+
3+
## Description
4+
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.
5+
6+
**Example 1:**
7+
8+
```
9+
Input: s = "annabelle", k = 2
10+
Output: true
11+
Explanation: You can construct two palindromes using all characters in s.
12+
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
13+
```
14+
15+
**Example 2:**
16+
17+
```
18+
Input: s = "leetcode", k = 3
19+
Output: false
20+
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
21+
```
22+
23+
**Example 3:**
24+
25+
```
26+
Input: s = "true", k = 4
27+
Output: true
28+
Explanation: The only possible solution is to put each character in a separate string.
29+
```
30+
31+
## 结语
32+
33+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
34+
35+
[title]: https://leetcode.com/problems/construct-k-palindrome-strings/
36+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, k int) bool {
4+
if len(s) < k {
5+
return false
6+
}
7+
count := [26]int{}
8+
for _, b := range s {
9+
count[b-'a']++
10+
}
11+
odd, even := 0, 0
12+
for _, n := range count {
13+
if n == 0 {
14+
continue
15+
}
16+
even += n
17+
if n&1 == 1 {
18+
odd++
19+
}
20+
}
21+
if odd > k {
22+
return false
23+
}
24+
if k == odd {
25+
return true
26+
}
27+
return k <= even
528
}

leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
s string
14+
k int
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "annabelle", 2, true},
18+
{"TestCase2", "leetcode", 3, false},
19+
{"TestCase3", "true", 4, true},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.s, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.s, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)