Skip to content

Commit ea843b3

Browse files
committed
feat: add typescript solution to lc problem: No.1456
No.1456.Maximum Number of Vowels in a Substring of Given Length
1 parent 5fc155e commit ea843b3

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@
8080

8181
```
8282

83+
### **TypeScript**
84+
85+
```ts
86+
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++;
95+
}
96+
preSum[i] = cnt;
97+
ans = Math.max(i < k ? cnt : preSum[i] - preSum[i - k], ans);
98+
}
99+
return ans;
100+
};
101+
```
102+
83103
### **...**
84104

85105
```

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@
9696

9797
```
9898

99+
### **TypeScript**
100+
101+
```ts
102+
function maxVowels(s: string, k: number): number {
103+
const n = s.length;
104+
let ans = 0;
105+
let preSum = new Array(n).fill(0);
106+
let cnt = 0;
107+
for (let i = 0; i < n && ans != k; i++) {
108+
let char = s.charAt(i);
109+
if (['a', 'e', 'i', 'o', 'u'].includes(char)) {
110+
cnt++;
111+
}
112+
preSum[i] = cnt;
113+
ans = Math.max(i < k ? cnt : preSum[i] - preSum[i - k], ans);
114+
}
115+
return ans;
116+
};
117+
```
118+
99119
### **...**
100120

101121
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maxVowels(s: string, k: number): number {
2+
const n = s.length;
3+
let ans = 0;
4+
let preSum = new Array(n).fill(0);
5+
let cnt = 0;
6+
for (let i = 0; i < n && ans != k; i++) {
7+
let char = s.charAt(i);
8+
if (['a', 'e', 'i', 'o', 'u'].includes(char)) {
9+
cnt++;
10+
}
11+
preSum[i] = cnt;
12+
ans = Math.max(i < k ? cnt : preSum[i] - preSum[i - k], ans);
13+
}
14+
return ans;
15+
};

0 commit comments

Comments
 (0)