【LeetCode】1400. 构造 K 个回文字符串 Construct K Palindrome Strings
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
目录
题目地址:https://leetcode-cn.com/problems/construct-k-palindrome-strings/
题目描述
给你一个字符串 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^5s中所有字符都是小写英文字母。1 <= k <= 10^5
题目大意
判断给出的字符串 s 能不能恰好构成 k 个回文串。
解题方法
统计奇数字符出现次数
其实很简单。我们只需要判断字符串中有多少个出现次数为奇数的字符就行了。
为什么?
一个回文字符串中只能有 0 个或者 1 个出现次数为 1 的字符,这个字符必须位于回文字符串的中间。
因此,要判断能不能有 k 个回文字符串,我们就看奇数字符出现的次数是否小于等于 k 个。
分配情况:
- 如果奇数字符恰好有 k 个,那么拆分出来的每个回文字符串中各分配 1 个。
- 如果奇数字符小于 k 个,那么剩下的回文串中不分配奇数字符,即只由偶数字符构成。
C++代码如下。
class Solution {
public:
bool canConstruct(string s, int k) {
const int N = s.size();
if (N < k) return false;
if (N == k) return true;
vector<int> count(26, 0);
for (char c : s) {
count[c - 'a'] ++;
}
int count_odd = 0;
for (int i = 0; i < count.size(); ++i) {
if (count[i] % 2 == 1) {
count_odd ++;
}
}
return count_odd <= k;
}
};
欢迎关注负雪明烛的刷题博客,leetcode刷题800多,每道都讲解了详细写法!
日期
2020 年 4 月 5 日 —— 好久不打周赛了
【LeetCode】1400. 构造 K 个回文字符串 Construct K Palindrome Strings的更多相关文章
- C#LeetCode刷题之#680-验证回文字符串 Ⅱ(Valid Palindrome II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3961 访问. 给定一个非空字符串 s,最多删除一个字符.判断是否 ...
- LeetCode 5. Longest Palindromic Substring & 回文字符串
Longest Palindromic Substring 回文这种简单的问题,在C里面印象很深啊.希望能一次过. 写的时候才想到有两种情况: 454(奇数位) 4554(偶数位) 第1次提交 cla ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1
680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- Leetcode 680.验证回文字符串
验证回文字符串 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
随机推荐
- Latex 文档格式化
title: "Latex 文档格式化" author: 李龙翔 date: "Nov 22, 2019" subject: "Markdown&qu ...
- BaiduPCS-Go----百度云下载工具
1.网页登录百度网盘:https://pan.baidu.com/2.百度输入法生成:http://pcs.baidu.com/rest/2.0/pcs/file?app_id=265486& ...
- MYSQL5.8---1
主键不能为空,唯一键可以为空且可以多个唯一键 外键必须为另一个表中的主键 外键的用途是确保数据的完整性.它通常包括以下几种: 1 实体完整性,确保每个实体是唯一的(通过主键来实施) 2 域完整性,确保 ...
- C语言计算fastq文件GC含量
C语言小练习:计算非压缩fastq格式的GC含量 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <strin ...
- 24-Longest Palindromic Substring-Leetcode
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [C++] vptr, where are you?
Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...
- Docker学习(六)——Dockerfile文件详解
Docker学习(六)--Dockerfile文件详解 一.环境介绍 1.Dockerfile中所用的所有文件一定要和Dockerfile文件在同一级父目录下,可以为Dockerfile父目录的子目录 ...
- linux修改文件权限命令
先看个实例: [root@local opt]#ls -al ls -al 命令是列出目录的所有文件,包括隐藏文件.隐藏文件的文件名第一个字符为'.' -rw-r--r-- 1 root root ...
- 【Python】【Basic】MacOS上搭建Python开发环境
1. Python3 1.1. 下载地址:https://www.python.org/downloads/mac-osx/ 1.1.1. PKG包安装: 没啥可说的,点点点,下一步而已,不用手动配置 ...
- spring注解-组件注册
一.@Configuration+@Bean @Configuration:配置类==配置文件 @Bean:给容器中注册一个Bean:类型为返回值的类型,默认是用方法名作为id @Bean(" ...