Code Logo

Remove Vowels

Published at25 Jul 2026
Easy 0 views
Like0

Given a string, remove all vowels (a, e, i, o, u) from it, both uppercase and lowercase, and return the resulting string with only consonants and other characters remaining.

Iterate through each character of the input string. Build a new string containing only characters that are not vowels. Check each character against the set of vowels (a, e, i, o, u, A, E, I, O, U). If the character is not a vowel, append it to the result.

This is a basic string filtering problem that tests character-by-character processing. The solution uses O(n) time where n is the string length, and O(n) space for the result string.

Edge cases include empty strings, strings with only vowels, strings with no vowels, and strings with mixed case vowels.

Example Input & Output

Example 1
Input
"bcdfg"
Output
"bcdfg"
Explanation

No vowels to remove

Example 2
Input
""
Output
""
Explanation

Empty string

Example 3
Input
"Hello World"
Output
"Hll Wrld"
Explanation

Mixed case vowels removed

Example 4
Input
"AEIOU"
Output
""
Explanation

All vowels removed

Example 5
Input
"hello world"
Output
"hll wrld"
Explanation

Vowels removed

Algorithm Flow

Recommendation Algorithm Flow for Remove Vowels
Recommendation Algorithm Flow for Remove Vowels

Solution Approach

function solution(s) {
  var result = '';
  var vowels = 'aeiouAEIOU';
  for (var i = 0; i < s.length; i++) {
    if (vowels.indexOf(s.charAt(i)) === -1) {
      result += s.charAt(i);
    }
  }
  return result;
}

Best Answers

java
class Solution {
    public String solution(String s) {
        StringBuilder sb=new StringBuilder();
        String v="aeiouAEIOU";
        for(int i=0;i<s.length();i++){
            if(v.indexOf(s.charAt(i))==-1)sb.append(s.charAt(i));
        }return sb.toString();
    }
}