diff --git a/README.md b/README.md index 4be6b4d..d2da83c 100644 --- a/README.md +++ b/README.md @@ -107,14 +107,15 @@ Ace Coding Interview with 75 Qs [11]: ./src/page-1/11.%20Container%20With%20Most%20Water/maxArea.ts [1679]: ./src/page-16/1679.%20Max%20Number%20of%20K-Sum%20Pairs/maxOperations.ts -| Sliding Window | | | -| ------------------------------------------------------------- | --------------- | ------ | -| 643. Maximum Average Subarray I | [Solution][643] | Easy | -| 1456. Maximum Number of Vowels in a Substring of Given Length | Solution | Medium | -| 1004. Max Consecutive Ones III | Solution | Medium | -| 1493. Longest Subarray of 1's After Deleting One Element | Solution | Medium | +| Sliding Window | | | +| ------------------------------------------------------------- | ---------------- | ------ | +| 643. Maximum Average Subarray I | [Solution][643] | Easy | +| 1456. Maximum Number of Vowels in a Substring of Given Length | [Solution][1456] | Medium | +| 1004. Max Consecutive Ones III | Solution | Medium | +| 1493. Longest Subarray of 1's After Deleting One Element | Solution | Medium | [643]: ./src/page-6/643.%20Maximum%20Average%20Subarray%20I/findMaxAverage.ts +[1456]: ./src/page-14/1456.%20Maximum%20Number%20of%20Vowels%20in%20a%20Substring%20of%20Given%20Length/maxVowels.ts | Prefix Sum | | | | ------------------------------- | -------- | ---- | diff --git a/src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.test.ts b/src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.test.ts new file mode 100644 index 0000000..6b6c60a --- /dev/null +++ b/src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.test.ts @@ -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); + }); +}); diff --git a/src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.ts b/src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.ts new file mode 100644 index 0000000..128bb90 --- /dev/null +++ b/src/page-14/1456. Maximum Number of Vowels in a Substring of Given Length/maxVowels.ts @@ -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; +}; diff --git a/src/page-6/643. Maximum Average Subarray I/findMaxAverage.ts b/src/page-6/643. Maximum Average Subarray I/findMaxAverage.ts index 41faa93..6718e08 100644 --- a/src/page-6/643. Maximum Average Subarray I/findMaxAverage.ts +++ b/src/page-6/643. Maximum Average Subarray I/findMaxAverage.ts @@ -1,5 +1,8 @@ type FindMaxAverage = (nums: number[], k: number) => number; +/** + * Accepted + */ export const findMaxAverage: FindMaxAverage = (nums, k) => { const n = nums.length; @@ -12,7 +15,7 @@ export const findMaxAverage: FindMaxAverage = (nums, k) => { // Slide the window over the array from the start to the end for (let i = k; i < n; i++) { - sum = sum - nums[i - k] + nums[i]; + sum = sum - nums[i - k] + nums[i]; // Slide the window to the right maxSum = Math.max(maxSum, sum); }