Last Updated: June 7, 2026
Return a new string with every vowel removed. A vowel is one of a, e, i, o, or u, and the check is case insensitive, so both a and A are dropped.
Note the phrase "new string." Strings cannot be shrunk in place in most languages, so rather than deleting characters from the original, create a fresh result and copy over only the characters to keep. Every consonant, digit, space, and punctuation mark stays. Only the five vowels in either case are filtered out.
One way to test for a vowel is a chain of comparisons against all ten lowercase and uppercase vowels. That works, but it is verbose. A set answers the membership question with a single check and makes the intent obvious.
0 <= s.length <= 1000 → The string can be empty, in which case the result is empty too. The code must run cleanly when there is nothing to scan.a and A are vowels, so the set must cover both cases.Walk through the string one character at a time. For each character, ask: is this a vowel? If yes, skip it. If no, append it to the result.
Store both the lowercase and uppercase vowels in a set, which handles case insensitivity without converting each character first. By the end of the string, the result holds every non-vowel character in its original order.
Take s = "hello". Index 0 is h, not a vowel, so it is kept and the result is "h". Index 1 is e, a vowel, so it is skipped and the result stays "h". Index 2 is l, kept, giving "hl". Index 3 is l, kept, giving "hll". Index 4 is o, a vowel, so it is skipped, leaving the final result "hll". The two vowels were dropped while the three consonants moved into the result in their original order.
A set answers "is this a vowel?" in constant time regardless of how many items it holds. Scanning a list of vowels for every character would instead cost time proportional to the list size on each check. With only ten vowels the difference is small, but the set makes the linear running time easy to reason about.