1119. Remove Vowels from a String
leetcode easy
#csharp#easy#leetcode#string
Task
Дана строка s, удалите из нее гласные 'a', 'e', 'i', 'o' и 'u' и верните новую строку.
Пример:
Input: s = "leetcodeisacommunityforcoders"
Output: "ltcdscmmntyfrcdrs"
C# solution
matched/originalpublic class Solution {
public string RemoveVowels(string s) {
var ans = new StringBuilder();
foreach (char c in s) {
if (!IsVowel(c)) {
ans.Append(c);
}
}
return ans.ToString();
}
private bool IsVowel(char c) {
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u';
}
}C++ solution
auto-draft, review before submit#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public string RemoveVowels(string s) {
var ans = new StringBuilder();
foreach (char c in s) {
if (!IsVowel(c)) {
ans.Append(c);
}
}
return ans.ToString();
}
private bool IsVowel(char c) {
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u';
}
}Java solution
matched/originalclass Solution {
private boolean isVowel(char c) {
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u';
}
public String removeVowels(String s) {
StringBuffer ans = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
if (!isVowel(s.charAt(i))) {
ans.append(s.charAt(i));
}
}
return ans.toString();
}
}JavaScript solution
matched/originalvar removeVowels = function(s) {
const isVowel = c => 'aeiou'.includes(c);
let ans = '';
for (let char of s) {
if (!isVowel(char)) {
ans += char;
}
}
return ans;
};Python solution
matched/originalclass Solution:
def removeVowels(self, s: str) -> str:
def isVowel(c):
return c in 'aeiou'
ans = []
for char in s:
if not isVowel(char):
ans.append(char)
return ''.join(ans)Go solution
matched/originalpackage main
func removeVowels(s string) string {
var ans []rune
for _, c := range s {
if !isVowel(c) {
ans = append(ans, c)
}
}
return string(ans)
}
func isVowel(c rune) bool {
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'
}Explanation
Algorithm
1⃣Создайте метод isVowel(), который возвращает true, если переданный символ является одной из гласных [a, e, i, o, u], и false в противном случае.
2⃣Инициализируйте пустую строку ans.
3⃣Пройдитесь по каждому символу в строке s, и для каждого символа c проверьте, является ли он гласной, используя isVowel(c). Если нет, добавьте символ в строку ans. В конце верните строку ans.
😎