- ***、题目链接
- 一、题目
- 1、题目描述
- 2、基础框架
- 二、解题报告
- 1、思路分析
- 2、时间复杂度
- 3、代码详解
- 三、写在最后
Day4:Leetcod 1400. 构造 K 个回文字符串
2、基础框架给你一个字符串 s 和一个整数 k 。请你用 s 字符串中 所有字符 构造 k 个非空 回文串 。
如果你可以用 s 中所有字符构造 k 个回文字符串,那么请你返回 True ,否则返回 False 。
样例输入: s = "annabelle", k = 2;
样例输出: true。
- C++给出的代码框架如下:
class Solution {
public:
bool canConstruct(string s, int k) {
}
};
二、解题报告 1、思路分析
(
1
)
(1)
(1) 能构造的最多的回文字符串个数为字符串本身的长度;
(
2
)
(2)
(2) 能构造最少回文串个数为在原字符串中出现个数为奇数的字母的数量。
O ( n ) O(n) O(n) , n n n 为字符串长度;
3、代码详解class Solution {
public:
bool canConstruct(string s, int k) {
int maxn = s.size();
int min = 0;
int nums[26] = {0};
for(char ss: s){
++nums[ ss - 'a'];
}
for(int a: nums){
if(a % 2 == 1)
++min;
}
min = max(min, 1);
return min <= k && k <= maxn;
}
};
三、写在最后
新知识 利用 char ss : s 来构造边界循环。



