You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2000-2099/2063.Vowels of All Substrings/README_EN.md
+42-4
Original file line number
Diff line number
Diff line change
@@ -33,20 +33,20 @@ tags:
33
33
<pre>
34
34
<strong>Input:</strong> word = "aba"
35
35
<strong>Output:</strong> 6
36
-
<strong>Explanation:</strong>
36
+
<strong>Explanation:</strong>
37
37
All possible substrings are: "a", "ab", "aba", "b", "ba", and "a".
38
38
- "b" has 0 vowels in it
39
39
- "a", "ab", "ba", and "a" have 1 vowel each
40
40
- "aba" 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.
42
42
</pre>
43
43
44
44
<p><strongclass="example">Example 2:</strong></p>
45
45
46
46
<pre>
47
47
<strong>Input:</strong> word = "abc"
48
48
<strong>Output:</strong> 3
49
-
<strong>Explanation:</strong>
49
+
<strong>Explanation:</strong>
50
50
All possible substrings are: "a", "ab", "abc", "b", "bc", and "c".
51
51
- "a", "ab", and "abc" have 1 vowel each
52
52
- "b", "bc", and "c" have 0 vowels each
@@ -75,7 +75,11 @@ Hence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3.
75
75
76
76
<!-- solution:start -->
77
77
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)$.
79
83
80
84
<!-- tabs:start -->
81
85
@@ -151,6 +155,40 @@ function countVowels(word: string): number {
151
155
}
152
156
```
153
157
158
+
#### Rust
159
+
160
+
```rust
161
+
implSolution {
162
+
pubfncount_vowels(word:String) ->i64 {
163
+
letn=word.len() asi64;
164
+
word.chars()
165
+
.enumerate()
166
+
.filter(|(_, c)|"aeiou".contains(*c))
167
+
.map(|(i, _)| (iasi64+1) * (n-iasi64))
168
+
.sum()
169
+
}
170
+
}
171
+
```
172
+
173
+
#### JavaScript
174
+
175
+
```js
176
+
/**
177
+
* @param{string}word
178
+
* @return{number}
179
+
*/
180
+
varcountVowels=function (word) {
181
+
constn=word.length;
182
+
let ans =0;
183
+
for (let i =0; i < n; ++i) {
184
+
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
0 commit comments