当前位置:网站首页>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^5
s
All 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 .
边栏推荐
- 10、Scanner. Next() cannot read spaces /indexof -1
- Web components series (VIII) -- custom component style settings
- 187. 重复的DNA序列
- Pytorch training deep learning network settings CUDA specified GPU visible
- 242. valid Letter heteronyms
- [TA frost wolf \u may- hundred talents plan] 1.2.3 MVP matrix operation
- 复习专栏之---消息队列
- Analysis and case of pageobject mode
- [ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle
- [TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
猜你喜欢
在线公网安备案保姆级教程【伸手党福利】
bootsrap中的栅格系统
Binary tree god level traversal: Morris traversal
Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
Millet College wechat scanning code login process record and bug resolution
Implement pow (x, n) function
C语言的sem_t变量类型
The programmer's girlfriend gave me a fatigue driving test
Network metering - application layer
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
随机推荐
【TA-霜狼_may-《百人计划》】1.1 渲染流水线
盘点华为云GaussDB(for Redis)六大秒级能力
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
程序员女友给我做了一个疲劳驾驶检测
How keil displays Chinese annotations (simple with pictures)
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
392. judgment subsequence
242. valid Letter heteronyms
The difference between MFC for static libraries and MFC for shared libraries
All in one 1086: Jiaogu conjecture
Analyse et cas du modèle pageobject
431. 将 N 叉树编码为二叉树 DFS
Pytorch training deep learning network settings CUDA specified GPU visible
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
You cannot right-click F12 to view the source code solution on the web page
168. Excel表列名称
187. 重复的DNA序列
171. Excel 表列序号
Network metering - application layer
Millet College wechat scanning code login process record and bug resolution