Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2432d4

Browse files
feat: update solutions to lc problem: No.1456 (doocs#2554)
No.1456.Maximum Number of Vowels in a Substring of Given Length
1 parent 96852d1 commit a2432d4

File tree

8 files changed

+142
-154
lines changed

8 files changed

+142
-154
lines changed

‎solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md‎

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -64,43 +64,45 @@
6464

6565
### 方法一:滑动窗口
6666

67-
找出所有窗口为 $k$ 中的元音字母数目,并记录最大值
67+
我们首先统计前 $k$ 个字符中元音字母的个数,记为 $cnt,ドル初始化答案 $ans$ 为 $cnt$
6868

69-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。
69+
然后我们从 $k$ 开始遍历字符串,每次遍历时,我们将当前字符加入窗口,如果当前字符是元音字母,则 $cnt$ 加一;将窗口第一个字符移出窗口,如果移除的字符是元音字母,则 $cnt$ 减一。然后,我们更新答案 $ans = \max(ans, cnt)$。
70+
71+
遍历结束后,返回答案即可。
72+
73+
时间复杂度 $O(n),ドル其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
7074

7175
<!-- tabs:start -->
7276

7377
```python
7478
class Solution:
7579
def maxVowels(self, s: str, k: int) -> int:
76-
vowels = set('aeiou')
77-
t = sum(c in vowels for c in s[:k])
78-
ans = t
80+
vowels = set("aeiou")
81+
ans = cnt = sum(c in vowels for c in s[:k])
7982
for i in range(k, len(s)):
80-
t += s[i] in vowels
81-
t -= s[i - k] in vowels
82-
ans = max(ans, t)
83+
cnt += int(s[i] in vowels) - int(s[i - k] in vowels)
84+
ans = max(ans, cnt)
8385
return ans
8486
```
8587

8688
```java
8789
class Solution {
8890
public int maxVowels(String s, int k) {
89-
int t = 0, n = s.length();
91+
int cnt = 0;
9092
for (int i = 0; i < k; ++i) {
9193
if (isVowel(s.charAt(i))) {
92-
++t;
94+
++cnt;
9395
}
9496
}
95-
int ans = t;
96-
for (int i = k; i < n; ++i) {
97+
int ans = cnt;
98+
for (int i = k; i < s.length(); ++i) {
9799
if (isVowel(s.charAt(i))) {
98-
++t;
100+
++cnt;
99101
}
100102
if (isVowel(s.charAt(i - k))) {
101-
--t;
103+
--cnt;
102104
}
103-
ans = Math.max(ans, t);
105+
ans = Math.max(ans, cnt);
104106
}
105107
return ans;
106108
}
@@ -115,20 +117,17 @@ class Solution {
115117
class Solution {
116118
public:
117119
int maxVowels(string s, int k) {
118-
int t = 0, n = s.size();
119-
for (int i = 0; i < k; ++i) t += isVowel(s[i]);
120-
int ans = t;
121-
for (int i = k; i < n; ++i) {
122-
t += isVowel(s[i]);
123-
t -= isVowel(s[i - k]);
124-
ans = max(ans, t);
120+
auto isVowel = [](char c) {
121+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
122+
};
123+
int cnt = count_if(s.begin(), s.begin() + k, isVowel);
124+
int ans = cnt;
125+
for (int i = k; i < s.size(); ++i) {
126+
cnt += isVowel(s[i]) - isVowel(s[i - k]);
127+
ans = max(ans, cnt);
125128
}
126129
return ans;
127130
}
128-
129-
bool isVowel(char c) {
130-
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
131-
}
132131
};
133132
```
134133
@@ -137,46 +136,44 @@ func maxVowels(s string, k int) int {
137136
isVowel := func(c byte) bool {
138137
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
139138
}
140-
t := 0
139+
cnt := 0
141140
for i := 0; i < k; i++ {
142141
if isVowel(s[i]) {
143-
t++
142+
cnt++
144143
}
145144
}
146-
ans := t
145+
ans := cnt
147146
for i := k; i < len(s); i++ {
148-
if isVowel(s[i]) {
149-
t++
150-
}
151147
if isVowel(s[i-k]) {
152-
t--
148+
cnt--
149+
}
150+
if isVowel(s[i]) {
151+
cnt++
153152
}
154-
ans = max(ans, t)
153+
ans = max(ans, cnt)
155154
}
156155
return ans
157156
}
158157
```
159158

160159
```ts
161160
function maxVowels(s: string, k: number): number {
162-
function isVowel(c) {
163-
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
164-
}
165-
let t = 0;
166-
for (let i = 0; i < k; ++i) {
167-
if (isVowel(s.charAt(i))) {
168-
t++;
161+
const isVowel = (c: string) => ['a', 'e', 'i', 'o', 'u'].includes(c);
162+
let cnt = 0;
163+
for (let i = 0; i < k; i++) {
164+
if (isVowel(s[i])) {
165+
cnt++;
169166
}
170167
}
171-
let ans = t;
172-
for (let i = k; i < s.length; ++i) {
173-
if (isVowel(s.charAt(i))) {
174-
t++;
168+
let ans = cnt;
169+
for (let i = k; i < s.length; i++) {
170+
if (isVowel(s[i])) {
171+
cnt++;
175172
}
176-
if (isVowel(s.charAt(i - k))) {
177-
t--;
173+
if (isVowel(s[i - k])) {
174+
cnt--;
178175
}
179-
ans = Math.max(ans, t);
176+
ans = Math.max(ans, cnt);
180177
}
181178
return ans;
182179
}
@@ -194,23 +191,22 @@ class Solution {
194191
}
195192
function maxVowels($s, $k) {
196193
$cnt = 0;
197-
$rs = 0;
198194
for ($i = 0; $i < $k; $i++) {
199195
if ($this->isVowel($s[$i])) {
200196
$cnt++;
201197
}
202198
}
203-
$rs = $cnt;
199+
$ans = $cnt;
204200
for ($j = $k; $j < strlen($s); $j++) {
205201
if ($this->isVowel($s[$j - $k])) {
206202
$cnt--;
207203
}
208204
if ($this->isVowel($s[$j])) {
209205
$cnt++;
210206
}
211-
$rs = max($rs, $cnt);
207+
$ans = max($ans, $cnt);
212208
}
213-
return $rs;
209+
return $ans;
214210
}
215211
}
216212
```

‎solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md‎

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,47 @@
4646

4747
## Solutions
4848

49-
### Solution 1
49+
### Solution 1: Sliding Window
50+
51+
First, we count the number of vowels in the first $k$ characters, denoted as $cnt,ドル and initialize the answer $ans$ as $cnt$.
52+
53+
Then we start traversing the string from $k$. For each iteration, we add the current character to the window. If the current character is a vowel, we increment $cnt$. We remove the first character from the window. If the removed character is a vowel, we decrement $cnt$. Then, we update the answer $ans = \max(ans, cnt)$.
54+
55+
After the traversal, we return the answer.
56+
57+
The time complexity is $O(n),ドル where $n$ is the length of the string $s$. The space complexity is $O(1)$.
5058

5159
<!-- tabs:start -->
5260

5361
```python
5462
class Solution:
5563
def maxVowels(self, s: str, k: int) -> int:
56-
vowels = set('aeiou')
57-
t = sum(c in vowels for c in s[:k])
58-
ans = t
64+
vowels = set("aeiou")
65+
ans = cnt = sum(c in vowels for c in s[:k])
5966
for i in range(k, len(s)):
60-
t += s[i] in vowels
61-
t -= s[i - k] in vowels
62-
ans = max(ans, t)
67+
cnt += int(s[i] in vowels) - int(s[i - k] in vowels)
68+
ans = max(ans, cnt)
6369
return ans
6470
```
6571

6672
```java
6773
class Solution {
6874
public int maxVowels(String s, int k) {
69-
int t = 0, n = s.length();
75+
int cnt = 0;
7076
for (int i = 0; i < k; ++i) {
7177
if (isVowel(s.charAt(i))) {
72-
++t;
78+
++cnt;
7379
}
7480
}
75-
int ans = t;
76-
for (int i = k; i < n; ++i) {
81+
int ans = cnt;
82+
for (int i = k; i < s.length(); ++i) {
7783
if (isVowel(s.charAt(i))) {
78-
++t;
84+
++cnt;
7985
}
8086
if (isVowel(s.charAt(i - k))) {
81-
--t;
87+
--cnt;
8288
}
83-
ans = Math.max(ans, t);
89+
ans = Math.max(ans, cnt);
8490
}
8591
return ans;
8692
}
@@ -95,20 +101,17 @@ class Solution {
95101
class Solution {
96102
public:
97103
int maxVowels(string s, int k) {
98-
int t = 0, n = s.size();
99-
for (int i = 0; i < k; ++i) t += isVowel(s[i]);
100-
int ans = t;
101-
for (int i = k; i < n; ++i) {
102-
t += isVowel(s[i]);
103-
t -= isVowel(s[i - k]);
104-
ans = max(ans, t);
104+
auto isVowel = [](char c) {
105+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
106+
};
107+
int cnt = count_if(s.begin(), s.begin() + k, isVowel);
108+
int ans = cnt;
109+
for (int i = k; i < s.size(); ++i) {
110+
cnt += isVowel(s[i]) - isVowel(s[i - k]);
111+
ans = max(ans, cnt);
105112
}
106113
return ans;
107114
}
108-
109-
bool isVowel(char c) {
110-
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
111-
}
112115
};
113116
```
114117
@@ -117,46 +120,44 @@ func maxVowels(s string, k int) int {
117120
isVowel := func(c byte) bool {
118121
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
119122
}
120-
t := 0
123+
cnt := 0
121124
for i := 0; i < k; i++ {
122125
if isVowel(s[i]) {
123-
t++
126+
cnt++
124127
}
125128
}
126-
ans := t
129+
ans := cnt
127130
for i := k; i < len(s); i++ {
128-
if isVowel(s[i]) {
129-
t++
130-
}
131131
if isVowel(s[i-k]) {
132-
t--
132+
cnt--
133+
}
134+
if isVowel(s[i]) {
135+
cnt++
133136
}
134-
ans = max(ans, t)
137+
ans = max(ans, cnt)
135138
}
136139
return ans
137140
}
138141
```
139142

140143
```ts
141144
function maxVowels(s: string, k: number): number {
142-
function isVowel(c) {
143-
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
144-
}
145-
let t = 0;
146-
for (let i = 0; i < k; ++i) {
147-
if (isVowel(s.charAt(i))) {
148-
t++;
145+
const isVowel = (c: string) => ['a', 'e', 'i', 'o', 'u'].includes(c);
146+
let cnt = 0;
147+
for (let i = 0; i < k; i++) {
148+
if (isVowel(s[i])) {
149+
cnt++;
149150
}
150151
}
151-
let ans = t;
152-
for (let i = k; i < s.length; ++i) {
153-
if (isVowel(s.charAt(i))) {
154-
t++;
152+
let ans = cnt;
153+
for (let i = k; i < s.length; i++) {
154+
if (isVowel(s[i])) {
155+
cnt++;
155156
}
156-
if (isVowel(s.charAt(i - k))) {
157-
t--;
157+
if (isVowel(s[i - k])) {
158+
cnt--;
158159
}
159-
ans = Math.max(ans, t);
160+
ans = Math.max(ans, cnt);
160161
}
161162
return ans;
162163
}
@@ -174,23 +175,22 @@ class Solution {
174175
}
175176
function maxVowels($s, $k) {
176177
$cnt = 0;
177-
$rs = 0;
178178
for ($i = 0; $i < $k; $i++) {
179179
if ($this->isVowel($s[$i])) {
180180
$cnt++;
181181
}
182182
}
183-
$rs = $cnt;
183+
$ans = $cnt;
184184
for ($j = $k; $j < strlen($s); $j++) {
185185
if ($this->isVowel($s[$j - $k])) {
186186
$cnt--;
187187
}
188188
if ($this->isVowel($s[$j])) {
189189
$cnt++;
190190
}
191-
$rs = max($rs, $cnt);
191+
$ans = max($ans, $cnt);
192192
}
193-
return $rs;
193+
return $ans;
194194
}
195195
}
196196
```
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
class Solution {
22
public:
33
int maxVowels(string s, int k) {
4-
int t = 0, n = s.size();
5-
for (int i = 0; i < k; ++i) t += isVowel(s[i]);
6-
int ans = t;
7-
for (int i = k; i < n; ++i) {
8-
t += isVowel(s[i]);
9-
t -= isVowel(s[i - k]);
10-
ans = max(ans, t);
4+
auto isVowel = [](char c) {
5+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
6+
};
7+
int cnt = count_if(s.begin(), s.begin() + k, isVowel);
8+
int ans = cnt;
9+
for (int i = k; i < s.size(); ++i) {
10+
cnt += isVowel(s[i]) - isVowel(s[i - k]);
11+
ans = max(ans, cnt);
1112
}
1213
return ans;
1314
}
14-
15-
bool isVowel(char c) {
16-
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
17-
}
1815
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /