-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 1400
- Loading branch information
Showing
3 changed files
with
71 additions
and
11 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/README.md
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,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 |
27 changes: 25 additions & 2 deletions
27
leetcode/1301-1400/1400.Construct-K-Palindrome-Strings/Solution.go
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 |
---|---|---|
@@ -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 _, n := range count { | ||
if n == 0 { | ||
continue | ||
} | ||
even += n | ||
if n&1 == 1 { | ||
odd++ | ||
} | ||
} | ||
if odd > k { | ||
return false | ||
} | ||
if k == odd { | ||
return true | ||
} | ||
return k <= even | ||
} |
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