当前位置:网站首页>LeetCode 1400. Construct K palindrome strings
LeetCode 1400. Construct K palindrome strings
2022-07-01 03:52:00 【Daylight629】
1400. structure K Palindrome strings
Give you a string s And an integer k . Please use s In a string All characters structure k One is not empty Palindrome string .
If you can use s All characters in the structure k Palindrome strings , So please go back to True , Otherwise return to False .
Example 1:
Input :s = "annabelle", k = 2
Output :true
explain : It can be used s All characters in the structure 2 Palindrome strings .
Some feasible construction schemes include :"anna" + "elble","anbna" + "elle","anellena" + "b"
Example 2:
Input :s = "leetcode", k = 3
Output :false
explain : No use s All characters in the structure 3 A palindrome string .
Example 3:
Input :s = "true", k = 4
Output :true
explain : The only feasible solution is to let s Each character in the form of a single string .
Example 4:
Input :s = "yzyzyzyzyzyzyzy", k = 2
Output :true
explain : All you will need is z Put it in a string , be-all y Put it in another string . Then both strings are palindromes .
Example 5:
Input :s = "cr", k = 7
Output :false
explain : We don't have enough characters to construct 7 A palindrome string .
Tips :
1 <= s.length <= 10^5sAll characters in are lowercase letters .1 <= k <= 10^5
Two 、 Method 1
Statistics can make the most and least palindrome substrings , have a look k Whether it's inside
class Solution {
public boolean canConstruct(String s, int k) {
// The right boundary is the length of the string
int right = s.length();
// Count the number of occurrences of each character
int[] occ = new int[26];
for (int i = 0; i < right; ++i) {
++occ[s.charAt(i) - 'a'];
}
// The left boundary is the number of characters with odd number of times
int left = 0;
for (int i = 0; i < 26; ++i) {
if (occ[i] % 2 == 1) {
++left;
}
}
// Note that there is no special case of characters with an odd number of times
left = Math.max(left, 1);
return left <= k && k <= right;
}
}
Complexity analysis
Time complexity :O(N+∣Σ∣), among N Is string s The length of ,Σ It's a character set. ( That is, the number of possible character types in the string ), In this question, the string will only contain lowercase letters , therefore ∣Σ∣=26. We need to s Do a walk , Get the number of occurrences of each character , The time complexity is O(N). After this , We need to iterate through each character , Count the number of characters that appear an odd number of times , The time complexity is O(∣Σ∣).
Spatial complexity :O(∣Σ∣). We need to use an array or hash table to store the number of occurrences of each character .
边栏推荐
- 318. 最大单词长度乘积
- [deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
- [JPCs publication] the Third International Conference on control theory and application in 2022 (icocta 2022)
- Jeecgboot output log, how to use @slf4j
- 283.移动零
- The programmer's girlfriend gave me a fatigue driving test
- 171. excel table column No
- 168. excel table column name
- 快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
- Review column - message queue
猜你喜欢

谷粒学院微信扫码登录过程记录以及bug解决

盘点华为云GaussDB(for Redis)六大秒级能力

Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)

[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API

陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
![[TA frost wolf \u may- hundred people plan] 1.3 secret of texture](/img/dc/ce2819258632cdcf2e130a4c05600e.png)
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture

[TA frost wolf \u may - "hundred people plan"] 2.1 color space

Usage of AfxMessageBox and MessageBox

How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
![[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions](/img/be/325f78dee744138a865c13d2c20475.png)
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
随机推荐
[reach out to Party welfare] developer reload system sequence
Libevent Library Learning
Network metering - application layer
What happens when a function is called before it is declared in C?
431. 将 N 叉树编码为二叉树 DFS
互联网行业最佳产品开发流程 推荐!
有效的 @SuppressWarnings 警告名称
171. Excel 表列序号
Analyse et cas du modèle pageobject
241. Design priorities for operational expressions
【TA-霜狼_may-《百人计划》】2.2 模型与材质空间
线程常用方法与守护线程
Database DDL (data definition language) knowledge points
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
程序员女友给我做了一个疲劳驾驶检测
谷粒学院微信扫码登录过程记录以及bug解决
【TA-霜狼_may-《百人计划》】1.2.1 向量基础
8. string conversion integer (ATOI)
PageObject模式解析及案例
389. find a difference