beginnerString ProblemsJava

Remove All Vowels From a String

Build a new string containing every character of the input except the vowels.

Quick Answer

Walk the string one character at a time and append each character to a builder only if it is not a vowel. Lowercase the character before testing so both cases are caught. Non-letters like spaces pass through unchanged. This runs in O(n) time.

Problem Statement

Given a string, remove all vowels (a, e, i, o, u, in either case) and return the remaining characters in their original order. Every non-vowel character, including spaces and punctuation, should be kept.

For example, hello world becomes hll wrld.

Input: A single string s.

Output: The string with all vowels removed.

Examples

Example 1
Input:  "hello world"
Output: hll wrld

The vowels e, o and o are removed; the space stays.

Example 2
Input:  "Programming"
Output: Prgrmmng

o, a and i are removed, leaving the consonants in order.

Constraints

  • 0 <= s.length <= 10^5
  • Remove both uppercase and lowercase vowels

Think Before You Code

Reveal the questions to ask yourself first
  • How do you test whether a single character is a vowel?
  • How do you handle both uppercase and lowercase vowels with one check?
  • What should happen to spaces and punctuation?
  • How do you assemble the surviving characters efficiently?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Lowercase each character before comparing so you only list a, e, i, o, u once.
Hint 2
Append a character to your result only when it is not one of the five vowels.
Hint 3
Use a StringBuilder so appending stays efficient instead of creating new strings.

Approach

Reveal the step-by-step approach

Filter the string in a single pass.

  1. Create an empty StringBuilder.
  2. For each character c, compute its lowercase form.
  3. If the lowercase form is a, e, i, o or u, skip it.
  4. Otherwise append the original character to the builder.
  5. Return the builder's contents.

Testing the lowercase form catches both A and a, while appending the original character preserves the input's letter casing for everything kept.

Dry Run

Walk through the example step by step

Removing vowels from "hello world":

char | lower | vowel? | result so far
-----+-------+--------+--------------
h    | h     | no     | h
e    | e     | yes    | h
l    | l     | no     | hl
l    | l     | no     | hll
o    | o     | yes    | hll
(sp) | (sp)  | no     | "hll "
w    | w     | no     | hll w
o    | o     | yes    | hll w
r    | r     | no     | hll wr
l    | l     | no     | hll wrl
d    | d     | no     | hll wrld
final: "hll wrld"

Solution

Reveal the full Java solution
public class RemoveVowels {
    public static String removeVowels(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            char lower = Character.toLowerCase(c);
            if (lower != 'a' && lower != 'e' && lower != 'i'
                    && lower != 'o' && lower != 'u') {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(removeVowels("hello world")); // hll wrld
        System.out.println(removeVowels("Programming")); // Prgrmmng
    }
}

Each character is tested once against the five vowels using its lowercase form, so a single condition covers both cases. Characters that are not vowels — letters, spaces, digits and punctuation — are appended unchanged, preserving order and original casing. The StringBuilder keeps appends cheap compared to repeated string concatenation.

Time: O(n) where n is the string lengthSpace: O(n) for the output string

Common Mistakes

  • Only removing lowercase vowels and leaving A, E, I, O, U behind.
  • Building the result with += on a String, creating a new object each time.
  • Accidentally removing the letter 'y' as if it were a vowel.

Edge Cases to Test

  • An empty string returns an empty string.
  • A string with no vowels is returned unchanged.
  • A string of only vowels, like "aeiou", returns an empty string.

Interview Follow-Ups

  • How would you also treat 'y' as a vowel to remove?
  • How would you remove vowels in place if given a char array?
  • How would you count the vowels you removed at the same time?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us