Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Create: 1456 - Maximum Number of Vowels in a Substring of Given Length
  • Loading branch information
AHTHneeuhl committed Jul 21, 2023
commit 73dc7a5e85880408e7e5840da2feeda6f6a3d12f
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {string} s
* @param {number} k
* @return {number}
*/

// Time complexity: O(N)
// Space complexity: O(1)

var maxVowels = function (s, k) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
let left = 0;
let count = 0;
let result = 0;

for (let right = 0; right < s.length; right++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use while loop for sliding window

Suggested change
for (let right = 0; right < s.length; right++) {
let [ left, right ] = [ 0, 0 ];
while (right < s.length) {
if (canSlide) {
left += 1;
}
right += 1;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aakhtar3 I have updated the code

count += vowels.has(s[right]) ? 1 : 0;

if (right - left + 1 > k) {
count -= vowels.has(s[left]) ? 1 : 0;
left++;
}

result = Math.max(result, count);
}

return result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Time complexity: O(N)
# Space complexity: O(1)


class Solution:
def maxVowels(self, s: str, k: int) -> int:
vowels = {"a", "e", "i", "o", "u"}

left, count, result = 0, 0, 0

for right in range(len(s)):
count += 1 if s[right] in vowels else 0

if right - left + 1 > k:
count -= 1 if s[left] in vowels else 0
left += 1

result = max(result, count)

return result
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time complexity: O(N)
// Space complexity: O(1)

function maxVowels(s: string, k: number): number {
const vowels: Set<string> = new Set(['a', 'e', 'i', 'o', 'u']);
let left: number = 0;
let count: number = 0;
let result: number = 0;

for (let right: number = 0; right < s.length; right++) {
count += vowels.has(s[right]) ? 1 : 0;

if (right - left + 1 > k) {
count -= vowels.has(s[left]) ? 1 : 0;
left++;
}

result = Math.max(result, count);
}

return result;
}