AlgoMaster Logo

Remove All Vowels from a String

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

Return a new string with every vowel removed. A vowel is one of a, e, i, o, or u, and the check is case insensitive, so both a and A are dropped.

Note the phrase "new string." Strings cannot be shrunk in place in most languages, so rather than deleting characters from the original, create a fresh result and copy over only the characters to keep. Every consonant, digit, space, and punctuation mark stays. Only the five vowels in either case are filtered out.

One way to test for a vowel is a chain of comparisons against all ten lowercase and uppercase vowels. That works, but it is verbose. A set answers the membership question with a single check and makes the intent obvious.

Key Constraints:

  • 0 <= s.length <= 1000 → The string can be empty, in which case the result is empty too. The code must run cleanly when there is nothing to scan.
  • Case insensitive vowels → Both a and A are vowels, so the set must cover both cases.

Approach 1: Filter and Build

Intuition

Walk through the string one character at a time. For each character, ask: is this a vowel? If yes, skip it. If no, append it to the result.

Store both the lowercase and uppercase vowels in a set, which handles case insensitivity without converting each character first. By the end of the string, the result holds every non-vowel character in its original order.

Algorithm

  1. Define the set of vowels, covering both lowercase and uppercase.
  2. Create an empty buffer to build the result string.
  3. Loop through the input string one character at a time.
  4. If the current character is in the vowel set, skip it.
  5. Otherwise, append the character to the result.
  6. After the loop ends, return the assembled result string.

Example Walkthrough

1Index 0 is 'h', not a vowel. Keep it. result = "h".
0
i
h
1
e
2
l
3
l
4
o
1/5

Take s = "hello". Index 0 is h, not a vowel, so it is kept and the result is "h". Index 1 is e, a vowel, so it is skipped and the result stays "h". Index 2 is l, kept, giving "hl". Index 3 is l, kept, giving "hll". Index 4 is o, a vowel, so it is skipped, leaving the final result "hll". The two vowels were dropped while the three consonants moved into the result in their original order.

A set answers "is this a vowel?" in constant time regardless of how many items it holds. Scanning a list of vowels for every character would instead cost time proportional to the list size on each check. With only ten vowels the difference is small, but the set makes the linear running time easy to reason about.

0
h
1
l
2
l
result

Code