Skip to main content

Remove Vowels from a String - Solution & Explanation

EasyPremiumFree on FleetCodeString5 min readAsked at: Amazon
Practice this problem

Problem Statement

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.

Approach Overview

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.

Solution

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simulation with Vowel SetO(n)O(n)General case. Fast membership checks using a set while iterating once through the string.
String Builder FilteringO(n)O(n)Preferred in languages where repeated string concatenation is expensive; uses a builder or buffer for efficiency.

Video Solution

Remove Vowels from a String • Kevin Naughton Jr. • 18,743 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Remove Vowels from a String easy or hard?
Remove Vowels from a String is classified as an Easy problem. It focuses on basic string traversal and filtering without requiring advanced data structures or algorithms.
Remove Vowels from a String Python/Java solution
In Python, iterate through the string and append non-vowel characters to a list, then join them into a string. In Java, loop through the characters and append valid ones to a StringBuilder. Both implementations follow the same O(n) simulation pattern.
How to solve Remove Vowels from a String in O(n)?
Iterate through the string from left to right and check whether each character is a vowel. Maintain a set like "aeiou" for constant-time membership checks. Append only the characters that are not vowels to a result builder, then join them into the final string. The single traversal guarantees O(n) time complexity.
What is the best approach for Remove Vowels from a String?
The optimal approach is a single-pass simulation that scans the string and skips characters that are vowels. Store vowels in a set such as {a, e, i, o, u} for constant-time checks. Append only non-vowel characters to the result. This runs in O(n) time and O(n) space.
Is Remove Vowels from a String asked at Google/Amazon/Meta?
Problems involving string filtering and character processing frequently appear in interviews at companies like Amazon, Google, and Meta. While this exact problem is simple, it tests core skills such as iteration, conditional checks, and efficient string construction.
What data structure is used in Remove Vowels from a String?
A hash set or constant lookup structure is typically used to store vowels for O(1) membership checks. The result is built using a list, string builder, or dynamic character buffer before converting it back into a string.
What is the time complexity of Remove Vowels from a String?
The standard solution runs in O(n) time where n is the length of the string. Each character is visited exactly once and checked against a vowel set with O(1) lookup. Space complexity is O(n) because a new string is created to store the filtered characters.

Ready to solve this problem?

Practice Remove Vowels from a String with our built-in code editor and test cases.

Practice on FleetCode