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 2801bae

Browse files
committed
feat: add solutions to lc problem: No.2062
No.2062.Count Vowel Substrings of a String
1 parent f5a316a commit 2801bae

File tree

9 files changed

+233
-17
lines changed

9 files changed

+233
-17
lines changed

‎solution/2000-2099/2062.Count Vowel Substrings of a String/README.md‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,101 @@
6767

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

70+
**方法一:暴力枚举**
71+
7072
<!-- tabs:start -->
7173

7274
### **Python3**
7375

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

7678
```python
77-
79+
class Solution:
80+
def countVowelSubstrings(self, word: str) -> int:
81+
n = len(word)
82+
s = set('aeiou')
83+
return sum(set(word[i: j]) == s for i in range(n) for j in range(i + 1, n + 1))
7884
```
7985

8086
### **Java**
8187

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

8490
```java
91+
class Solution {
92+
public int countVowelSubstrings(String word) {
93+
int n = word.length();
94+
int ans = 0;
95+
for (int i = 0; i < n; ++i) {
96+
Set<Character> t = new HashSet<>();
97+
for (int j = i; j < n; ++j) {
98+
char c = word.charAt(j);
99+
if (!isVowel(c)) {
100+
break;
101+
}
102+
t.add(c);
103+
if (t.size() == 5) {
104+
++ans;
105+
}
106+
}
107+
}
108+
return ans;
109+
}
85110

111+
private boolean isVowel(char c) {
112+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int countVowelSubstrings(string word) {
123+
int ans = 0;
124+
int n = word.size();
125+
for (int i = 0; i < n; ++i)
126+
{
127+
unordered_set<char> t;
128+
for (int j = i; j < n; ++j)
129+
{
130+
char c = word[j];
131+
if (!isVowel(c)) break;
132+
t.insert(c);
133+
ans += t.size() == 5;
134+
}
135+
}
136+
return ans;
137+
}
138+
139+
bool isVowel(char c) {
140+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
141+
}
142+
};
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
func countVowelSubstrings(word string) int {
149+
ans, n := 0, len(word)
150+
for i := range word {
151+
t := map[byte]bool{}
152+
for j := i; j < n; j++ {
153+
c := word[j]
154+
if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
155+
break
156+
}
157+
t[c] = true
158+
if len(t) == 5 {
159+
ans++
160+
}
161+
}
162+
}
163+
return ans
164+
}
86165
```
87166

88167
### **TypeScript**

‎solution/2000-2099/2062.Count Vowel Substrings of a String/README_EN.md‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,90 @@
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def countVowelSubstrings(self, word: str) -> int:
64+
n = len(word)
65+
s = set('aeiou')
66+
return sum(set(word[i: j]) == s for i in range(n) for j in range(i + 1, n + 1))
6367
```
6468

6569
### **Java**
6670

6771
```java
72+
class Solution {
73+
public int countVowelSubstrings(String word) {
74+
int n = word.length();
75+
int ans = 0;
76+
for (int i = 0; i < n; ++i) {
77+
Set<Character> t = new HashSet<>();
78+
for (int j = i; j < n; ++j) {
79+
char c = word.charAt(j);
80+
if (!isVowel(c)) {
81+
break;
82+
}
83+
t.add(c);
84+
if (t.size() == 5) {
85+
++ans;
86+
}
87+
}
88+
}
89+
return ans;
90+
}
91+
92+
private boolean isVowel(char c) {
93+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int countVowelSubstrings(string word) {
104+
int ans = 0;
105+
int n = word.size();
106+
for (int i = 0; i < n; ++i)
107+
{
108+
unordered_set<char> t;
109+
for (int j = i; j < n; ++j)
110+
{
111+
char c = word[j];
112+
if (!isVowel(c)) break;
113+
t.insert(c);
114+
ans += t.size() == 5;
115+
}
116+
}
117+
return ans;
118+
}
68119

120+
bool isVowel(char c) {
121+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
122+
}
123+
};
124+
```
125+
126+
### **Go**
127+
128+
```go
129+
func countVowelSubstrings(word string) int {
130+
ans, n := 0, len(word)
131+
for i := range word {
132+
t := map[byte]bool{}
133+
for j := i; j < n; j++ {
134+
c := word[j]
135+
if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
136+
break
137+
}
138+
t[c] = true
139+
if len(t) == 5 {
140+
ans++
141+
}
142+
}
143+
}
144+
return ans
145+
}
69146
```
70147

71148
### **TypeScript**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int countVowelSubstrings(string word) {
4+
int ans = 0;
5+
int n = word.size();
6+
for (int i = 0; i < n; ++i)
7+
{
8+
unordered_set<char> t;
9+
for (int j = i; j < n; ++j)
10+
{
11+
char c = word[j];
12+
if (!isVowel(c)) break;
13+
t.insert(c);
14+
ans += t.size() == 5;
15+
}
16+
}
17+
return ans;
18+
}
19+
20+
bool isVowel(char c) {
21+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
22+
}
23+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func countVowelSubstrings(word string) int {
2+
ans, n := 0, len(word)
3+
for i := range word {
4+
t := map[byte]bool{}
5+
for j := i; j < n; j++ {
6+
c := word[j]
7+
if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
8+
break
9+
}
10+
t[c] = true
11+
if len(t) == 5 {
12+
ans++
13+
}
14+
}
15+
}
16+
return ans
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int countVowelSubstrings(String word) {
3+
int n = word.length();
4+
int ans = 0;
5+
for (int i = 0; i < n; ++i) {
6+
Set<Character> t = new HashSet<>();
7+
for (int j = i; j < n; ++j) {
8+
char c = word.charAt(j);
9+
if (!isVowel(c)) {
10+
break;
11+
}
12+
t.add(c);
13+
if (t.size() == 5) {
14+
++ans;
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
21+
private boolean isVowel(char c) {
22+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def countVowelSubstrings(self, word: str) -> int:
3+
n = len(word)
4+
s = set('aeiou')
5+
return sum(set(word[i: j]) == s for i in range(n) for j in range(i + 1, n + 1))

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@
7777
```python
7878
class Solution:
7979
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
80+
n = len(word)
81+
return sum((i + 1) * (n - i) for i, c in enumerate(word) if c in 'aeiou')
8582
```
8683

8784
### **Java**

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ Hence, the total sum of vowels = 1 +たす 1 +たす 1 +たす 0 +たす 0 +たす 0 = 3.
6161
```python
6262
class Solution:
6363
def countVowels(self, word: str) -> int:
64-
ans, n = 0, len(word)
65-
for i, c in enumerate(word):
66-
if c in ['a', 'e', 'i', 'o', 'u']:
67-
ans += (i + 1) * (n - i)
68-
return ans
64+
n = len(word)
65+
return sum((i + 1) * (n - i) for i, c in enumerate(word) if c in 'aeiou')
6966
```
7067

7168
### **Java**
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class Solution:
22
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
3+
n = len(word)
4+
return sum((i + 1) * (n - i) for i, c in enumerate(word) if c in 'aeiou')

0 commit comments

Comments
(0)

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