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
No vowels to remove
Empty string
Mixed case vowels removed
All vowels removed
Vowels removed
Algorithm Flow

Solution Approach
Best Answers
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();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
