Skip to content

feat: add solutions to lc problem: No.424 #3029

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

Merged
merged 1 commit into from
Jun 4, 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 @@ -59,7 +59,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:双指针

我们使用一个哈希表 `cnt` 统计字符串中每个字符出现的次数,使用双指针 `l` 和 `r` 维护一个滑动窗口,使得窗口的大小减去出现次数最多的字符的次数,结果不超过 $k$。

我们遍历字符串,每次更新窗口的右边界 `r`,并更新窗口内的字符出现次数,同时更新出现过的字符的最大出现次数 `mx`。当窗口的大小减去 `mx` 大于 $k$ 时,我们需要缩小窗口的左边界 `l`,同时更新窗口内的字符出现次数,直到窗口的大小减去 `mx` 不大于 $k$。

最后,答案为 $n - l$,其中 $n$ 为字符串的长度。

时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 为字符串的长度,而 $|\Sigma|$ 为字符集的大小,本题中字符集为大写英文字母,所以 $|\Sigma| = 26$。

<!-- tabs:start -->

Expand All @@ -68,36 +76,32 @@ tags:
```python
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
counter = [0] * 26
i = j = maxCnt = 0
while i < len(s):
counter[ord(s[i]) - ord('A')] += 1
maxCnt = max(maxCnt, counter[ord(s[i]) - ord('A')])
if i - j + 1 > maxCnt + k:
counter[ord(s[j]) - ord('A')] -= 1
j += 1
i += 1
return i - j
cnt = Counter()
l = mx = 0
for r, c in enumerate(s):
cnt[c] += 1
mx = max(mx, cnt[c])
if r - l + 1 - mx > k:
cnt[s[l]] -= 1
l += 1
return len(s) - l
```

#### Java

```java
class Solution {
public int characterReplacement(String s, int k) {
int[] counter = new int[26];
int i = 0;
int j = 0;
for (int maxCnt = 0; i < s.length(); ++i) {
char c = s.charAt(i);
++counter[c - 'A'];
maxCnt = Math.max(maxCnt, counter[c - 'A']);
if (i - j + 1 - maxCnt > k) {
--counter[s.charAt(j) - 'A'];
++j;
int[] cnt = new int[26];
int l = 0, mx = 0;
int n = s.length();
for (int r = 0; r < n; ++r) {
mx = Math.max(mx, ++cnt[s.charAt(r) - 'A']);
if (r - l + 1 - mx > k) {
--cnt[s.charAt(l++) - 'A'];
}
}
return i - j;
return n - l;
}
}
```
Expand All @@ -108,18 +112,16 @@ class Solution {
class Solution {
public:
int characterReplacement(string s, int k) {
vector<int> counter(26);
int i = 0, j = 0, maxCnt = 0;
for (char& c : s) {
++counter[c - 'A'];
maxCnt = max(maxCnt, counter[c - 'A']);
if (i - j + 1 > maxCnt + k) {
--counter[s[j] - 'A'];
++j;
int cnt[26]{};
int l = 0, mx = 0;
int n = s.length();
for (int r = 0; r < n; ++r) {
mx = max(mx, ++cnt[s[r] - 'A']);
if (r - l + 1 - mx > k) {
--cnt[s[l++] - 'A'];
}
++i;
}
return i - j;
return n - l;
}
};
```
Expand All @@ -128,20 +130,35 @@ public:

```go
func characterReplacement(s string, k int) int {
counter := make([]int, 26)
j, maxCnt := 0, 0
for i := range s {
c := s[i] - 'A'
counter[c]++
if maxCnt < counter[c] {
maxCnt = counter[c]
}
if i-j+1 > maxCnt+k {
counter[s[j]-'A']--
j++
cnt := [26]int{}
l, mx := 0, 0
for r, c := range s {
cnt[c-'A']++
mx = max(mx, cnt[c-'A'])
if r-l+1-mx > k {
cnt[s[l]-'A']--
l++
}
}
return len(s) - j
return len(s) - l
}
```

#### TypeScript

```ts
function characterReplacement(s: string, k: number): number {
const idx = (c: string) => c.charCodeAt(0) - 65;
const cnt: number[] = Array(26).fill(0);
const n = s.length;
let [l, mx] = [0, 0];
for (let r = 0; r < n; ++r) {
mx = Math.max(mx, ++cnt[idx(s[r])]);
if (r - l + 1 - mx > k) {
--cnt[idx(s[l++])];
}
}
return n - l;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ There may exists other ways to achieve this answer too.</pre>

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

We use a hash table `cnt` to count the occurrence of each character in the string, and two pointers `l` and `r` to maintain a sliding window, such that the size of the window minus the count of the most frequent character does not exceed $k$.

We iterate through the string, updating the right boundary `r` of the window each time, updating the count of characters within the window, and updating the maximum count `mx` of the characters that have appeared. When the size of the window minus `mx` is greater than $k$, we need to shrink the left boundary `l` of the window, updating the count of characters within the window, until the size of the window minus `mx` is no longer greater than $k$.

Finally, the answer is $n - l$, where $n$ is the length of the string.

The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string, and $|\Sigma|$ is the size of the character set. In this problem, the character set is uppercase English letters, so $|\Sigma| = 26$.

<!-- tabs:start -->

Expand All @@ -64,36 +72,32 @@ There may exists other ways to achieve this answer too.</pre>
```python
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
counter = [0] * 26
i = j = maxCnt = 0
while i < len(s):
counter[ord(s[i]) - ord('A')] += 1
maxCnt = max(maxCnt, counter[ord(s[i]) - ord('A')])
if i - j + 1 > maxCnt + k:
counter[ord(s[j]) - ord('A')] -= 1
j += 1
i += 1
return i - j
cnt = Counter()
l = mx = 0
for r, c in enumerate(s):
cnt[c] += 1
mx = max(mx, cnt[c])
if r - l + 1 - mx > k:
cnt[s[l]] -= 1
l += 1
return len(s) - l
```

#### Java

```java
class Solution {
public int characterReplacement(String s, int k) {
int[] counter = new int[26];
int i = 0;
int j = 0;
for (int maxCnt = 0; i < s.length(); ++i) {
char c = s.charAt(i);
++counter[c - 'A'];
maxCnt = Math.max(maxCnt, counter[c - 'A']);
if (i - j + 1 - maxCnt > k) {
--counter[s.charAt(j) - 'A'];
++j;
int[] cnt = new int[26];
int l = 0, mx = 0;
int n = s.length();
for (int r = 0; r < n; ++r) {
mx = Math.max(mx, ++cnt[s.charAt(r) - 'A']);
if (r - l + 1 - mx > k) {
--cnt[s.charAt(l++) - 'A'];
}
}
return i - j;
return n - l;
}
}
```
Expand All @@ -104,18 +108,16 @@ class Solution {
class Solution {
public:
int characterReplacement(string s, int k) {
vector<int> counter(26);
int i = 0, j = 0, maxCnt = 0;
for (char& c : s) {
++counter[c - 'A'];
maxCnt = max(maxCnt, counter[c - 'A']);
if (i - j + 1 > maxCnt + k) {
--counter[s[j] - 'A'];
++j;
int cnt[26]{};
int l = 0, mx = 0;
int n = s.length();
for (int r = 0; r < n; ++r) {
mx = max(mx, ++cnt[s[r] - 'A']);
if (r - l + 1 - mx > k) {
--cnt[s[l++] - 'A'];
}
++i;
}
return i - j;
return n - l;
}
};
```
Expand All @@ -124,20 +126,35 @@ public:

```go
func characterReplacement(s string, k int) int {
counter := make([]int, 26)
j, maxCnt := 0, 0
for i := range s {
c := s[i] - 'A'
counter[c]++
if maxCnt < counter[c] {
maxCnt = counter[c]
}
if i-j+1 > maxCnt+k {
counter[s[j]-'A']--
j++
cnt := [26]int{}
l, mx := 0, 0
for r, c := range s {
cnt[c-'A']++
mx = max(mx, cnt[c-'A'])
if r-l+1-mx > k {
cnt[s[l]-'A']--
l++
}
}
return len(s) - j
return len(s) - l
}
```

#### TypeScript

```ts
function characterReplacement(s: string, k: number): number {
const idx = (c: string) => c.charCodeAt(0) - 65;
const cnt: number[] = Array(26).fill(0);
const n = s.length;
let [l, mx] = [0, 0];
for (let r = 0; r < n; ++r) {
mx = Math.max(mx, ++cnt[idx(s[r])]);
if (r - l + 1 - mx > k) {
--cnt[idx(s[l++])];
}
}
return n - l;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
class Solution {
public:
int characterReplacement(string s, int k) {
vector<int> counter(26);
int i = 0, j = 0, maxCnt = 0;
for (char& c : s) {
++counter[c - 'A'];
maxCnt = max(maxCnt, counter[c - 'A']);
if (i - j + 1 > maxCnt + k) {
--counter[s[j] - 'A'];
++j;
int cnt[26]{};
int l = 0, mx = 0;
int n = s.length();
for (int r = 0; r < n; ++r) {
mx = max(mx, ++cnt[s[r] - 'A']);
if (r - l + 1 - mx > k) {
--cnt[s[l++] - 'A'];
}
++i;
}
return i - j;
return n - l;
}
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
func characterReplacement(s string, k int) int {
counter := make([]int, 26)
j, maxCnt := 0, 0
for i := range s {
c := s[i] - 'A'
counter[c]++
if maxCnt < counter[c] {
maxCnt = counter[c]
}
if i-j+1 > maxCnt+k {
counter[s[j]-'A']--
j++
cnt := [26]int{}
l, mx := 0, 0
for r, c := range s {
cnt[c-'A']++
mx = max(mx, cnt[c-'A'])
if r-l+1-mx > k {
cnt[s[l]-'A']--
l++
}
}
return len(s) - j
return len(s) - l
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
class Solution {
public int characterReplacement(String s, int k) {
int[] counter = new int[26];
int i = 0;
int j = 0;
for (int maxCnt = 0; i < s.length(); ++i) {
char c = s.charAt(i);
++counter[c - 'A'];
maxCnt = Math.max(maxCnt, counter[c - 'A']);
if (i - j + 1 - maxCnt > k) {
--counter[s.charAt(j) - 'A'];
++j;
int[] cnt = new int[26];
int l = 0, mx = 0;
int n = s.length();
for (int r = 0; r < n; ++r) {
mx = Math.max(mx, ++cnt[s.charAt(r) - 'A']);
if (r - l + 1 - mx > k) {
--cnt[s.charAt(l++) - 'A'];
}
}
return i - j;
return n - l;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
counter = [0] * 26
i = j = maxCnt = 0
while i < len(s):
counter[ord(s[i]) - ord('A')] += 1
maxCnt = max(maxCnt, counter[ord(s[i]) - ord('A')])
if i - j + 1 > maxCnt + k:
counter[ord(s[j]) - ord('A')] -= 1
j += 1
i += 1
return i - j
cnt = Counter()
l = mx = 0
for r, c in enumerate(s):
cnt[c] += 1
mx = max(mx, cnt[c])
if r - l + 1 - mx > k:
cnt[s[l]] -= 1
l += 1
return len(s) - l
Loading
Loading