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 <= 1000s consists of only lowercase English letters.Problem Overview: You are given a string s. The task is to remove every vowel (a, e, i, o, u) from the string and return the remaining characters in their original order. The result must preserve the relative order of non‑vowel characters.
Approach 1: Simulation with Vowel Check (O(n) time, O(n) space)
The most direct solution iterates through the string once and filters out vowels as you go. Create a set or constant string containing all vowels ("aeiou"). For each character in the input, check whether it exists in the vowel set. If it is not a vowel, append it to the result builder (such as a list, string builder, or buffer). Finally, join the collected characters into the resulting string.
The key insight is that you never need to modify the original string in place. Instead, construct the answer while scanning left to right. Using a set for vowel lookup keeps membership checks at O(1), making the entire pass linear. This pattern is a classic string filtering technique and appears frequently in problems that require removing or transforming specific characters.
Space complexity is O(n) because a new string is created to store the filtered characters. Time complexity is O(n), where n is the length of the string, since each character is processed exactly once. This approach works efficiently for both short and large inputs.
Approach 2: Character Filtering with Builder (O(n) time, O(n) space)
Another variation uses a dynamic string builder (or array buffer) and appends characters conditionally. Iterate through the string with a loop, check each character against the vowel set, and append only non‑vowel characters to the builder. Languages like Java and C++ benefit from this pattern because repeated string concatenation can be expensive without a builder structure.
This technique still performs a single pass over the string and relies on constant‑time membership checks. Internally, it behaves like a streaming filter where characters flow through a condition and only valid ones are kept. It is conceptually identical to a simulation but highlights efficient string construction patterns commonly used in production code.
Recommended for interviews: The simulation approach with a vowel set is what interviewers expect. It demonstrates that you can iterate through a string, apply conditional filtering, and build the result efficiently. Even though the problem is simple, writing the solution with clean iteration and O(n) complexity shows strong fundamentals in string processing and basic algorithm design.
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 O(n), where n is the length of the string. Ignoring the space consumption of the answer, the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulation with Vowel Set | O(n) | O(n) | General case. Fast membership checks using a set while iterating once through the string. |
| String Builder Filtering | O(n) | O(n) | Preferred in languages where repeated string concatenation is expensive; uses a builder or buffer for efficiency. |
Remove Vowels from a String • Kevin Naughton Jr. • 18,732 views views
Watch 9 more video solutions →Practice Remove Vowels from a String with our built-in code editor and test cases.
Practice on FleetCode