Skip to content

Commit 5708f0a

Browse files
committed
feat: update solutions to lc problem: No.2063
No.2063.Vowels of All Substrings
1 parent 5f04197 commit 5708f0a

File tree

6 files changed

+58
-57
lines changed

6 files changed

+58
-57
lines changed

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69-
判断 `word[i]` 是否为元音,且在所有子字符串中一共出现了 `(i+1)*(n-i)` 次。
69+
**方法一:枚举贡献**
70+
71+
我们可以枚举字符串的每个字符 $word[i]$,如果 $word[i]$ 是元音字母,那么 $word[i]$ 一共在 $(i + 1) \times (n - i)$ 个子字符串中出现,将这些子字符串的个数累加即可。
72+
73+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $word$ 的长度。
7074

7175
<!-- tabs:start -->
7276

@@ -92,30 +96,14 @@ class Solution {
9296
for (int i = 0, n = word.length(); i < n; ++i) {
9397
char c = word.charAt(i);
9498
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
95-
ans += (long) (i + 1) * (n - i);
99+
ans += (i + 1L) * (n - i);
96100
}
97101
}
98102
return ans;
99103
}
100104
}
101105
```
102106

103-
### **TypeScript**
104-
105-
```ts
106-
function countVowels(word: string): number {
107-
const n = word.length;
108-
let ans = 0;
109-
for (let i = 0; i < n; i++) {
110-
let char = word.charAt(i);
111-
if (['a', 'e', 'i', 'o', 'u'].includes(char)) {
112-
ans += (i + 1) * (n - i);
113-
}
114-
}
115-
return ans;
116-
}
117-
```
118-
119107
### **C++**
120108

121109
```cpp
@@ -125,7 +113,9 @@ public:
125113
long long ans = 0;
126114
for (int i = 0, n = word.size(); i < n; ++i) {
127115
char c = word[i];
128-
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long)(i + 1) * (n - i);
116+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
117+
ans += (i + 1LL) * (n - i);
118+
}
129119
}
130120
return ans;
131121
}
@@ -135,15 +125,28 @@ public:
135125
### **Go**
136126
137127
```go
138-
func countVowels(word string) int64 {
139-
var ans int64
140-
n := len(word)
128+
func countVowels(word string) (ans int64) {
141129
for i, c := range word {
142130
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
143-
ans += int64((i + 1) * (n - i))
131+
ans += int64((i + 1) * (len(word) - i))
144132
}
145133
}
146-
return ans
134+
return
135+
}
136+
```
137+
138+
### **TypeScript**
139+
140+
```ts
141+
function countVowels(word: string): number {
142+
const n = word.length;
143+
let ans = 0;
144+
for (let i = 0; i < n; ++i) {
145+
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
146+
ans += (i + 1) * (n - i);
147+
}
148+
}
149+
return ans;
147150
}
148151
```
149152

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,14 @@ class Solution {
7474
for (int i = 0, n = word.length(); i < n; ++i) {
7575
char c = word.charAt(i);
7676
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
77-
ans += (long) (i + 1) * (n - i);
77+
ans += (i + 1L) * (n - i);
7878
}
7979
}
8080
return ans;
8181
}
8282
}
8383
```
8484

85-
### **TypeScript**
86-
87-
```ts
88-
function countVowels(word: string): number {
89-
const n = word.length;
90-
let ans = 0;
91-
for (let i = 0; i < n; i++) {
92-
let char = word.charAt(i);
93-
if (['a', 'e', 'i', 'o', 'u'].includes(char)) {
94-
ans += (i + 1) * (n - i);
95-
}
96-
}
97-
return ans;
98-
}
99-
```
100-
10185
### **C++**
10286

10387
```cpp
@@ -107,7 +91,9 @@ public:
10791
long long ans = 0;
10892
for (int i = 0, n = word.size(); i < n; ++i) {
10993
char c = word[i];
110-
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long)(i + 1) * (n - i);
94+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
95+
ans += (i + 1LL) * (n - i);
96+
}
11197
}
11298
return ans;
11399
}
@@ -117,15 +103,28 @@ public:
117103
### **Go**
118104
119105
```go
120-
func countVowels(word string) int64 {
121-
var ans int64
122-
n := len(word)
106+
func countVowels(word string) (ans int64) {
123107
for i, c := range word {
124108
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
125-
ans += int64((i + 1) * (n - i))
109+
ans += int64((i + 1) * (len(word) - i))
126110
}
127111
}
128-
return ans
112+
return
113+
}
114+
```
115+
116+
### **TypeScript**
117+
118+
```ts
119+
function countVowels(word: string): number {
120+
const n = word.length;
121+
let ans = 0;
122+
for (let i = 0; i < n; ++i) {
123+
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
124+
ans += (i + 1) * (n - i);
125+
}
126+
}
127+
return ans;
129128
}
130129
```
131130

solution/2000-2099/2063.Vowels of All Substrings/Solution.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ class Solution {
44
long long ans = 0;
55
for (int i = 0, n = word.size(); i < n; ++i) {
66
char c = word[i];
7-
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long) (i + 1) * (n - i);
7+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
8+
ans += (i + 1LL) * (n - i);
9+
}
810
}
911
return ans;
1012
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
func countVowels(word string) int64 {
2-
var ans int64
3-
n := len(word)
1+
func countVowels(word string) (ans int64) {
42
for i, c := range word {
53
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
6-
ans += int64((i + 1) * (n - i))
4+
ans += int64((i + 1) * (len(word) - i))
75
}
86
}
9-
return ans
7+
return
108
}

solution/2000-2099/2063.Vowels of All Substrings/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public long countVowels(String word) {
44
for (int i = 0, n = word.length(); i < n; ++i) {
55
char c = word.charAt(i);
66
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
7-
ans += (long) (i + 1) * (n - i);
7+
ans += (i + 1L) * (n - i);
88
}
99
}
1010
return ans;

solution/2000-2099/2063.Vowels of All Substrings/Solution.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
function countVowels(word: string): number {
22
const n = word.length;
33
let ans = 0;
4-
for (let i = 0; i < n; i++) {
5-
let char = word.charAt(i);
6-
if (['a', 'e', 'i', 'o', 'u'].includes(char)) {
4+
for (let i = 0; i < n; ++i) {
5+
if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
76
ans += (i + 1) * (n - i);
87
}
98
}

0 commit comments

Comments
 (0)