Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68eed49

Browse files
feat: add solutions to lc problem: No.2063 (doocs#4075)
No.2063.Vowels of All Substrings
1 parent d24325a commit 68eed49

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

‎solution/2000-2099/2063.Vowels of All Substrings/README.md‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ tags:
8585

8686
### 方法一:枚举贡献
8787

88-
我们可以枚举字符串的每个字符 $word[i],ドル如果 $word[i]$ 是元音字母,那么 $word[i]$ 一共在 $(i + 1) \times (n - i)$ 个子字符串中出现,将这些子字符串的个数累加即可。
88+
我们可以枚举字符串的每个字符 $\textit{word}[i],ドル如果 $\textit{word}[i]$ 是元音字母,那么 $\textit{word}[i]$ 一共在 $(i + 1) \times (n - i)$ 个子字符串中出现,将这些子字符串的个数累加即可。
8989

90-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为字符串 $word$ 的长度。
90+
时间复杂度 $O(n),ドル其中 $n$ 为字符串 $\textit{word}$ 的长度。空间复杂度 $O(1)$
9191

9292
<!-- tabs:start -->
9393

@@ -163,6 +163,40 @@ function countVowels(word: string): number {
163163
}
164164
```
165165

166+
#### Rust
167+
168+
```rust
169+
impl Solution {
170+
pub fn count_vowels(word: String) -> i64 {
171+
let n = word.len() as i64;
172+
word.chars()
173+
.enumerate()
174+
.filter(|(_, c)| "aeiou".contains(*c))
175+
.map(|(i, _)| (i as i64 + 1) * (n - i as i64))
176+
.sum()
177+
}
178+
}
179+
```
180+
181+
#### JavaScript
182+
183+
```js
184+
/**
185+
* @param {string} word
186+
* @return {number}
187+
*/
188+
var countVowels = function (word) {
189+
const n = word.length;
190+
let ans = 0;
191+
for (let i = 0; i < n; ++i) {
192+
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
193+
ans += (i + 1) * (n - i);
194+
}
195+
}
196+
return ans;
197+
};
198+
```
199+
166200
<!-- tabs:end -->
167201

168202
<!-- solution:end -->

‎solution/2000-2099/2063.Vowels of All Substrings/README_EN.md‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ tags:
3333
<pre>
3434
<strong>Input:</strong> word = &quot;aba&quot;
3535
<strong>Output:</strong> 6
36-
<strong>Explanation:</strong>
36+
<strong>Explanation:</strong>
3737
All possible substrings are: &quot;a&quot;, &quot;ab&quot;, &quot;aba&quot;, &quot;b&quot;, &quot;ba&quot;, and &quot;a&quot;.
3838
- &quot;b&quot; has 0 vowels in it
3939
- &quot;a&quot;, &quot;ab&quot;, &quot;ba&quot;, and &quot;a&quot; have 1 vowel each
4040
- &quot;aba&quot; has 2 vowels in it
41-
Hence, the total sum of vowels = 0 + 1 +たす 1 +たす 1 +たす 1 +たす 2 = 6.
41+
Hence, the total sum of vowels = 0 + 1 +たす 1 +たす 1 +たす 1 +たす 2 = 6.
4242
</pre>
4343

4444
<p><strong class="example">Example 2:</strong></p>
4545

4646
<pre>
4747
<strong>Input:</strong> word = &quot;abc&quot;
4848
<strong>Output:</strong> 3
49-
<strong>Explanation:</strong>
49+
<strong>Explanation:</strong>
5050
All possible substrings are: &quot;a&quot;, &quot;ab&quot;, &quot;abc&quot;, &quot;b&quot;, &quot;bc&quot;, and &quot;c&quot;.
5151
- &quot;a&quot;, &quot;ab&quot;, and &quot;abc&quot; have 1 vowel each
5252
- &quot;b&quot;, &quot;bc&quot;, and &quot;c&quot; have 0 vowels each
@@ -75,7 +75,11 @@ Hence, the total sum of vowels = 1 +たす 1 +たす 1 +たす 0 +たす 0 +たす 0 = 3.
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Enumerate Contribution
79+
80+
We can enumerate each character $\textit{word}[i]$ in the string. If $\textit{word}[i]$ is a vowel, then $\textit{word}[i]$ appears in $(i + 1) \times (n - i)$ substrings. We sum up the counts of these substrings.
81+
82+
The time complexity is $O(n),ドル where $n$ is the length of the string $\textit{word}$. The space complexity is $O(1)$.
7983

8084
<!-- tabs:start -->
8185

@@ -151,6 +155,40 @@ function countVowels(word: string): number {
151155
}
152156
```
153157

158+
#### Rust
159+
160+
```rust
161+
impl Solution {
162+
pub fn count_vowels(word: String) -> i64 {
163+
let n = word.len() as i64;
164+
word.chars()
165+
.enumerate()
166+
.filter(|(_, c)| "aeiou".contains(*c))
167+
.map(|(i, _)| (i as i64 + 1) * (n - i as i64))
168+
.sum()
169+
}
170+
}
171+
```
172+
173+
#### JavaScript
174+
175+
```js
176+
/**
177+
* @param {string} word
178+
* @return {number}
179+
*/
180+
var countVowels = function (word) {
181+
const n = word.length;
182+
let ans = 0;
183+
for (let i = 0; i < n; ++i) {
184+
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
185+
ans += (i + 1) * (n - i);
186+
}
187+
}
188+
return ans;
189+
};
190+
```
191+
154192
<!-- tabs:end -->
155193

156194
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {string} word
3+
* @return {number}
4+
*/
5+
var countVowels = function (word) {
6+
const n = word.length;
7+
let ans = 0;
8+
for (let i = 0; i < n; ++i) {
9+
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
10+
ans += (i + 1) * (n - i);
11+
}
12+
}
13+
return ans;
14+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn count_vowels(word: String) -> i64 {
3+
let n = word.len() as i64;
4+
word.chars()
5+
.enumerate()
6+
.filter(|(_, c)| "aeiou".contains(*c))
7+
.map(|(i, _)| (i as i64 + 1) * (n - i as i64))
8+
.sum()
9+
}
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /