Remove Vowels from String
This question is asked by Amazon. Given a string s, remove all the vowels it contains and return the resulting string.
Note: In this problem y is not considered a vowel.
Example(s)
Example 1:
Input: s = "aeiou"
Output: ""
Explanation:
All characters are vowels (a, e, i, o, u), so removing them results in an empty string.
Example 2:
Input: s = "byte"
Output: "byt"
Explanation:
- 'b' is not a vowel → keep
- 'y' is not a vowel (per problem note) → keep
- 't' is not a vowel → keep
- 'e' is a vowel → remove
Result: "byt"
Example 3:
Input: s = "xyz"
Output: "xyz"
Explanation:
- 'x' is not a vowel → keep
- 'y' is not a vowel → keep
- 'z' is not a vowel → keep
All characters are kept: "xyz"
Solution
The solution uses string filtering:
- Define vowels: Create a set of vowels (a, e, i, o, u)
- Filter characters: Keep only non-vowel characters
- Return result: Return the filtered string
- JavaScript Solution
- Python Solution
JavaScript Solution - Filter
/**
* Remove all vowels from string
* @param {string} s - Input string
* @return {string} - String with vowels removed
*/
function removeVowels(s) {
// Define vowels (y is not considered a vowel)
const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
// Filter out vowels
return s.split('').filter(char => !vowels.has(char)).join('');
}
// Test cases
console.log('Example 1:', removeVowels("aeiou"));
// ""
console.log('Example 2:', removeVowels("byte"));
// "byt"
console.log('Example 3:', removeVowels("xyz"));
// "xyz"
console.log('Test 4:', removeVowels("leetcode"));
// "ltcd"Output:
Click "Run Code" to execute the code and see the results.
Python Solution - Filter
def remove_vowels(s: str) -> str:
"""
Remove all vowels from string
Args:
s: Input string
Returns:
str: String with vowels removed
"""
# Define vowels (y is not considered a vowel)
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
# Filter out vowels
return ''.join(char for char in s if char not in vowels)
# Test cases
print('Example 1:', remove_vowels("aeiou"))
# ""
print('Example 2:', remove_vowels("byte"))
# "byt"
print('Example 3:', remove_vowels("xyz"))
# "xyz"
print('Test 4:', remove_vowels("leetcode"))
# "ltcd"Loading Python runtime...
Output:
Click "Run Code" to execute the code and see the results.
Alternative Solution (String Replacement)
Here's a version using string replacement:
- JavaScript Replace
- Python Replace
/**
* Using string replacement
*/
function removeVowelsReplace(s) {
return s.replace(/[aeiouAEIOU]/g, '');
}
import re
def remove_vowels_replace(s: str) -> str:
"""
Using string replacement
"""
return re.sub(r'[aeiouAEIOU]', '', s)
Alternative Solution (String Builder)
Here's a version using string building:
- JavaScript Builder
- Python Builder
/**
* Using string builder
*/
function removeVowelsBuilder(s) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
let result = '';
for (const char of s) {
if (!vowels.has(char)) {
result += char;
}
}
return result;
}
def remove_vowels_builder(s: str) -> str:
"""
Using string builder
"""
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
result = []
for char in s:
if char not in vowels:
result.append(char)
return ''.join(result)
Complexity
- Time Complexity: O(n) - Where n is the length of the string. We process each character once.
- Space Complexity: O(n) - For the result string (or O(1) if modifying in-place, but strings are immutable in most languages).
Approach
The solution uses string filtering:
-
Define vowels:
- Create a set of vowels: a, e, i, o, u (both lowercase and uppercase)
- Note: y is NOT considered a vowel
-
Filter characters:
- Iterate through each character
- Keep only characters that are not vowels
-
Return result:
- Join the filtered characters into a string
- Return the result
Key Insights
- Set for vowels: Efficient O(1) lookup for vowel checking
- Filter approach: Clean and readable
- Case insensitive: Handle both uppercase and lowercase vowels
- Y is not a vowel: Important constraint in this problem
- O(n) time: Must process each character
Step-by-Step Example
Let's trace through Example 2: s = "byte"
s = "byte"
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
Process each character:
'b': vowels.has('b')? No → keep
'y': vowels.has('y')? No → keep (y is not a vowel)
't': vowels.has('t')? No → keep
'e': vowels.has('e')? Yes → remove
Filtered: ['b', 'y', 't']
Join: "byt"
Result: "byt"
Visual Representation
Example 1: s = "aeiou"
Characters: a e i o u
Vowels? ✓ ✓ ✓ ✓ ✓
Keep? ✗ ✗ ✗ ✗ ✗
Result: "" (all removed)
Example 2: s = "byte"
Characters: b y t e
Vowels? ✗ ✗ ✗ ✓
Keep? ✓ ✓ ✓ ✗
Result: "byt"
Example 3: s = "xyz"
Characters: x y z
Vowels? ✗ ✗ ✗
Keep? ✓ ✓ ✓
Result: "xyz" (all kept)
Edge Cases
- All vowels: Returns empty string
- No vowels: Returns original string
- Empty string: Returns empty string
- Mixed case: Handles both uppercase and lowercase
- Single character: Works correctly
- Only y: Returns "y" (y is not a vowel)
Important Notes
- Y is not a vowel: Important constraint in this problem
- Case insensitive: Handle both uppercase and lowercase
- Set lookup: O(1) time for vowel checking
- O(n) time: Must process each character
- O(n) space: For the result string
Vowel Definition
Standard vowels: a, e, i, o, u
- Both lowercase and uppercase: A, E, I, O, U
- Y is NOT a vowel in this problem (unlike some contexts)
Related Problems
- Reverse Vowels of a String: Different problem
- Longest Substring Without Repeating Characters: Different problem
- Valid Anagram: Different string problem
- Remove Duplicates: Different problem
Takeaways
- Set for vowels provides efficient O(1) lookup
- Filter approach is clean and readable
- Y is not a vowel is an important constraint
- O(n) time is optimal (must process each character)
- Case handling for both uppercase and lowercase