From a2432d464aa6e9ceaf4a3407f21f9ed8587362cc Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 8 Apr 2024 11:59:04 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.1456 (#2554) No.1456.Maximum Number of Vowels in a Substring of Given Length --- .../README.md | 100 +++++++++-------- .../README_EN.md | 102 +++++++++--------- .../Solution.cpp | 19 ++-- .../Solution.go | 16 +-- .../Solution.java | 14 +-- .../Solution.php | 9 +- .../Solution.py | 10 +- .../Solution.ts | 26 +++-- 8 files changed, 142 insertions(+), 154 deletions(-) diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md index 00827605f3bae..a996928f0f4fb 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md @@ -64,43 +64,45 @@ ### 方法一:滑动窗口 -找出所有窗口为 $k$ 中的元音字母数目,并记录最大值。 +我们首先统计前 $k$ 个字符中元音字母的个数,记为 $cnt$,初始化答案 $ans$ 为 $cnt$。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。 +然后我们从 $k$ 开始遍历字符串,每次遍历时,我们将当前字符加入窗口,如果当前字符是元音字母,则 $cnt$ 加一;将窗口第一个字符移出窗口,如果移除的字符是元音字母,则 $cnt$ 减一。然后,我们更新答案 $ans = \max(ans, cnt)$。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 ```python class Solution: def maxVowels(self, s: str, k: int) -> int: - vowels = set('aeiou') - t = sum(c in vowels for c in s[:k]) - ans = t + vowels = set("aeiou") + ans = cnt = sum(c in vowels for c in s[:k]) for i in range(k, len(s)): - t += s[i] in vowels - t -= s[i - k] in vowels - ans = max(ans, t) + cnt += int(s[i] in vowels) - int(s[i - k] in vowels) + ans = max(ans, cnt) return ans ``` ```java class Solution { public int maxVowels(String s, int k) { - int t = 0, n = s.length(); + int cnt = 0; for (int i = 0; i < k; ++i) { if (isVowel(s.charAt(i))) { - ++t; + ++cnt; } } - int ans = t; - for (int i = k; i < n; ++i) { + int ans = cnt; + for (int i = k; i < s.length(); ++i) { if (isVowel(s.charAt(i))) { - ++t; + ++cnt; } if (isVowel(s.charAt(i - k))) { - --t; + --cnt; } - ans = Math.max(ans, t); + ans = Math.max(ans, cnt); } return ans; } @@ -115,20 +117,17 @@ class Solution { class Solution { public: int maxVowels(string s, int k) { - int t = 0, n = s.size(); - for (int i = 0; i < k; ++i) t += isVowel(s[i]); - int ans = t; - for (int i = k; i < n; ++i) { - t += isVowel(s[i]); - t -= isVowel(s[i - k]); - ans = max(ans, t); + auto isVowel = [](char c) { + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + }; + int cnt = count_if(s.begin(), s.begin() + k, isVowel); + int ans = cnt; + for (int i = k; i < s.size(); ++i) { + cnt += isVowel(s[i]) - isVowel(s[i - k]); + ans = max(ans, cnt); } return ans; } - - bool isVowel(char c) { - return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; - } }; ``` @@ -137,21 +136,21 @@ func maxVowels(s string, k int) int { isVowel := func(c byte) bool { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' } - t := 0 + cnt := 0 for i := 0; i < k; i++ { if isVowel(s[i]) { - t++ + cnt++ } } - ans := t + ans := cnt for i := k; i < len(s); i++ { - if isVowel(s[i]) { - t++ - } if isVowel(s[i-k]) { - t-- + cnt-- + } + if isVowel(s[i]) { + cnt++ } - ans = max(ans, t) + ans = max(ans, cnt) } return ans } @@ -159,24 +158,22 @@ func maxVowels(s string, k int) int { ```ts function maxVowels(s: string, k: number): number { - function isVowel(c) { - return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; - } - let t = 0; - for (let i = 0; i < k; ++i) { - if (isVowel(s.charAt(i))) { - t++; + const isVowel = (c: string) => ['a', 'e', 'i', 'o', 'u'].includes(c); + let cnt = 0; + for (let i = 0; i < k; i++) { + if (isVowel(s[i])) { + cnt++; } } - let ans = t; - for (let i = k; i < s.length; ++i) { - if (isVowel(s.charAt(i))) { - t++; + let ans = cnt; + for (let i = k; i < s.length; i++) { + if (isVowel(s[i])) { + cnt++; } - if (isVowel(s.charAt(i - k))) { - t--; + if (isVowel(s[i - k])) { + cnt--; } - ans = Math.max(ans, t); + ans = Math.max(ans, cnt); } return ans; } @@ -194,13 +191,12 @@ class Solution { } function maxVowels($s, $k) { $cnt = 0; - $rs = 0; for ($i = 0; $i < $k; $i++) { if ($this->isVowel($s[$i])) { $cnt++; } } - $rs = $cnt; + $ans = $cnt; for ($j = $k; $j < strlen($s); $j++) { if ($this->isVowel($s[$j - $k])) { $cnt--; @@ -208,9 +204,9 @@ class Solution { if ($this->isVowel($s[$j])) { $cnt++; } - $rs = max($rs, $cnt); + $ans = max($ans, $cnt); } - return $rs; + return $ans; } } ``` diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md index a17e8fd7ac2f3..0d072a2ba79f5 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md @@ -46,41 +46,47 @@ ## Solutions -### Solution 1 +### Solution 1: Sliding Window + +First, we count the number of vowels in the first $k$ characters, denoted as $cnt$, and initialize the answer $ans$ as $cnt$. + +Then we start traversing the string from $k$. For each iteration, we add the current character to the window. If the current character is a vowel, we increment $cnt$. We remove the first character from the window. If the removed character is a vowel, we decrement $cnt$. Then, we update the answer $ans = \max(ans, cnt)$. + +After the traversal, we return the answer. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. ```python class Solution: def maxVowels(self, s: str, k: int) -> int: - vowels = set('aeiou') - t = sum(c in vowels for c in s[:k]) - ans = t + vowels = set("aeiou") + ans = cnt = sum(c in vowels for c in s[:k]) for i in range(k, len(s)): - t += s[i] in vowels - t -= s[i - k] in vowels - ans = max(ans, t) + cnt += int(s[i] in vowels) - int(s[i - k] in vowels) + ans = max(ans, cnt) return ans ``` ```java class Solution { public int maxVowels(String s, int k) { - int t = 0, n = s.length(); + int cnt = 0; for (int i = 0; i < k; ++i) { if (isVowel(s.charAt(i))) { - ++t; + ++cnt; } } - int ans = t; - for (int i = k; i < n; ++i) { + int ans = cnt; + for (int i = k; i < s.length(); ++i) { if (isVowel(s.charAt(i))) { - ++t; + ++cnt; } if (isVowel(s.charAt(i - k))) { - --t; + --cnt; } - ans = Math.max(ans, t); + ans = Math.max(ans, cnt); } return ans; } @@ -95,20 +101,17 @@ class Solution { class Solution { public: int maxVowels(string s, int k) { - int t = 0, n = s.size(); - for (int i = 0; i < k; ++i) t += isVowel(s[i]); - int ans = t; - for (int i = k; i < n; ++i) { - t += isVowel(s[i]); - t -= isVowel(s[i - k]); - ans = max(ans, t); + auto isVowel = [](char c) { + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + }; + int cnt = count_if(s.begin(), s.begin() + k, isVowel); + int ans = cnt; + for (int i = k; i < s.size(); ++i) { + cnt += isVowel(s[i]) - isVowel(s[i - k]); + ans = max(ans, cnt); } return ans; } - - bool isVowel(char c) { - return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; - } }; ``` @@ -117,21 +120,21 @@ func maxVowels(s string, k int) int { isVowel := func(c byte) bool { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' } - t := 0 + cnt := 0 for i := 0; i < k; i++ { if isVowel(s[i]) { - t++ + cnt++ } } - ans := t + ans := cnt for i := k; i < len(s); i++ { - if isVowel(s[i]) { - t++ - } if isVowel(s[i-k]) { - t-- + cnt-- + } + if isVowel(s[i]) { + cnt++ } - ans = max(ans, t) + ans = max(ans, cnt) } return ans } @@ -139,24 +142,22 @@ func maxVowels(s string, k int) int { ```ts function maxVowels(s: string, k: number): number { - function isVowel(c) { - return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; - } - let t = 0; - for (let i = 0; i < k; ++i) { - if (isVowel(s.charAt(i))) { - t++; + const isVowel = (c: string) => ['a', 'e', 'i', 'o', 'u'].includes(c); + let cnt = 0; + for (let i = 0; i < k; i++) { + if (isVowel(s[i])) { + cnt++; } } - let ans = t; - for (let i = k; i < s.length; ++i) { - if (isVowel(s.charAt(i))) { - t++; + let ans = cnt; + for (let i = k; i < s.length; i++) { + if (isVowel(s[i])) { + cnt++; } - if (isVowel(s.charAt(i - k))) { - t--; + if (isVowel(s[i - k])) { + cnt--; } - ans = Math.max(ans, t); + ans = Math.max(ans, cnt); } return ans; } @@ -174,13 +175,12 @@ class Solution { } function maxVowels($s, $k) { $cnt = 0; - $rs = 0; for ($i = 0; $i < $k; $i++) { if ($this->isVowel($s[$i])) { $cnt++; } } - $rs = $cnt; + $ans = $cnt; for ($j = $k; $j < strlen($s); $j++) { if ($this->isVowel($s[$j - $k])) { $cnt--; @@ -188,9 +188,9 @@ class Solution { if ($this->isVowel($s[$j])) { $cnt++; } - $rs = max($rs, $cnt); + $ans = max($ans, $cnt); } - return $rs; + return $ans; } } ``` diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.cpp b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.cpp index 60eb8c1d0708d..aa8ab81b49f94 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.cpp +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.cpp @@ -1,18 +1,15 @@ class Solution { public: int maxVowels(string s, int k) { - int t = 0, n = s.size(); - for (int i = 0; i < k; ++i) t += isVowel(s[i]); - int ans = t; - for (int i = k; i < n; ++i) { - t += isVowel(s[i]); - t -= isVowel(s[i - k]); - ans = max(ans, t); + auto isVowel = [](char c) { + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + }; + int cnt = count_if(s.begin(), s.begin() + k, isVowel); + int ans = cnt; + for (int i = k; i < s.size(); ++i) { + cnt += isVowel(s[i]) - isVowel(s[i - k]); + ans = max(ans, cnt); } return ans; } - - bool isVowel(char c) { - return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; - } }; \ No newline at end of file diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.go b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.go index af91bc70f130f..2c959f77cdf96 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.go +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.go @@ -2,21 +2,21 @@ func maxVowels(s string, k int) int { isVowel := func(c byte) bool { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' } - t := 0 + cnt := 0 for i := 0; i < k; i++ { if isVowel(s[i]) { - t++ + cnt++ } } - ans := t + ans := cnt for i := k; i < len(s); i++ { - if isVowel(s[i]) { - t++ - } if isVowel(s[i-k]) { - t-- + cnt-- + } + if isVowel(s[i]) { + cnt++ } - ans = max(ans, t) + ans = max(ans, cnt) } return ans } \ No newline at end of file diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.java b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.java index 667592a1bccd7..b6b7ee33f4d8c 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.java +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.java @@ -1,20 +1,20 @@ class Solution { public int maxVowels(String s, int k) { - int t = 0, n = s.length(); + int cnt = 0; for (int i = 0; i < k; ++i) { if (isVowel(s.charAt(i))) { - ++t; + ++cnt; } } - int ans = t; - for (int i = k; i < n; ++i) { + int ans = cnt; + for (int i = k; i < s.length(); ++i) { if (isVowel(s.charAt(i))) { - ++t; + ++cnt; } if (isVowel(s.charAt(i - k))) { - --t; + --cnt; } - ans = Math.max(ans, t); + ans = Math.max(ans, cnt); } return ans; } diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.php b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.php index 2daa08c28da11..968aabe49c53d 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.php +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.php @@ -9,13 +9,12 @@ function isVowel($c) { } function maxVowels($s, $k) { $cnt = 0; - $rs = 0; for ($i = 0; $i < $k; $i++) { if ($this->isVowel($s[$i])) { $cnt++; } } - $rs = $cnt; + $ans = $cnt; for ($j = $k; $j < strlen($s); $j++) { if ($this->isVowel($s[$j - $k])) { $cnt--; @@ -23,8 +22,8 @@ function maxVowels($s, $k) { if ($this->isVowel($s[$j])) { $cnt++; } - $rs = max($rs, $cnt); + $ans = max($ans, $cnt); } - return $rs; + return $ans; } -} +} \ No newline at end of file diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.py b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.py index 7e8a980abee54..afd236a4e30dc 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.py +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.py @@ -1,10 +1,8 @@ class Solution: def maxVowels(self, s: str, k: int) -> int: - vowels = set('aeiou') - t = sum(c in vowels for c in s[:k]) - ans = t + vowels = set("aeiou") + ans = cnt = sum(c in vowels for c in s[:k]) for i in range(k, len(s)): - t += s[i] in vowels - t -= s[i - k] in vowels - ans = max(ans, t) + cnt += int(s[i] in vowels) - int(s[i - k] in vowels) + ans = max(ans, cnt) return ans diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.ts b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.ts index e20d75e537c90..93449f8d6d245 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.ts +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.ts @@ -1,22 +1,20 @@ function maxVowels(s: string, k: number): number { - function isVowel(c) { - return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; - } - let t = 0; - for (let i = 0; i < k; ++i) { - if (isVowel(s.charAt(i))) { - t++; + const isVowel = (c: string) => ['a', 'e', 'i', 'o', 'u'].includes(c); + let cnt = 0; + for (let i = 0; i < k; i++) { + if (isVowel(s[i])) { + cnt++; } } - let ans = t; - for (let i = k; i < s.length; ++i) { - if (isVowel(s.charAt(i))) { - t++; + let ans = cnt; + for (let i = k; i < s.length; i++) { + if (isVowel(s[i])) { + cnt++; } - if (isVowel(s.charAt(i - k))) { - t--; + if (isVowel(s[i - k])) { + cnt--; } - ans = Math.max(ans, t); + ans = Math.max(ans, cnt); } return ans; }