当前位置:网站首页>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 .
边栏推荐
- 【TA-霜狼_may-《百人计划》】2.1 色彩空间
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
- Use of JMeter counters
- 318. Maximum word length product
- Cygwin的下载和安装配置
- Blueprism registration, download and install -rpa Chapter 1
- The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context
- 431. encode n-ary tree as binary tree DFS
- Millet College wechat scanning code login process record and bug resolution
- 【TA-霜狼_may-《百人计划》】1.1 渲染流水线
猜你喜欢

Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page

不同性能测试工具的并发模式

The difference between MFC for static libraries and MFC for shared libraries

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

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

How keil displays Chinese annotations (simple with pictures)
【历史上的今天】6 月 30 日:冯·诺依曼发表第一份草案;九十年代末的半导体大战;CBS 收购 CNET

C语言的sem_t变量类型

25.K个一组翻转链表

Grid system in bootstrap
随机推荐
10. regular expression matching
168. excel table column name
Jenkins自动清理构建历史
Promql select time series
409. longest palindrome
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
166. 分数到小数
The difference between MFC for static libraries and MFC for shared libraries
Pytorch training deep learning network settings CUDA specified GPU visible
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
431. encode n-ary tree as binary tree DFS
在线公网安备案保姆级教程【伸手党福利】
214. minimum palindrome string
Go learning --- unit test subtest
程序员女友给我做了一个疲劳驾驶检测
Unexpected token o in JSON at position 1 ,JSON解析问题
SEM of C language_ Tvariable type
208. 实现 Trie (前缀树)
72. 编辑距离