-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73a2a8c
commit cb008d7
Showing
4 changed files
with
44 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { maxVowels } from './maxVowels'; | ||
|
||
describe('1456. Maximum Number of Vowels in a Substring of Given Length', () => { | ||
test('maxVowels', () => { | ||
expect(maxVowels('abciiidef', 3)).toBe(3); | ||
expect(maxVowels('aeiou', 2)).toBe(2); | ||
expect(maxVowels('leetcode', 3)).toBe(2); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
type MaxVowels = (s: string, k: number) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const maxVowels: MaxVowels = (s, k) => { | ||
const vowels = new Set(['a', 'e', 'i', 'o', 'u']); | ||
|
||
let maxVowels = 0; | ||
let currentVowels = 0; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
// Check if the current character is a vowel | ||
if (vowels.has(s[i])) currentVowels += 1; | ||
|
||
// If the window size exceeds k, slide the window | ||
if (i >= k && vowels.has(s[i - k])) currentVowels -= 1; | ||
|
||
// Update maxVowels | ||
if (i >= k - 1) maxVowels = Math.max(maxVowels, currentVowels); | ||
} | ||
|
||
return maxVowels; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters