Domain 1EasyCORE

Remove Vowels from a String

The problem requires removing all vowels from a given string `s`. The input is a single string consisting of lowercase English letters, and the output should be the same string with all vowels removed. The constraints specify that the length of the string will be between 1 and 1000 characters.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

1119. Remove Vowels from a String

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 1
Acceptance rate: 90.5%


Problem Breakdown

The problem requires removing all vowels from a given string s. The input is a single string consisting of lowercase English letters, and the output should be the same string with all vowels removed. The constraints specify that the length of the string will be between 1 and 1000 characters.

Implicit constraints include:

  • The string may contain no vowels.
  • The string may consist entirely of vowels.

Intuition

A brute-force approach would involve iterating through each character in the string, checking if it is a vowel, and building a new string without vowels. This approach has a time complexity of O(n) where n is the length of the string, but it involves creating multiple intermediate strings, which could be inefficient in terms of space.

A better approach is to use a set to store the vowels for quick lookup and build the result string by appending only non-vowel characters. This reduces the need for multiple intermediate strings and maintains linear time complexity while optimizing space usage.

Approach

  1. Define a set containing all vowels: {'a', 'e', 'i', 'o', 'u'}.
  2. Initialize an empty list to store the result characters.
  3. Iterate through each character in the input string:
    • If the character is not in the vowel set, append it to the result list.
  4. Join the list into a single string and return it.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string. We iterate through each character once.
  • Space Complexity: O(n), where n is the length of the input string. In the worst case, all characters are non-vowels, and we store them in the result list.

Solution

def removeVowels(s: str) -> str:
    vowels = {'a', 'e', 'i', 'o', 'u'}
    result = []
    
    for char in s:
        if char not in vowels:
            result.append(char)
    
    return ''.join(result)

Edge Cases Handled

  • No Vowels: If the input string contains no vowels, the output will be the same as the input.
  • All Vowels: If the input string consists entirely of vowels, the output will be an empty string.
  • Single Character: The function handles strings of length 1 correctly, returning an empty string if the character is a vowel or the character itself if it’s not.