Skip to content

Commit d80c1cb

Browse files
committed
feat: add solutions to lc problem: No.2516
No.2516.Take K of Each Character From Left and Right
1 parent 072ca8e commit d80c1cb

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

solution/2500-2599/2516.Take K of Each Character From Left and Right/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,61 @@ func max(a, b int) int {
165165
}
166166
```
167167

168+
### **TypeScript**
169+
170+
```ts
171+
function takeCharacters(s: string, k: number): number {
172+
const getIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
173+
const count = [0, 0, 0];
174+
for (const c of s) {
175+
count[getIndex(c)]++;
176+
}
177+
if (count.some(v => v < k)) {
178+
return -1;
179+
}
180+
const n = s.length;
181+
let ans = 0;
182+
for (let i = 0, j = 0; j < n; j++) {
183+
count[getIndex(s[j])]--;
184+
while (count[getIndex(s[j])] < k) {
185+
count[getIndex(s[i])]++;
186+
i++;
187+
}
188+
ans = Math.max(ans, j - i + 1);
189+
}
190+
return n - ans;
191+
}
192+
```
193+
194+
### **Rust**
195+
196+
```rust
197+
impl Solution {
198+
pub fn take_characters(s: String, k: i32) -> i32 {
199+
let s = s.as_bytes();
200+
let mut count = vec![0; 3];
201+
for c in s.iter() {
202+
count[(c - b'a') as usize] += 1;
203+
}
204+
if count.iter().any(|v| *v < k) {
205+
return -1;
206+
}
207+
let n = s.len();
208+
let mut ans = 0;
209+
let mut i = 0;
210+
for j in 0..n {
211+
count[(s[j] - b'a') as usize] -= 1;
212+
while count[(s[j] - b'a') as usize] < k {
213+
count[(s[i] - b'a') as usize] += 1;
214+
i += 1;
215+
}
216+
ans = ans.max(j - i + 1);
217+
}
218+
(n - ans) as i32
219+
}
220+
}
221+
```
222+
168223
### **...**
169224

170225
```

solution/2500-2599/2516.Take K of Each Character From Left and Right/README_EN.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,61 @@ func max(a, b int) int {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function takeCharacters(s: string, k: number): number {
150+
const getIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
151+
const count = [0, 0, 0];
152+
for (const c of s) {
153+
count[getIndex(c)]++;
154+
}
155+
if (count.some(v => v < k)) {
156+
return -1;
157+
}
158+
const n = s.length;
159+
let ans = 0;
160+
for (let i = 0, j = 0; j < n; j++) {
161+
count[getIndex(s[j])]--;
162+
while (count[getIndex(s[j])] < k) {
163+
count[getIndex(s[i])]++;
164+
i++;
165+
}
166+
ans = Math.max(ans, j - i + 1);
167+
}
168+
return n - ans;
169+
}
170+
```
171+
172+
### **Rust**
173+
174+
```rust
175+
impl Solution {
176+
pub fn take_characters(s: String, k: i32) -> i32 {
177+
let s = s.as_bytes();
178+
let mut count = vec![0; 3];
179+
for c in s.iter() {
180+
count[(c - b'a') as usize] += 1;
181+
}
182+
if count.iter().any(|v| *v < k) {
183+
return -1;
184+
}
185+
let n = s.len();
186+
let mut ans = 0;
187+
let mut i = 0;
188+
for j in 0..n {
189+
count[(s[j] - b'a') as usize] -= 1;
190+
while count[(s[j] - b'a') as usize] < k {
191+
count[(s[i] - b'a') as usize] += 1;
192+
i += 1;
193+
}
194+
ans = ans.max(j - i + 1);
195+
}
196+
(n - ans) as i32
197+
}
198+
}
199+
```
200+
146201
### **...**
147202

148203
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn take_characters(s: String, k: i32) -> i32 {
3+
let s = s.as_bytes();
4+
let mut count = vec![0; 3];
5+
for c in s.iter() {
6+
count[(c - b'a') as usize] += 1;
7+
}
8+
if count.iter().any(|v| *v < k) {
9+
return -1;
10+
}
11+
let n = s.len();
12+
let mut ans = 0;
13+
let mut i = 0;
14+
for j in 0..n {
15+
count[(s[j] - b'a') as usize] -= 1;
16+
while count[(s[j] - b'a') as usize] < k {
17+
count[(s[i] - b'a') as usize] += 1;
18+
i += 1;
19+
}
20+
ans = ans.max(j - i + 1);
21+
}
22+
(n - ans) as i32
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function takeCharacters(s: string, k: number): number {
2+
const getIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
3+
const count = [0, 0, 0];
4+
for (const c of s) {
5+
count[getIndex(c)]++;
6+
}
7+
if (count.some(v => v < k)) {
8+
return -1;
9+
}
10+
const n = s.length;
11+
let ans = 0;
12+
for (let i = 0, j = 0; j < n; j++) {
13+
count[getIndex(s[j])]--;
14+
while (count[getIndex(s[j])] < k) {
15+
count[getIndex(s[i])]++;
16+
i++;
17+
}
18+
ans = Math.max(ans, j - i + 1);
19+
}
20+
return n - ans;
21+
}

0 commit comments

Comments
 (0)