Find First Occurrence
Given a string, remove all vowel characters (a, e, i, o, u) both lowercase and uppercase, and return the resulting string with only consonants and non-letter characters remaining.
For example, removing vowels from "hello" produces "hll". From "AEIOU" produces "". From "xyz" produces "xyz". An empty string returns "".
Removing vowels is a simple text filtering operation that tests character-by-character iteration and conditional skipping. It is used in text analysis, word games, and creating abbreviations.
The solution defines a set of vowels and iterates through the string, building a result string that excludes any character found in the vowel set.
Edge cases include an empty string (return ""), a string with only vowels (return ""), a string with no vowels (return unchanged), and mixed case (both uppercase and lowercase vowels are removed).
Example Input & Output
"sad" occurs at index 0 and 6. The first occurrence is at index 0.
"leeto" did not occur in "leetcode", so we return -1.
Algorithm Flow
Solution Approach
Iterate through the string and build a new string excluding vowels.
Create a set of vowel characters (both cases). Loop through each character. If the character is not in the vowel set, append it to the result. Return the filtered result.
Time complexity is O(n), space complexity is O(n) for the result.
Best Answers
class Solution {
public String reverse_string_simple(String s) {
return new StringBuilder(s).reverse().toString();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
