Remove Vowels from a String - Problem

Given a string s, remove all vowels ('a', 'e', 'i', 'o', and 'u') from it and return the new string.

Note: The vowels are case-sensitive, so only lowercase vowels should be removed.

Input & Output

Example 1 — Basic Case
$ Input: s = "leetcode"
Output: "ltcd"
💡 Note: Remove vowels 'e', 'e', 'o', 'e' from "leetcode" to get "ltcd"
Example 2 — Mixed Case
$ Input: s = "aEiOu"
Output: "EOU"
💡 Note: Only lowercase vowels are removed: 'a', 'i', 'u'. Uppercase 'E', 'O', 'U' remain
Example 3 — No Vowels
$ Input: s = "bcdfg"
Output: "bcdfg"
💡 Note: String contains no lowercase vowels, so it remains unchanged

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase and uppercase English letters

Visualization

Tap to expand
Remove Vowels from a String INPUT String s = "leetcode" l e e t c o d e 0 1 2 3 4 5 6 7 = Vowel (to remove) = Consonant (keep) Vowels Set: { a, e, i, o, u } Input: s = "leetcode" ALGORITHM STEPS StringBuilder Approach 1 Initialize StringBuilder Create empty StringBuilder 2 Iterate Each Character Loop through string s 3 Check if Not Vowel Skip if char in {a,e,i,o,u} 4 Append to Result Add consonant to builder StringBuilder Progress: "" --> "l" --> "lt" --> "ltc" --> "ltcd" Complexity: Time: O(n) | Space: O(n) FINAL RESULT Vowels Removed: l t c d Removed vowels: e e o e Original length: 8 Vowels removed: 4 Result length: 4 Output: "ltcd" OK Key Insight: Using StringBuilder instead of string concatenation avoids creating new string objects on each append operation. This reduces time complexity from O(n^2) to O(n) since StringBuilder uses a mutable character array internally, making it optimal for building strings iteratively. TutorialsPoint - Remove Vowels from a String | StringBuilder/List Approach
Asked in
Facebook 15 Google 12
26.4K Views
Medium Frequency
~10 min Avg. Time
850 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