Skip to content

Commit 90d21ff

Browse files
committed
feat: add solutions to lc problem: No.2063.Vowels of All Substrings
1 parent af18042 commit 90d21ff

File tree

6 files changed

+145
-7
lines changed

6 files changed

+145
-7
lines changed

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

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

69-
判断word[i]是否为元音,且在所有子字符串中一共出现了 (i+1)*(n-i)
69+
判断 `word[i]` 是否为元音,且在所有子字符串中一共出现了 `(i+1)*(n-i)` 次。
7070

7171
<!-- tabs:start -->
7272

@@ -75,15 +75,32 @@
7575
<!-- 这里可写当前语言的特殊实现逻辑 -->
7676

7777
```python
78-
78+
class Solution:
79+
def countVowels(self, word: str) -> int:
80+
ans, n = 0, len(word)
81+
for i, c in enumerate(word):
82+
if c in ['a', 'e', 'i', 'o', 'u']:
83+
ans += (i + 1) * (n - i)
84+
return ans
7985
```
8086

8187
### **Java**
8288

8389
<!-- 这里可写当前语言的特殊实现逻辑 -->
8490

8591
```java
86-
92+
class Solution {
93+
public long countVowels(String word) {
94+
long ans = 0;
95+
for (int i = 0, n = word.length(); i < n; ++i) {
96+
char c = word.charAt(i);
97+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
98+
ans += (long) (i + 1) * (n - i);
99+
}
100+
}
101+
return ans;
102+
}
103+
}
87104
```
88105

89106
### **TypeScript**
@@ -102,6 +119,38 @@ function countVowels(word: string): number {
102119
};
103120
```
104121

122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
long long countVowels(string word) {
128+
long long ans = 0;
129+
for (int i = 0, n = word.size(); i < n; ++i)
130+
{
131+
char c = word[i];
132+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long) (i + 1) * (n - i);
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func countVowels(word string) int64 {
143+
var ans int64
144+
n := len(word)
145+
for i, c := range word {
146+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
147+
ans += int64((i + 1) * (n - i))
148+
}
149+
}
150+
return ans
151+
}
152+
```
153+
105154
### **...**
106155

107156
```

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,37 @@ Hence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3. </pre>
5858
<li><code>word</code> consists of lowercase English letters.</li>
5959
</ul>
6060

61-
6261
## Solutions
6362

6463
<!-- tabs:start -->
6564

6665
### **Python3**
6766

6867
```python
68+
class Solution:
69+
def countVowels(self, word: str) -> int:
70+
ans, n = 0, len(word)
71+
for i, c in enumerate(word):
72+
if c in ['a', 'e', 'i', 'o', 'u']:
73+
ans += (i + 1) * (n - i)
74+
return ans
75+
```
76+
77+
### **Java**
6978

79+
```java
80+
class Solution {
81+
public long countVowels(String word) {
82+
long ans = 0;
83+
for (int i = 0, n = word.length(); i < n; ++i) {
84+
char c = word.charAt(i);
85+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
86+
ans += (long) (i + 1) * (n - i);
87+
}
88+
}
89+
return ans;
90+
}
91+
}
7092
```
7193

7294
### **TypeScript**
@@ -85,10 +107,36 @@ function countVowels(word: string): number {
85107
};
86108
```
87109

88-
### **Java**
89-
90-
```java
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
long long countVowels(string word) {
116+
long long ans = 0;
117+
for (int i = 0, n = word.size(); i < n; ++i)
118+
{
119+
char c = word[i];
120+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long) (i + 1) * (n - i);
121+
}
122+
return ans;
123+
}
124+
};
125+
```
91126
127+
### **Go**
128+
129+
```go
130+
func countVowels(word string) int64 {
131+
var ans int64
132+
n := len(word)
133+
for i, c := range word {
134+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
135+
ans += int64((i + 1) * (n - i))
136+
}
137+
}
138+
return ans
139+
}
92140
```
93141

94142
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
long long countVowels(string word) {
4+
long long ans = 0;
5+
for (int i = 0, n = word.size(); i < n; ++i)
6+
{
7+
char c = word[i];
8+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long) (i + 1) * (n - i);
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func countVowels(word string) int64 {
2+
var ans int64
3+
n := len(word)
4+
for i, c := range word {
5+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
6+
ans += int64((i + 1) * (n - i))
7+
}
8+
}
9+
return ans
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public long countVowels(String word) {
3+
long ans = 0;
4+
for (int i = 0, n = word.length(); i < n; ++i) {
5+
char c = word.charAt(i);
6+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
7+
ans += (long) (i + 1) * (n - i);
8+
}
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def countVowels(self, word: str) -> int:
3+
ans, n = 0, len(word)
4+
for i, c in enumerate(word):
5+
if c in ['a', 'e', 'i', 'o', 'u']:
6+
ans += (i + 1) * (n - i)
7+
return ans

0 commit comments

Comments
 (0)