Remove Vowels from a String - Problem

Given a string s, your task is to remove all vowels from it and return the resulting string. The vowels to remove are the five standard English vowels: 'a', 'e', 'i', 'o', and 'u' (both lowercase and uppercase).

This is a fundamental string manipulation problem that tests your understanding of character filtering and string building techniques. It's commonly used as a warm-up question in coding interviews to assess basic programming skills.

Example:

  • Input: "hello world"
  • Output: "hll wrld"
  • Explanation: We remove 'e', 'o', 'o' from the original string

Input & Output

example_1.py โ€” Basic String
$ Input: s = "hello"
โ€บ Output: "hll"
๐Ÿ’ก Note: We remove the vowels 'e' and 'o' from "hello", leaving us with "hll"
example_2.py โ€” Mixed Case
$ Input: s = "AEIOU"
โ€บ Output: ""
๐Ÿ’ก Note: All characters are vowels (uppercase), so the result is an empty string
example_3.py โ€” No Vowels
$ Input: s = "bcdfg"
โ€บ Output: "bcdfg"
๐Ÿ’ก Note: No vowels present in the input, so the string remains unchanged

Constraints

  • 0 โ‰ค s.length โ‰ค 1000
  • s consists of only lowercase and uppercase English letters
  • Vowels are case-sensitive: both 'a' and 'A' should be removed

Visualization

Tap to expand
Vowel Removal VisualizationVowel Filter Set{a, e, i, o, u, A, E, I, O, U}O(1) Lookup Time โšกProcessing: "Hello World"Hello WorldVowels Filtered OutResult: "Hll Wrld"Hll WrldEfficiencyTime: O(n)Space: O(n)
Understanding the Visualization
1
Create the Filter
Set up a hash set with all vowels for instant recognition
2
Process Each Character
Check each character against our filter in constant time
3
Build Result
Collect all non-vowel characters efficiently
4
Return Clean String
Return the filtered string with all vowels removed
Key Takeaway
๐ŸŽฏ Key Insight: Using a hash set for vowel lookup transforms multiple comparisons per character into a single O(1) operation, making the algorithm both faster and more maintainable.
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
28.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen