Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2516 #2581

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@

### 方法一:滑动窗口

我们先用哈希表或者一个长度为 $3$ 的数组 `cnt` 统计字符串 $s$ 中每个字符的个数,如果有字符的个数小于 $k$ 个,则无法取到,提前返回 $-1$。
我们先用哈希表或者一个长度为 $3$ 的数组 $cnt$ 统计字符串 $s$ 中每个字符的个数,如果有字符的个数小于 $k$ 个,则无法取到,提前返回 $-1$。

题目要我们在字符串左侧以及右侧取走字符,最终取到的每种字符的个数都不少于 $k$ 个。我们不妨反着考虑问题,取走中间某个窗口大小的字符串,使得剩下的两侧字符串中,每种字符的个数都不少于 $k$ 个。

因此,我们维护一个滑动窗口,用指针 $j$ 和 $i$ 分别表示窗口的左右边界,窗口内的字符串是我们要取走的。我们每一次移动右边界 $i$,将对应的字符 $s[i]$ 加入到窗口中(也即取走一个字符 $s[i]$),此时如果 $cnt[s[i]]$ 个数小于 $k$,那么我们循环移动左边界 $j$,直到 $cnt[s[i]]$ 个数不小于 $k$ 为止。此时的窗口大小为 $i - j + 1$,更新最大窗口。

最终的答案就是字符串 $s$ 的长度减去最大窗口的大小。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand All @@ -66,14 +66,14 @@ class Solution:
cnt = Counter(s)
if any(cnt[c] < k for c in "abc"):
return -1
ans = j = 0
mx = j = 0
for i, c in enumerate(s):
cnt[c] -= 1
while cnt[c] < k:
cnt[s[j]] += 1
j += 1
ans = max(ans, i - j + 1)
return len(s) - ans
mx = max(mx, i - j + 1)
return len(s) - mx
```

```java
Expand All @@ -84,19 +84,21 @@ class Solution {
for (int i = 0; i < n; ++i) {
++cnt[s.charAt(i) - 'a'];
}
if (cnt[0] < k || cnt[1] < k || cnt[2] < k) {
return -1;
for (int x : cnt) {
if (x < k) {
return -1;
}
}
int ans = 0, j = 0;
int mx = 0, j = 0;
for (int i = 0; i < n; ++i) {
int c = s.charAt(i) - 'a';
--cnt[c];
while (cnt[c] < k) {
++cnt[s.charAt(j++) - 'a'];
}
ans = Math.max(ans, i - j + 1);
mx = Math.max(mx, i - j + 1);
}
return n - ans;
return n - mx;
}
}
```
Expand All @@ -105,20 +107,26 @@ class Solution {
class Solution {
public:
int takeCharacters(string s, int k) {
int cnt[3] = {0};
for (char& c : s) ++cnt[c - 'a'];
if (cnt[0] < k || cnt[1] < k || cnt[2] < k) return -1;
int ans = 0, j = 0;
int n = s.size();
int cnt[3]{};
int n = s.length();
for (int i = 0; i < n; ++i) {
++cnt[s[i] - 'a'];
}
for (int x : cnt) {
if (x < k) {
return -1;
}
}
int mx = 0, j = 0;
for (int i = 0; i < n; ++i) {
int c = s[i] - 'a';
--cnt[c];
while (cnt[c] < k) {
++cnt[s[j++] - 'a'];
}
ans = max(ans, i - j + 1);
mx = max(mx, i - j + 1);
}
return n - ans;
return n - mx;
}
};
```
Expand All @@ -129,70 +137,74 @@ func takeCharacters(s string, k int) int {
for _, c := range s {
cnt[c-'a']++
}
if cnt[0] < k || cnt[1] < k || cnt[2] < k {
return -1
for _, x := range cnt {
if x < k {
return -1
}
}
ans, j := 0, 0
mx, j := 0, 0
for i, c := range s {
c -= 'a'
cnt[c]--
for cnt[c] < k {
for cnt[c]--; cnt[c] < k; j++ {
cnt[s[j]-'a']++
j++
}
ans = max(ans, i-j+1)
mx = max(mx, i-j+1)
}
return len(s) - ans
return len(s) - mx
}
```

```ts
function takeCharacters(s: string, k: number): number {
const getIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
const count = [0, 0, 0];
const idx = (c: string) => c.charCodeAt(0) - 97;
const cnt: number[] = Array(3).fill(0);
for (const c of s) {
count[getIndex(c)]++;
++cnt[idx(c)];
}
if (count.some(v => v < k)) {
if (cnt.some(v => v < k)) {
return -1;
}
const n = s.length;
let ans = 0;
for (let i = 0, j = 0; j < n; j++) {
count[getIndex(s[j])]--;
while (count[getIndex(s[j])] < k) {
count[getIndex(s[i])]++;
i++;
let [mx, j] = [0, 0];
for (let i = 0; i < n; ++i) {
const c = idx(s[i]);
--cnt[c];
while (cnt[c] < k) {
++cnt[idx(s[j++])];
}
ans = Math.max(ans, j - i + 1);
mx = Math.max(mx, i - j + 1);
}
return n - ans;
return n - mx;
}
```

```rust
use std::collections::HashMap;

impl Solution {
pub fn take_characters(s: String, k: i32) -> i32 {
let s = s.as_bytes();
let mut count = vec![0; 3];
for c in s.iter() {
count[(c - b'a') as usize] += 1;
let mut cnt: HashMap<char, i32> = HashMap::new();
for c in s.chars() {
*cnt.entry(c).or_insert(0) += 1;
}
if count.iter().any(|v| *v < k) {

if "abc".chars().any(|c| cnt.get(&c).unwrap_or(&0) < &k) {
return -1;
}
let n = s.len();
let mut ans = 0;
let mut i = 0;
for j in 0..n {
count[(s[j] - b'a') as usize] -= 1;
while count[(s[j] - b'a') as usize] < k {
count[(s[i] - b'a') as usize] += 1;
i += 1;

let mut mx = 0;
let mut j = 0;
let mut cs = s.chars().collect::<Vec<char>>();
for i in 0..cs.len() {
let c = cs[i];
*cnt.get_mut(&c).unwrap() -= 1;
while cnt.get(&c).unwrap() < &k {
*cnt.get_mut(&cs[j]).unwrap() += 1;
j += 1;
}
ans = ans.max(j - i + 1);
mx = mx.max(i - j + 1);
}
(n - ans) as i32
(cs.len() as i32) - (mx as i32)
}
}
```
Expand Down
Loading
Loading