Skip to content

Commit da2ffb8

Browse files
authoredOct 20, 2024
feat: add solutions to lc problem: No.3325 (doocs#3654)
1 parent a377858 commit da2ffb8

File tree

9 files changed

+243
-14
lines changed

9 files changed

+243
-14
lines changed
 

‎solution/0900-0999/0908.Smallest Range I/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ tags:
6969

7070
### 方法一:数学
7171

72-
根据题目描述,我们可以将数组中的最大值加上 $k$,最小值减去 $k$,这样可以使得数组中的最大值和最小值之差变小。
72+
根据题目描述,我们可以将数组中的最大值减去 $k$,最小值加上 $k$,这样可以使得数组中的最大值和最小值之差变小。
7373

74-
因此,最终的答案就是 $\max(nums) - \min(nums) - 2 \times k$。
74+
因此,最终的答案就是 $\max(\textit{nums}) - \min(\textit{nums}) - 2 \times k$ 和 $0$ 之间的较大值
7575

76-
时间复杂度 $O(n)$,其中 $n$ 为数组 `nums` 的长度。空间复杂度 $O(1)$。
76+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
7777

7878
<!-- tabs:start -->
7979

‎solution/0900-0999/0908.Smallest Range I/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ tags:
6767

6868
### Solution 1: Mathematics
6969

70-
According to the problem description, we can add $k$ to the maximum value in the array and subtract $k$ from the minimum value. This can reduce the difference between the maximum and minimum values in the array.
70+
According to the problem description, we can subtract $k$ from the maximum value in the array and add $k$ to the minimum value in the array, which can reduce the difference between the maximum and minimum values in the array.
7171

72-
Therefore, the final answer is $\max(nums) - \min(nums) - 2 \times k$.
72+
Therefore, the final answer is the larger value between $\max(\textit{nums}) - \min(\textit{nums}) - 2 \times k$ and $0$.
7373

74-
The time complexity is $O(n)$, where $n$ is the length of the array `nums`. The space complexity is $O(1)$.
74+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
7575

7676
<!-- tabs:start -->
7777

‎solution/3300-3399/3325.Count Substrings With K-Frequency Characters I/README.md

+84-4
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,112 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3325.Co
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:滑动窗口
71+
72+
我们可以枚举子字符串的右端点,然后用一个滑动窗口维护子字符串的左端点,使得滑动窗口内的子字符串中的每个字符出现次数都小于 $k$。
73+
74+
我们可以用一个数组 $\textit{cnt}$ 维护滑动窗口内的每个字符的出现次数,然后用一个变量 $\textit{l}$ 维护滑动窗口的左端点,用一个变量 $\textit{ans}$ 维护答案。
75+
76+
当我们枚举右端点时,我们可以将右端点的字符加入滑动窗口,然后判断滑动窗口内右端点的字符出现次数是否大于等于 $k$,如果是,则将左端点的字符移出滑动窗口,直到滑动窗口内的每个字符出现次数都小于 $k$。此时,对于左端点为 $[0, ..l - 1]$,且右端点为 $r$ 的子字符串,都满足题目要求,因此答案加上 $l$。
77+
78+
枚举结束后,返回答案即可。
79+
80+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,这里是小写字母集合,因此 $|\Sigma| = 26$。
7181

7282
<!-- tabs:start -->
7383

7484
#### Python3
7585

7686
```python
77-
87+
class Solution:
88+
def numberOfSubstrings(self, s: str, k: int) -> int:
89+
cnt = Counter()
90+
ans = l = 0
91+
for c in s:
92+
cnt[c] += 1
93+
while cnt[c] >= k:
94+
cnt[s[l]] -= 1
95+
l += 1
96+
ans += l
97+
return ans
7898
```
7999

80100
#### Java
81101

82102
```java
83-
103+
class Solution {
104+
public int numberOfSubstrings(String s, int k) {
105+
int[] cnt = new int[26];
106+
int ans = 0, l = 0;
107+
for (int r = 0; r < s.length(); ++r) {
108+
int c = s.charAt(r) - 'a';
109+
++cnt[c];
110+
while (cnt[c] >= k) {
111+
--cnt[s.charAt(l) - 'a'];
112+
l++;
113+
}
114+
ans += l;
115+
}
116+
return ans;
117+
}
118+
}
84119
```
85120

86121
#### C++
87122

88123
```cpp
89-
124+
class Solution {
125+
public:
126+
int numberOfSubstrings(string s, int k) {
127+
int n = s.size();
128+
int ans = 0, l = 0;
129+
int cnt[26]{};
130+
for (char& c : s) {
131+
++cnt[c - 'a'];
132+
while (cnt[c - 'a'] >= k) {
133+
--cnt[s[l++] - 'a'];
134+
}
135+
ans += l;
136+
}
137+
return ans;
138+
}
139+
};
90140
```
91141
92142
#### Go
93143
94144
```go
145+
func numberOfSubstrings(s string, k int) (ans int) {
146+
l := 0
147+
cnt := [26]int{}
148+
for _, c := range s {
149+
cnt[c-'a']++
150+
for cnt[c-'a'] >= k {
151+
cnt[s[l]-'a']--
152+
l++
153+
}
154+
ans += l
155+
}
156+
return
157+
}
158+
```
95159

160+
#### TypeScript
161+
162+
```ts
163+
function numberOfSubstrings(s: string, k: number): number {
164+
let [ans, l] = [0, 0];
165+
const cnt: number[] = Array(26).fill(0);
166+
for (const c of s) {
167+
const x = c.charCodeAt(0) - 'a'.charCodeAt(0);
168+
++cnt[x];
169+
while (cnt[x] >= k) {
170+
--cnt[s[l++].charCodeAt(0) - 'a'.charCodeAt(0)];
171+
}
172+
ans += l;
173+
}
174+
return ans;
175+
}
96176
```
97177

98178
<!-- tabs:end -->

‎solution/3300-3399/3325.Count Substrings With K-Frequency Characters I/README_EN.md

+84-4
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,112 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3325.Co
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Sliding Window
69+
70+
We can enumerate the right endpoint of the substring, and then use a sliding window to maintain the left endpoint of the substring, ensuring that the occurrence count of each character in the sliding window is less than $k$.
71+
72+
We can use an array $\textit{cnt}$ to maintain the occurrence count of each character in the sliding window, then use a variable $\textit{l}$ to maintain the left endpoint of the sliding window, and use a variable $\textit{ans}$ to maintain the answer.
73+
74+
When we enumerate the right endpoint, we can add the character at the right endpoint to the sliding window, then check if the occurrence count of the character at the right endpoint in the sliding window is greater than or equal to $k$. If it is, we remove the character at the left endpoint from the sliding window until the occurrence count of each character in the sliding window is less than $k$. At this point, for substrings with left endpoints in the range $[0, ..l - 1]$ and right endpoint $r$, all satisfy the problem's requirements, so we add $l$ to the answer.
75+
76+
After enumeration, we return the answer.
77+
78+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set, which in this case is the set of lowercase letters, so $|\Sigma| = 26$.
6979

7080
<!-- tabs:start -->
7181

7282
#### Python3
7383

7484
```python
75-
85+
class Solution:
86+
def numberOfSubstrings(self, s: str, k: int) -> int:
87+
cnt = Counter()
88+
ans = l = 0
89+
for c in s:
90+
cnt[c] += 1
91+
while cnt[c] >= k:
92+
cnt[s[l]] -= 1
93+
l += 1
94+
ans += l
95+
return ans
7696
```
7797

7898
#### Java
7999

80100
```java
81-
101+
class Solution {
102+
public int numberOfSubstrings(String s, int k) {
103+
int[] cnt = new int[26];
104+
int ans = 0, l = 0;
105+
for (int r = 0; r < s.length(); ++r) {
106+
int c = s.charAt(r) - 'a';
107+
++cnt[c];
108+
while (cnt[c] >= k) {
109+
--cnt[s.charAt(l) - 'a'];
110+
l++;
111+
}
112+
ans += l;
113+
}
114+
return ans;
115+
}
116+
}
82117
```
83118

84119
#### C++
85120

86121
```cpp
87-
122+
class Solution {
123+
public:
124+
int numberOfSubstrings(string s, int k) {
125+
int n = s.size();
126+
int ans = 0, l = 0;
127+
int cnt[26]{};
128+
for (char& c : s) {
129+
++cnt[c - 'a'];
130+
while (cnt[c - 'a'] >= k) {
131+
--cnt[s[l++] - 'a'];
132+
}
133+
ans += l;
134+
}
135+
return ans;
136+
}
137+
};
88138
```
89139
90140
#### Go
91141
92142
```go
143+
func numberOfSubstrings(s string, k int) (ans int) {
144+
l := 0
145+
cnt := [26]int{}
146+
for _, c := range s {
147+
cnt[c-'a']++
148+
for cnt[c-'a'] >= k {
149+
cnt[s[l]-'a']--
150+
l++
151+
}
152+
ans += l
153+
}
154+
return
155+
}
156+
```
93157

158+
#### TypeScript
159+
160+
```ts
161+
function numberOfSubstrings(s: string, k: number): number {
162+
let [ans, l] = [0, 0];
163+
const cnt: number[] = Array(26).fill(0);
164+
for (const c of s) {
165+
const x = c.charCodeAt(0) - 'a'.charCodeAt(0);
166+
++cnt[x];
167+
while (cnt[x] >= k) {
168+
--cnt[s[l++].charCodeAt(0) - 'a'.charCodeAt(0)];
169+
}
170+
ans += l;
171+
}
172+
return ans;
173+
}
94174
```
95175

96176
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int numberOfSubstrings(string s, int k) {
4+
int n = s.size();
5+
int ans = 0, l = 0;
6+
int cnt[26]{};
7+
for (char& c : s) {
8+
++cnt[c - 'a'];
9+
while (cnt[c - 'a'] >= k) {
10+
--cnt[s[l++] - 'a'];
11+
}
12+
ans += l;
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func numberOfSubstrings(s string, k int) (ans int) {
2+
l := 0
3+
cnt := [26]int{}
4+
for _, c := range s {
5+
cnt[c-'a']++
6+
for cnt[c-'a'] >= k {
7+
cnt[s[l]-'a']--
8+
l++
9+
}
10+
ans += l
11+
}
12+
return
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int numberOfSubstrings(String s, int k) {
3+
int[] cnt = new int[26];
4+
int ans = 0, l = 0;
5+
for (int r = 0; r < s.length(); ++r) {
6+
int c = s.charAt(r) - 'a';
7+
++cnt[c];
8+
while (cnt[c] >= k) {
9+
--cnt[s.charAt(l) - 'a'];
10+
l++;
11+
}
12+
ans += l;
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def numberOfSubstrings(self, s: str, k: int) -> int:
3+
cnt = Counter()
4+
ans = l = 0
5+
for c in s:
6+
cnt[c] += 1
7+
while cnt[c] >= k:
8+
cnt[s[l]] -= 1
9+
l += 1
10+
ans += l
11+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function numberOfSubstrings(s: string, k: number): number {
2+
let [ans, l] = [0, 0];
3+
const cnt: number[] = Array(26).fill(0);
4+
for (const c of s) {
5+
const x = c.charCodeAt(0) - 'a'.charCodeAt(0);
6+
++cnt[x];
7+
while (cnt[x] >= k) {
8+
--cnt[s[l++].charCodeAt(0) - 'a'.charCodeAt(0)];
9+
}
10+
ans += l;
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)
Please sign in to comment.