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 c2be5c8

Browse files
committed
feat: add solutions to lc problem: No.1456
No.1456.Maximum Number of Vowels in a Substring of Given Length
1 parent 6e67b4a commit c2be5c8

File tree

7 files changed

+321
-32
lines changed

7 files changed

+321
-32
lines changed

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

Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,39 +62,142 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:滑动窗口**
66+
67+
找出所有窗口为 $k$ 中的元音字母数目,并记录最大值。
68+
69+
时间复杂度 O(n),空间复杂度 O(1)。
70+
6571
<!-- tabs:start -->
6672

6773
### **Python3**
6874

6975
<!-- 这里可写当前语言的特殊实现逻辑 -->
7076

7177
```python
72-
78+
class Solution:
79+
def maxVowels(self, s: str, k: int) -> int:
80+
vowels = set('aeiou')
81+
t = sum(c in vowels for c in s[:k])
82+
ans = t
83+
for i in range(k, len(s)):
84+
t += s[i] in vowels
85+
t -= s[i - k] in vowels
86+
ans = max(ans, t)
87+
return ans
7388
```
7489

7590
### **Java**
7691

7792
<!-- 这里可写当前语言的特殊实现逻辑 -->
7893

7994
```java
95+
class Solution {
96+
public int maxVowels(String s, int k) {
97+
int t = 0, n = s.length();
98+
for (int i = 0; i < k; ++i) {
99+
if (isVowel(s.charAt(i))) {
100+
++t;
101+
}
102+
}
103+
int ans = t;
104+
for (int i = k; i < n; ++i) {
105+
if (isVowel(s.charAt(i))) {
106+
++t;
107+
}
108+
if (isVowel(s.charAt(i - k))) {
109+
--t;
110+
}
111+
ans = Math.max(ans, t);
112+
}
113+
return ans;
114+
}
115+
116+
private boolean isVowel(char c) {
117+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int maxVowels(string s, int k) {
128+
int t = 0, n = s.size();
129+
for (int i = 0; i < k; ++i) t += isVowel(s[i]);
130+
int ans = t;
131+
for (int i = k; i < n; ++i) {
132+
t += isVowel(s[i]);
133+
t -= isVowel(s[i - k]);
134+
ans = max(ans, t);
135+
}
136+
return ans;
137+
}
138+
139+
bool isVowel(char c) {
140+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
141+
}
142+
};
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
func maxVowels(s string, k int) int {
149+
isVowel := func(c byte) bool {
150+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
151+
}
152+
t := 0
153+
for i := 0; i < k; i++ {
154+
if isVowel(s[i]) {
155+
t++
156+
}
157+
}
158+
ans := t
159+
for i := k; i < len(s); i++ {
160+
if isVowel(s[i]) {
161+
t++
162+
}
163+
if isVowel(s[i-k]) {
164+
t--
165+
}
166+
ans = max(ans, t)
167+
}
168+
return ans
169+
}
80170

171+
func max(a, b int) int {
172+
if a > b {
173+
return a
174+
}
175+
return b
176+
}
81177
```
82178

83179
### **TypeScript**
84180

85181
```ts
86182
function maxVowels(s: string, k: number): number {
87-
const n = s.length;
88-
let ans = 0;
89-
let preSum = new Array(n).fill(0);
90-
let cnt = 0;
91-
for (let i = 0; i < n && ans != k; i++) {
92-
let char = s.charAt(i);
93-
if (['a', 'e', 'i', 'o', 'u'].includes(char)) {
94-
cnt++;
183+
function isVowel(c) {
184+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
185+
}
186+
let t = 0;
187+
for (let i = 0; i < k; ++i) {
188+
if (isVowel(s.charAt(i))) {
189+
t++;
190+
}
191+
}
192+
let ans = t;
193+
for (let i = k; i < s.length; ++i) {
194+
if (isVowel(s.charAt(i))) {
195+
t++;
196+
}
197+
if (isVowel(s.charAt(i - k))) {
198+
t--;
95199
}
96-
preSum[i] = cnt;
97-
ans = Math.max(i < k ? cnt : preSum[i] - preSum[i - k], ans);
200+
ans = Math.max(ans, t);
98201
}
99202
return ans;
100203
}

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

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,127 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def maxVowels(self, s: str, k: int) -> int:
54+
vowels = set('aeiou')
55+
t = sum(c in vowels for c in s[:k])
56+
ans = t
57+
for i in range(k, len(s)):
58+
t += s[i] in vowels
59+
t -= s[i - k] in vowels
60+
ans = max(ans, t)
61+
return ans
5362
```
5463

5564
### **Java**
5665

5766
```java
67+
class Solution {
68+
public int maxVowels(String s, int k) {
69+
int t = 0, n = s.length();
70+
for (int i = 0; i < k; ++i) {
71+
if (isVowel(s.charAt(i))) {
72+
++t;
73+
}
74+
}
75+
int ans = t;
76+
for (int i = k; i < n; ++i) {
77+
if (isVowel(s.charAt(i))) {
78+
++t;
79+
}
80+
if (isVowel(s.charAt(i - k))) {
81+
--t;
82+
}
83+
ans = Math.max(ans, t);
84+
}
85+
return ans;
86+
}
5887

88+
private boolean isVowel(char c) {
89+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int maxVowels(string s, int k) {
100+
int t = 0, n = s.size();
101+
for (int i = 0; i < k; ++i) t += isVowel(s[i]);
102+
int ans = t;
103+
for (int i = k; i < n; ++i) {
104+
t += isVowel(s[i]);
105+
t -= isVowel(s[i - k]);
106+
ans = max(ans, t);
107+
}
108+
return ans;
109+
}
110+
111+
bool isVowel(char c) {
112+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
113+
}
114+
};
115+
```
116+
117+
### **Go**
118+
119+
```go
120+
func maxVowels(s string, k int) int {
121+
isVowel := func(c byte) bool {
122+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
123+
}
124+
t := 0
125+
for i := 0; i < k; i++ {
126+
if isVowel(s[i]) {
127+
t++
128+
}
129+
}
130+
ans := t
131+
for i := k; i < len(s); i++ {
132+
if isVowel(s[i]) {
133+
t++
134+
}
135+
if isVowel(s[i-k]) {
136+
t--
137+
}
138+
ans = max(ans, t)
139+
}
140+
return ans
141+
}
142+
143+
func max(a, b int) int {
144+
if a > b {
145+
return a
146+
}
147+
return b
148+
}
59149
```
60150

61151
### **TypeScript**
62152

63153
```ts
64154
function maxVowels(s: string, k: number): number {
65-
const n = s.length;
66-
let ans = 0;
67-
let preSum = new Array(n).fill(0);
68-
let cnt = 0;
69-
for (let i = 0; i < n && ans != k; i++) {
70-
let char = s.charAt(i);
71-
if (['a', 'e', 'i', 'o', 'u'].includes(char)) {
72-
cnt++;
155+
function isVowel(c) {
156+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
157+
}
158+
let t = 0;
159+
for (let i = 0; i < k; ++i) {
160+
if (isVowel(s.charAt(i))) {
161+
t++;
162+
}
163+
}
164+
let ans = t;
165+
for (let i = k; i < s.length; ++i) {
166+
if (isVowel(s.charAt(i))) {
167+
t++;
168+
}
169+
if (isVowel(s.charAt(i - k))) {
170+
t--;
73171
}
74-
preSum[i] = cnt;
75-
ans = Math.max(i < k ? cnt : preSum[i] - preSum[i - k], ans);
172+
ans = Math.max(ans, t);
76173
}
77174
return ans;
78175
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
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);
11+
}
12+
return ans;
13+
}
14+
15+
bool isVowel(char c) {
16+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
17+
}
18+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func maxVowels(s string, k int) int {
2+
isVowel := func(c byte) bool {
3+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
4+
}
5+
t := 0
6+
for i := 0; i < k; i++ {
7+
if isVowel(s[i]) {
8+
t++
9+
}
10+
}
11+
ans := t
12+
for i := k; i < len(s); i++ {
13+
if isVowel(s[i]) {
14+
t++
15+
}
16+
if isVowel(s[i-k]) {
17+
t--
18+
}
19+
ans = max(ans, t)
20+
}
21+
return ans
22+
}
23+
24+
func max(a, b int) int {
25+
if a > b {
26+
return a
27+
}
28+
return b
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int maxVowels(String s, int k) {
3+
int t = 0, n = s.length();
4+
for (int i = 0; i < k; ++i) {
5+
if (isVowel(s.charAt(i))) {
6+
++t;
7+
}
8+
}
9+
int ans = t;
10+
for (int i = k; i < n; ++i) {
11+
if (isVowel(s.charAt(i))) {
12+
++t;
13+
}
14+
if (isVowel(s.charAt(i - k))) {
15+
--t;
16+
}
17+
ans = Math.max(ans, t);
18+
}
19+
return ans;
20+
}
21+
22+
private boolean isVowel(char c) {
23+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxVowels(self, s: str, k: int) -> int:
3+
vowels = set('aeiou')
4+
t = sum(c in vowels for c in s[:k])
5+
ans = t
6+
for i in range(k, len(s)):
7+
t += s[i] in vowels
8+
t -= s[i - k] in vowels
9+
ans = max(ans, t)
10+
return ans

0 commit comments

Comments
(0)

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