comments | difficulty | edit_url | rating | source | tags | |
---|---|---|---|---|---|---|
true |
Easy |
1232 |
Biweekly Contest 4 Q2 |
|
Given a string s
, remove the vowels 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
from it, and return the new string.
Example 1:
Input: s = "leetcodeisacommunityforcoders" Output: "ltcdscmmntyfrcdrs"
Example 2:
Input: s = "aeiou" Output: ""
Constraints:
1 <= s.length <= 1000
s
consists of only lowercase English letters.
We can directly traverse the string according to the requirements of the problem, and append characters that are not vowels to the result string.
The time complexity is
class Solution:
def removeVowels(self, s: str) -> str:
return "".join(c for c in s if c not in "aeiou")
class Solution {
public String removeVowels(String s) {
StringBuilder ans = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
ans.append(c);
}
}
return ans.toString();
}
}
class Solution {
public:
string removeVowels(string s) {
string ans;
for (char& c : s) {
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
ans += c;
}
}
return ans;
}
};
func removeVowels(s string) string {
ans := []rune{}
for _, c := range s {
if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
ans = append(ans, c)
}
}
return string(ans)
}
function removeVowels(s: string): string {
return s.replace(/[aeiou]/g, '');
}