Skip to content

Commit 3c2ebb8

Browse files
committed
feat: add solutions to lc problem: No.1119
No.1119.Remove Vowels from a String
1 parent 079c84f commit 3c2ebb8

File tree

6 files changed

+107
-27
lines changed

6 files changed

+107
-27
lines changed

solution/1100-1199/1119.Remove Vowels from a String/README.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
**方法一:模拟**
41+
42+
我们直接按照题目要求,遍历字符串,将不是元音字母的字符拼接到结果字符串中即可。
43+
44+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。忽略答案字符串的空间消耗,空间复杂度 $O(1)$。
45+
4046
<!-- tabs:start -->
4147

4248
### **Python3**
@@ -46,11 +52,7 @@
4652
```python
4753
class Solution:
4854
def removeVowels(self, s: str) -> str:
49-
res = []
50-
for c in s:
51-
if c not in {'a', 'e', 'i', 'o', 'u'}:
52-
res.append(c)
53-
return ''.join(res)
55+
return "".join(c for c in s if c not in "aeiou")
5456
```
5557

5658
### **Java**
@@ -60,14 +62,46 @@ class Solution:
6062
```java
6163
class Solution {
6264
public String removeVowels(String s) {
63-
StringBuilder res = new StringBuilder();
64-
for (char c : s.toCharArray()) {
65+
StringBuilder ans = new StringBuilder();
66+
for (int i = 0; i < s.length(); ++i) {
67+
char c = s.charAt(i);
68+
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
69+
ans.append(c);
70+
}
71+
}
72+
return ans.toString();
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
string removeVowels(string s) {
83+
string ans;
84+
for (char& c : s) {
6585
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
66-
res.append(c);
86+
ans += c;
6787
}
6888
}
69-
return res.toString();
89+
return ans;
7090
}
91+
};
92+
```
93+
94+
### **Go**
95+
96+
```go
97+
func removeVowels(s string) string {
98+
ans := []rune{}
99+
for _, c := range s {
100+
if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
101+
ans = append(ans, c)
102+
}
103+
}
104+
return string(ans)
71105
}
72106
```
73107

solution/1100-1199/1119.Remove Vowels from a String/README_EN.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,57 @@
3838
```python
3939
class Solution:
4040
def removeVowels(self, s: str) -> str:
41-
res = []
42-
for c in s:
43-
if c not in {'a', 'e', 'i', 'o', 'u'}:
44-
res.append(c)
45-
return ''.join(res)
41+
return "".join(c for c in s if c not in "aeiou")
4642
```
4743

4844
### **Java**
4945

5046
```java
5147
class Solution {
5248
public String removeVowels(String s) {
53-
StringBuilder res = new StringBuilder();
54-
for (char c : s.toCharArray()) {
49+
StringBuilder ans = new StringBuilder();
50+
for (int i = 0; i < s.length(); ++i) {
51+
char c = s.charAt(i);
5552
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
56-
res.append(c);
53+
ans.append(c);
5754
}
5855
}
59-
return res.toString();
56+
return ans.toString();
6057
}
6158
}
6259
```
6360

61+
### **C++**
62+
63+
```cpp
64+
class Solution {
65+
public:
66+
string removeVowels(string s) {
67+
string ans;
68+
for (char& c : s) {
69+
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
70+
ans += c;
71+
}
72+
}
73+
return ans;
74+
}
75+
};
76+
```
77+
78+
### **Go**
79+
80+
```go
81+
func removeVowels(s string) string {
82+
ans := []rune{}
83+
for _, c := range s {
84+
if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
85+
ans = append(ans, c)
86+
}
87+
}
88+
return string(ans)
89+
}
90+
```
91+
6492
### **...**
6593

6694
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
string removeVowels(string s) {
4+
string ans;
5+
for (char& c : s) {
6+
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
7+
ans += c;
8+
}
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func removeVowels(s string) string {
2+
ans := []rune{}
3+
for _, c := range s {
4+
if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
5+
ans = append(ans, c)
6+
}
7+
}
8+
return string(ans)
9+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public String removeVowels(String s) {
3-
StringBuilder res = new StringBuilder();
4-
for (char c : s.toCharArray()) {
3+
StringBuilder ans = new StringBuilder();
4+
for (int i = 0; i < s.length(); ++i) {
5+
char c = s.charAt(i);
56
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
6-
res.append(c);
7+
ans.append(c);
78
}
89
}
9-
return res.toString();
10+
return ans.toString();
1011
}
1112
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
class Solution:
22
def removeVowels(self, s: str) -> str:
3-
res = []
4-
for c in s:
5-
if c not in {'a', 'e', 'i', 'o', 'u'}:
6-
res.append(c)
7-
return ''.join(res)
3+
return "".join(c for c in s if c not in "aeiou")

0 commit comments

Comments
 (0)