Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.1456 (#2554)
Browse files Browse the repository at this point in the history
No.1456.Maximum Number of Vowels in a Substring of Given Length
  • Loading branch information
yanglbme authored Apr 8, 2024
1 parent 96852d1 commit a2432d4
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

<!-- tabs:start -->

```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;
}
Expand All @@ -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';
}
};
```
Expand All @@ -137,46 +136,44 @@ 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
}
```

```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;
}
Expand All @@ -194,23 +191,22 @@ 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--;
}
if ($this->isVowel($s[$j])) {
$cnt++;
}
$rs = max($rs, $cnt);
$ans = max($ans, $cnt);
}
return $rs;
return $ans;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)$.

<!-- tabs:start -->

```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;
}
Expand All @@ -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';
}
};
```
Expand All @@ -117,46 +120,44 @@ 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
}
```

```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;
}
Expand All @@ -174,23 +175,22 @@ 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--;
}
if ($this->isVowel($s[$j])) {
$cnt++;
}
$rs = max($rs, $cnt);
$ans = max($ans, $cnt);
}
return $rs;
return $ans;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
}
};
Loading

0 comments on commit a2432d4

Please sign in to comment.