mirror of https://github.com/doocs/leetcode.git
feat: add solutions to lc problem: No.2063 (#4075)
No.2063.Vowels of All Substrings
This commit is contained in:
parent
d24325a613
commit
68eed4908e
|
|
@ -85,9 +85,9 @@ tags:
|
|||
|
||||
### 方法一:枚举贡献
|
||||
|
||||
我们可以枚举字符串的每个字符 $word[i]$,如果 $word[i]$ 是元音字母,那么 $word[i]$ 一共在 $(i + 1) \times (n - i)$ 个子字符串中出现,将这些子字符串的个数累加即可。
|
||||
我们可以枚举字符串的每个字符 $\textit{word}[i]$,如果 $\textit{word}[i]$ 是元音字母,那么 $\textit{word}[i]$ 一共在 $(i + 1) \times (n - i)$ 个子字符串中出现,将这些子字符串的个数累加即可。
|
||||
|
||||
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $word$ 的长度。
|
||||
时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{word}$ 的长度。空间复杂度 $O(1)$。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -163,6 +163,40 @@ function countVowels(word: string): number {
|
|||
}
|
||||
```
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn count_vowels(word: String) -> i64 {
|
||||
let n = word.len() as i64;
|
||||
word.chars()
|
||||
.enumerate()
|
||||
.filter(|(_, c)| "aeiou".contains(*c))
|
||||
.map(|(i, _)| (i as i64 + 1) * (n - i as i64))
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```js
|
||||
/**
|
||||
* @param {string} word
|
||||
* @return {number}
|
||||
*/
|
||||
var countVowels = function (word) {
|
||||
const n = word.length;
|
||||
let ans = 0;
|
||||
for (let i = 0; i < n; ++i) {
|
||||
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
|
||||
ans += (i + 1) * (n - i);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
};
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
|
|
|||
|
|
@ -33,12 +33,12 @@ tags:
|
|||
<pre>
|
||||
<strong>Input:</strong> word = "aba"
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
<strong>Explanation:</strong>
|
||||
All possible substrings are: "a", "ab", "aba", "b", "ba", and "a".
|
||||
- "b" has 0 vowels in it
|
||||
- "a", "ab", "ba", and "a" have 1 vowel each
|
||||
- "aba" has 2 vowels in it
|
||||
Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6.
|
||||
Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
|
@ -46,7 +46,7 @@ Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6.
|
|||
<pre>
|
||||
<strong>Input:</strong> word = "abc"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
<strong>Explanation:</strong>
|
||||
All possible substrings are: "a", "ab", "abc", "b", "bc", and "c".
|
||||
- "a", "ab", and "abc" have 1 vowel each
|
||||
- "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.
|
|||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 1
|
||||
### Solution 1: Enumerate Contribution
|
||||
|
||||
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.
|
||||
|
||||
The time complexity is $O(n)$, where $n$ is the length of the string $\textit{word}$. The space complexity is $O(1)$.
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -151,6 +155,40 @@ function countVowels(word: string): number {
|
|||
}
|
||||
```
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn count_vowels(word: String) -> i64 {
|
||||
let n = word.len() as i64;
|
||||
word.chars()
|
||||
.enumerate()
|
||||
.filter(|(_, c)| "aeiou".contains(*c))
|
||||
.map(|(i, _)| (i as i64 + 1) * (n - i as i64))
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```js
|
||||
/**
|
||||
* @param {string} word
|
||||
* @return {number}
|
||||
*/
|
||||
var countVowels = function (word) {
|
||||
const n = word.length;
|
||||
let ans = 0;
|
||||
for (let i = 0; i < n; ++i) {
|
||||
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
|
||||
ans += (i + 1) * (n - i);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
};
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* @param {string} word
|
||||
* @return {number}
|
||||
*/
|
||||
var countVowels = function (word) {
|
||||
const n = word.length;
|
||||
let ans = 0;
|
||||
for (let i = 0; i < n; ++i) {
|
||||
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
|
||||
ans += (i + 1) * (n - i);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
};
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
impl Solution {
|
||||
pub fn count_vowels(word: String) -> i64 {
|
||||
let n = word.len() as i64;
|
||||
word.chars()
|
||||
.enumerate()
|
||||
.filter(|(_, c)| "aeiou".contains(*c))
|
||||
.map(|(i, _)| (i as i64 + 1) * (n - i as i64))
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue