Talentd
FleetCode
Make and follow sheets.Sign in

Remove Vowels from a String solution and explanation

EasyString3 min read91.2% acceptanceUpdated 23 Mar 2026Premium elsewhere, free here
Remove Vowels from a StringKevin Naughton Jr. · 5:43 · 18K viewsWatch on YouTube
9 more videos on this problem
  1. Remove Vowels from a StringKevin Naughton Jr. · 5:43 · 18K views
  2. LeetCode 1119: Remove Vowels from a String - Interview Prep Ep 3Fisher Coder · 6:49 · 1K views
  3. Remove Vowels from a String | LeetCode 1119 | EnglishCodeClips with Abhishek Ranjan · 2:59 · 452 views
  4. Leetcode Solution - 1119 Remove Vowels from a StringJohn Leonardo · 2:15 · 374 views
  5. LeetCode 1119: Remove Vowels from a String: Python EasyHashem Alsaket · 2:15 · 360 views
  6. LeetCode 1119: Remove vowels from string in pythonThe Coding Manual - ICCEICQ · 2:37 · 224 views
  7. Leetcode 1119 - Remove Vowels From a String (JAVA Solution Explained!)LC Bear · 2:21 · 216 views
  8. LeetCode 1119: Remove Vowels from a StringTech Careers in USA · 6:07 · 212 views
  9. leetcode 1119. Remove Vowels from a String - list comprehension and string joinCode-Yao · 3:23 · 184 views
  10. 1119. Remove Vowels from a String - PythonMark Mariscal · 4:19 · 166 views

The problem

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

class Solution:
    def removeVowels(self, s: str) -> str:
        return "".join(c for c in s if c not in "aeiou")
class Solution {
    public String removeVowels(String s) {
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
                ans.append(c);
            }
        }
        return ans.toString();
    }
}
class Solution {
public:
    string removeVowels(string s) {
        string ans;
        for (char& c : s) {
            if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
                ans += c;
            }
        }
        return ans;
    }
};
function removeVowels(s: string): string {
    return s.replace(/[aeiou]/g, '');
}
func removeVowels(s string) string {
	ans := []rune{}
	for _, c := range s {
		if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
			ans = append(ans, c)
		}
	}
	return string(ans)
}
Try this approach in the editor

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.

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.

Comments

Now write it yourselfClose this page and solve Remove Vowels from a String in the editor, without looking.
Practise Remove Vowels from a String