当前位置:网站首页>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 .
边栏推荐
猜你喜欢

How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars

基于Unet的环路滤波

报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
![[ta- frost wolf \u may- hundred people plan] 2.2 model and material space](/img/93/95ee3d4389ffd227559dc8b3207e1d.png)
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
![[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline](/img/af/4498382bc47d8c9ae41c407b9d1265.png)
[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline

Visit the image URL stored by Alibaba cloud to preview the thumbnail directly on the web page instead of downloading it directly

Why can't you find the corresponding function by clicking go to definiton (super easy has a diagram)

【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算

Grid system in bootstrap

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
随机推荐
[EI conference] the Third International Conference on nanomaterials and nanotechnology in 2022 (nanomt 2022)
392. 判断子序列
5. [WebGIS practice] software operation - service release and permission management
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
72. edit distance
浏览器顶部loading(来自知乎)
166. fractions to decimals
The programmer's girlfriend gave me a fatigue driving test
205. 同构字符串
Valentine's Day is nothing.
187. 重复的DNA序列
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Pytorch training deep learning network settings CUDA specified GPU visible
【历史上的今天】6 月 30 日:冯·诺依曼发表第一份草案;九十年代末的半导体大战;CBS 收购 CNET
8. string conversion integer (ATOI)
[reach out to Party welfare] developer reload system sequence
PageObject模式解析及案例
Explain spark operation mode in detail (local+standalone+yarn)