Skip to content

Commit 70a7f19

Browse files
committed
feat: add solutions to lc problem: No.1400
No.1400.Construct K Palindrome Strings
1 parent b540fbb commit 70a7f19

File tree

12 files changed

+195
-89
lines changed

12 files changed

+195
-89
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.idea/
22
.DS_Store
33
.vscode
4+
.temp
5+
.cache
46
/node_modules
57
/solution/result.json
68
/solution/contest.json

solution/1400-1499/1400.Construct K Palindrome Strings/README.md

+56-29
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:计数**
71+
72+
我们先判断字符串 $s$ 的长度是否小于 $k$,如果是,那么一定无法构造出 $k$ 个回文串,可以直接返回 `false`
73+
74+
否则,我们用一个哈希表或数组 $cnt$ 统计字符串 $s$ 中每个字符出现的次数。最后,我们只需要统计 $cnt$ 中奇数次数的字符个数 $x$,如果 $x$ 大于 $k$,那么一定无法构造出 $k$ 个回文串,返回 `false`;否则,返回 `true`
75+
76+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度;而 $C$ 是字符集大小,这里 $C=26$。
77+
7078
<!-- tabs:start -->
7179

7280
### **Python3**
@@ -78,9 +86,8 @@ class Solution:
7886
def canConstruct(self, s: str, k: int) -> bool:
7987
if len(s) < k:
8088
return False
81-
counter = Counter(s)
82-
cnt = sum(1 for n in counter.values() if n % 2 == 1)
83-
return cnt <= k
89+
cnt = Counter(s)
90+
return sum(v & 1 for v in cnt.values()) <= k
8491
```
8592

8693
### **Java**
@@ -90,20 +97,19 @@ class Solution:
9097
```java
9198
class Solution {
9299
public boolean canConstruct(String s, int k) {
93-
if (s.length() < k) {
100+
int n = s.length();
101+
if (n < k) {
94102
return false;
95103
}
96-
int[] counter = new int[26];
97-
for (char c : s.toCharArray()) {
98-
++counter[c - 'a'];
104+
int[] cnt = new int[26];
105+
for (int i = 0; i < n; ++i) {
106+
++cnt[s.charAt(i) - 'a'];
99107
}
100-
int cnt = 0;
101-
for (int v : counter) {
102-
if (v % 2 == 1) {
103-
++cnt;
104-
}
108+
int x = 0;
109+
for (int v : cnt) {
110+
x += v & 1;
105111
}
106-
return cnt <= k;
112+
return x <= k;
107113
}
108114
}
109115
```
@@ -114,14 +120,18 @@ class Solution {
114120
class Solution {
115121
public:
116122
bool canConstruct(string s, int k) {
117-
if (s.size() < k) return 0;
118-
vector<int> counter(26);
119-
for (char c : s) ++counter[c - 'a'];
120-
int cnt = 0;
121-
for (int v : counter)
122-
if (v % 2)
123-
++cnt;
124-
return cnt <= k;
123+
if (s.size() < k) {
124+
return false;
125+
}
126+
int cnt[26]{};
127+
for (char& c : s) {
128+
++cnt[c - 'a'];
129+
}
130+
int x = 0;
131+
for (int v : cnt) {
132+
x += v & 1;
133+
}
134+
return x <= k;
125135
}
126136
};
127137
```
@@ -133,17 +143,34 @@ func canConstruct(s string, k int) bool {
133143
if len(s) < k {
134144
return false
135145
}
136-
counter := make([]int, 26)
146+
cnt := [26]int{}
137147
for _, c := range s {
138-
counter[c-'a']++
148+
cnt[c-'a']++
139149
}
140-
cnt := 0
141-
for _, v := range counter {
142-
if v%2 == 1 {
143-
cnt++
144-
}
150+
x := 0
151+
for _, v := range cnt {
152+
x += v & 1
145153
}
146-
return cnt <= k
154+
return x <= k
155+
}
156+
```
157+
158+
### **TypeScript**
159+
160+
```ts
161+
function canConstruct(s: string, k: number): boolean {
162+
if (s.length < k) {
163+
return false;
164+
}
165+
const cnt: number[] = new Array(26).fill(0);
166+
for (const c of s) {
167+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
168+
}
169+
let x = 0;
170+
for (const v of cnt) {
171+
x += v & 1;
172+
}
173+
return x <= k;
147174
}
148175
```
149176

solution/1400-1499/1400.Construct K Palindrome Strings/README_EN.md

+48-29
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,28 @@ class Solution:
5252
def canConstruct(self, s: str, k: int) -> bool:
5353
if len(s) < k:
5454
return False
55-
counter = Counter(s)
56-
cnt = sum(1 for n in counter.values() if n % 2 == 1)
57-
return cnt <= k
55+
cnt = Counter(s)
56+
return sum(v & 1 for v in cnt.values()) <= k
5857
```
5958

6059
### **Java**
6160

6261
```java
6362
class Solution {
6463
public boolean canConstruct(String s, int k) {
65-
if (s.length() < k) {
64+
int n = s.length();
65+
if (n < k) {
6666
return false;
6767
}
68-
int[] counter = new int[26];
69-
for (char c : s.toCharArray()) {
70-
++counter[c - 'a'];
68+
int[] cnt = new int[26];
69+
for (int i = 0; i < n; ++i) {
70+
++cnt[s.charAt(i) - 'a'];
7171
}
72-
int cnt = 0;
73-
for (int v : counter) {
74-
if (v % 2 == 1) {
75-
++cnt;
76-
}
72+
int x = 0;
73+
for (int v : cnt) {
74+
x += v & 1;
7775
}
78-
return cnt <= k;
76+
return x <= k;
7977
}
8078
}
8179
```
@@ -86,14 +84,18 @@ class Solution {
8684
class Solution {
8785
public:
8886
bool canConstruct(string s, int k) {
89-
if (s.size() < k) return 0;
90-
vector<int> counter(26);
91-
for (char c : s) ++counter[c - 'a'];
92-
int cnt = 0;
93-
for (int v : counter)
94-
if (v % 2)
95-
++cnt;
96-
return cnt <= k;
87+
if (s.size() < k) {
88+
return false;
89+
}
90+
int cnt[26]{};
91+
for (char& c : s) {
92+
++cnt[c - 'a'];
93+
}
94+
int x = 0;
95+
for (int v : cnt) {
96+
x += v & 1;
97+
}
98+
return x <= k;
9799
}
98100
};
99101
```
@@ -105,17 +107,34 @@ func canConstruct(s string, k int) bool {
105107
if len(s) < k {
106108
return false
107109
}
108-
counter := make([]int, 26)
110+
cnt := [26]int{}
109111
for _, c := range s {
110-
counter[c-'a']++
112+
cnt[c-'a']++
111113
}
112-
cnt := 0
113-
for _, v := range counter {
114-
if v%2 == 1 {
115-
cnt++
116-
}
114+
x := 0
115+
for _, v := range cnt {
116+
x += v & 1
117117
}
118-
return cnt <= k
118+
return x <= k
119+
}
120+
```
121+
122+
### **TypeScript**
123+
124+
```ts
125+
function canConstruct(s: string, k: number): boolean {
126+
if (s.length < k) {
127+
return false;
128+
}
129+
const cnt: number[] = new Array(26).fill(0);
130+
for (const c of s) {
131+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
132+
}
133+
let x = 0;
134+
for (const v of cnt) {
135+
x += v & 1;
136+
}
137+
return x <= k;
119138
}
120139
```
121140

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
class Solution {
22
public:
33
bool canConstruct(string s, int k) {
4-
if (s.size() < k) return 0;
5-
vector<int> counter(26);
6-
for (char c : s) ++counter[c - 'a'];
7-
int cnt = 0;
8-
for (int v : counter)
9-
if (v % 2)
10-
++cnt;
11-
return cnt <= k;
4+
if (s.size() < k) {
5+
return false;
6+
}
7+
int cnt[26]{};
8+
for (char& c : s) {
9+
++cnt[c - 'a'];
10+
}
11+
int x = 0;
12+
for (int v : cnt) {
13+
x += v & 1;
14+
}
15+
return x <= k;
1216
}
1317
};

solution/1400-1499/1400.Construct K Palindrome Strings/Solution.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ func canConstruct(s string, k int) bool {
22
if len(s) < k {
33
return false
44
}
5-
counter := make([]int, 26)
5+
cnt := [26]int{}
66
for _, c := range s {
7-
counter[c-'a']++
7+
cnt[c-'a']++
88
}
9-
cnt := 0
10-
for _, v := range counter {
11-
if v%2 == 1 {
12-
cnt++
13-
}
9+
x := 0
10+
for _, v := range cnt {
11+
x += v & 1
1412
}
15-
return cnt <= k
13+
return x <= k
1614
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
22
public boolean canConstruct(String s, int k) {
3-
if (s.length() < k) {
3+
int n = s.length();
4+
if (n < k) {
45
return false;
56
}
6-
int[] counter = new int[26];
7-
for (char c : s.toCharArray()) {
8-
++counter[c - 'a'];
7+
int[] cnt = new int[26];
8+
for (int i = 0; i < n; ++i) {
9+
++cnt[s.charAt(i) - 'a'];
910
}
10-
int cnt = 0;
11-
for (int v : counter) {
12-
if (v % 2 == 1) {
13-
++cnt;
14-
}
11+
int x = 0;
12+
for (int v : cnt) {
13+
x += v & 1;
1514
}
16-
return cnt <= k;
15+
return x <= k;
1716
}
1817
}

solution/1400-1499/1400.Construct K Palindrome Strings/Solution.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ class Solution:
22
def canConstruct(self, s: str, k: int) -> bool:
33
if len(s) < k:
44
return False
5-
counter = Counter(s)
6-
cnt = sum(1 for n in counter.values() if n % 2 == 1)
7-
return cnt <= k
5+
cnt = Counter(s)
6+
return sum(v & 1 for v in cnt.values()) <= k
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function canConstruct(s: string, k: number): boolean {
2+
if (s.length < k) {
3+
return false;
4+
}
5+
const cnt: number[] = new Array(26).fill(0);
6+
for (const c of s) {
7+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
8+
}
9+
let x = 0;
10+
for (const v of cnt) {
11+
x += v & 1;
12+
}
13+
return x <= k;
14+
}

solution/2600-2699/2647.Color the Triangle Red/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222

2323
<ol>
2424
<li>选择一个 <strong>至少有两个</strong> 红色相邻三角形的白色三角形。
25-
2625
<ul>
2726
<li>如果没有这样的三角形,请停止算法。</li>
2827
</ul>
2928
</li>
3029
<li>将该三角形涂成 <strong>红色</strong> 。</li>
3130
<li>回到步骤 1。</li>
32-
3331
</ol>
3432

3533
<p>选择最小的 <code>k</code> 并在运行此算法之前将 <code>k</code> 个三角形涂成红色,使得在算法停止后,所有单元三角形都被涂成红色。</p>

solution/2600-2699/2677.Chunk Array/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,21 @@ function chunk(arr: any[], size: number): any[][] {
7373
}
7474
```
7575

76+
### **JavaScript**
77+
78+
```js
79+
/**
80+
* @param {Array} arr
81+
* @param {number} size
82+
* @return {Array[]}
83+
*/
84+
var chunk = function (arr, size) {
85+
const ans = [];
86+
for (let i = 0, n = arr.length; i < n; i += size) {
87+
ans.push(arr.slice(i, i + size));
88+
}
89+
return ans;
90+
};
91+
```
92+
7693
<!-- tabs:end -->

0 commit comments

Comments
 (0)