Remove Vowels from a String
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u'
from it, and return the new string.
Examples
Example 1:
Input: s = "leetcodeisacommunityforcoders"
Output: "ltcdscmmntyfrcdrs"
Example 2:
Input: s = "aeiou"
Output: ""
Constraints:
1 <= s.length <= 1000sconsists of only lowercase English letters.
Solution
Method 1 - String Filtering
Intuition
We want to remove all vowels from the string. This can be done by iterating through the string and skipping any character that is a vowel.
Approach
- Define a set of vowels.
- Build a new string by including only non-vowel characters.
Code
C++
#include <string>
using namespace std;
string removeVowels(string s) {
string res;
for (char c : s) {
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u') res += c;
}
return res;
}
Go
func removeVowels(s string) string {
vowels := map[byte]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true}
res := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
if !vowels[s[i]] {
res = append(res, s[i])
}
}
return string(res)
}
Java
public class Solution {
public String removeVowels(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u') sb.append(c);
}
return sb.toString();
}
}
Kotlin
fun removeVowels(s: String): String {
return s.filter { it !in "aeiou" }
}
Python
def removeVowels(s: str) -> str:
return ''.join(c for c in s if c not in 'aeiou')
Rust
fn remove_vowels(s: &str) -> String {
s.chars().filter(|&c| !"aeiou".contains(c)).collect()
}
TypeScript
function removeVowels(s: string): string {
return s.split('').filter(c => !'aeiou'.includes(c)).join('');
}
Complexity
- ⏰ Time complexity: O(N), where N is the length of s.
- 🧺 Space complexity: O(N), for the output string.