Remove Vowels from a String

Updated on 24 June, 2025
Remove Vowels from a String header image

Problem Statement

In this task, you are provided with a string s, which exclusively contains lowercase English letters. Your objective is to create a new string by removing all the vowel characters from s. The vowels in this context are the characters 'a', 'e', 'i', 'o', and 'u'. Once these characters are removed from the string, the resultant string should be returned. This process is intended to filter out vowels and return a consonant-only version of the input string, which can be an empty string if s originally contains only vowels.

Examples

Example 1

Input:

s = "vultrhostsdevelopers"

Output:

"vltrhstsdvlprs"

Example 2

Input:

s = "aeiou"

Output:

""

Constraints

  • 1 <= s.length <= 1000
  • s consists of only lowercase English letters.

Approach and Intuition

To solve this problem, follow these steps:

  1. Define the string of vowels that need to be removed: 'aeiou'.
  2. Traverse each character in the input string s.
  3. Check if the character is not a vowel and then append it to a new string.
  4. Return the newly constructed string which contains only the consonants or may be empty if no consonants were present.

Key Considerations:

  • Edge Cases:

    • If s is completely made up of vowels, your function should return an empty string, just like in s = "aeiou".
    • It's crucial that the function processes up to 1000 characters efficiently, as this is the upper limit for the length of s according to given constraints.
  • Efficiency: The solution should traverse the string once, checking each character, resulting in a time complexity of O(n), which is efficient given the constraints.

This straightforward approach ensures that all vowels are systematically checked and removed, and guarantees the construction of the output string with only the desired characters.

Solutions

  • C++
  • Java
cpp
class Solution {
public:
    bool checkVowel(char ch) {
        // List all lowercase vowels
        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
    }

    string deleteVowels(string str) {
        string result;

        for (char letter : str) {
            if (!checkVowel(letter)) {
                result += letter;
            }
        }
        
        return result;
    }
};

The given C++ solution consists of a class named Solution that provides a method to remove vowels from a string. The "deleteVowels" function iterates through each character of the input string. It uses the helper function "checkVowel" to determine if a character is a vowel or not. If it's not a vowel, the character is appended to the result string. This technique ensures that the returned string contains only non-vowel characters from the original string.

  • Key components of the code:
    • checkVowel function: Determines if a character is a vowel (a, e, i, o, u) using a simple condition checking.
    • deleteVowels function: Utilizes the checkVowel function to filter out vowels from the input string and constructs the resultant no-vowel string.

By using this approach, characters are efficiently checked and concatenated only if they are not vowels, resulting in the desired output string. This method is straightforward and effective for string manipulation that targets specific character removal based on given conditions.

java
class Solution {
    private boolean checkVowel(char character) {
        return "aeiou".indexOf(character) != -1;
    }
    
    public String deleteVowels(String input) {
        StringBuilder result = new StringBuilder(input.length());
        
        for (int index = 0; index < input.length(); index++) {
            if (!checkVowel(input.charAt(index))) {
                result.append(input.charAt(index));
            }
        }
        
        return result.toString();
    }
}

The provided Java code defines a class Solution with methods for removing all vowels from a string. Follow these steps to understand how the solution works:

  1. The checkVowel method checks if a given character is a vowel by determining if it is present in the string "aeiou". It returns true if the character is a vowel, otherwise false.

  2. The deleteVowels method accepts a string input and initializes a StringBuilder which helps in constructing the final string without vowels.

  3. Iterate through each character of the input string using a for loop.

  4. Check each character of the string by invoking the checkVowel method. If checkVowel returns false, the character is not a vowel and is then appended to the StringBuilder object.

  5. Finally, the StringBuilder object is converted back to a string and returned, representing the input string stripped of all vowels.

This concise and efficient approach ensures that all vowels are removed from any provided string using the Java language.

Comments

No comments yet.