1400:构造 K 个回文字符串

This commit is contained in:
huangge1199 2021-09-08 15:58:55 +08:00
parent 1b0f9df876
commit 4036051760
2 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,90 @@
//给你一个字符串 s 和一个整数 k 请你用 s 字符串中 所有字符 构造 k 个非空 回文串
//
// 如果你可以用 s 中所有字符构造 k 个回文字符串那么请你返回 True 否则返回 False
//
//
//
// 示例 1
//
//
//输入s = "annabelle", k = 2
//输出true
//解释可以用 s 中所有字符构造 2 个回文字符串
//一些可行的构造方案包括"anna" + "elble""anbna" + "elle""anellena" + "b"
//
//
// 示例 2
//
//
//输入s = "leetcode", k = 3
//输出false
//解释无法用 s 中所有字符构造 3 个回文串
//
//
// 示例 3
//
//
//输入s = "true", k = 4
//输出true
//解释唯一可行的方案是让 s 中每个字符单独构成一个字符串
//
//
// 示例 4
//
//
//输入s = "yzyzyzyzyzyzyzy", k = 2
//输出true
//解释你只需要将所有的 z 放在一个字符串中所有的 y 放在另一个字符串中那么两个字符串都是回文串
//
//
// 示例 5
//
//
//输入s = "cr", k = 7
//输出false
//解释我们没有足够的字符去构造 7 个回文串
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 10^5
// s 中所有字符都是小写英文字母
// 1 <= k <= 10^5
//
// Related Topics 贪心 哈希表 字符串 计数 👍 28 👎 0
package leetcode.editor.cn;
//1400:构造 K 个回文字符串
class ConstructKPalindromeStrings {
public static void main(String[] args) {
//测试代码
Solution solution = new ConstructKPalindromeStrings().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean canConstruct(String s, int k) {
if (s.length() < k) {
return false;
}
int[] arrs = new int[26];
for (char ch : s.toCharArray()) {
arrs[ch - 'a']++;
}
int count = 0;
for (int i = 0; i < 26; i++) {
if (arrs[i] % 2 == 1) {
count++;
}
}
return count <= k;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,57 @@
<p>给你一个字符串 <code>s</code>&nbsp;和一个整数 <code>k</code>&nbsp;。请你用 <code>s</code>&nbsp;字符串中 <strong>所有字符</strong>&nbsp;构造 <code>k</code>&nbsp;个非空 <strong>回文串</strong>&nbsp;</p>
<p>如果你可以用&nbsp;<code>s</code>&nbsp;中所有字符构造&nbsp;<code>k</code>&nbsp;个回文字符串,那么请你返回 <strong>True</strong>&nbsp;,否则返回&nbsp;<strong>False</strong>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = &quot;annabelle&quot;, k = 2
<strong>输出:</strong>true
<strong>解释:</strong>可以用 s 中所有字符构造 2 个回文字符串。
一些可行的构造方案包括:&quot;anna&quot; + &quot;elble&quot;&quot;anbna&quot; + &quot;elle&quot;&quot;anellena&quot; + &quot;b&quot;
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = &quot;leetcode&quot;, k = 3
<strong>输出:</strong>false
<strong>解释:</strong>无法用 s 中所有字符构造 3 个回文串。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>s = &quot;true&quot;, k = 4
<strong>输出:</strong>true
<strong>解释:</strong>唯一可行的方案是让 s 中每个字符单独构成一个字符串。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>s = &quot;yzyzyzyzyzyzyzy&quot;, k = 2
<strong>输出:</strong>true
<strong>解释:</strong>你只需要将所有的 z 放在一个字符串中,所有的 y 放在另一个字符串中。那么两个字符串都是回文串。
</pre>
<p><strong>示例 5</strong></p>
<pre>
<strong>输入:</strong>s = &quot;cr&quot;, k = 7
<strong>输出:</strong>false
<strong>解释:</strong>我们没有足够的字符去构造 7 个回文串。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
<li><code>s</code>&nbsp;中所有字符都是小写英文字母。</li>
<li><code>1 &lt;= k &lt;= 10^5</code></li>
</ul>
<div><div>Related Topics</div><div><li>贪心</li><li>哈希表</li><li>字符串</li><li>计数</li></div></div><br><div><li>👍 28</li><li>👎 0</li></div>