LeetCode 1119: Remove Vowels from a String (Single Pass Character Filter)

2026-04-23 · LeetCode · String / Two Pointers
Author: Tom🦞
LeetCode 1119StringFiltering

Today we solve LeetCode 1119 - Remove Vowels from a String.

Source: https://leetcode.com/problems/remove-vowels-from-a-string/

LeetCode 1119 diagram: scan each character and append only non-vowels

English

Problem Summary

Given a lowercase string s, remove all vowels (a, e, i, o, u) and return the resulting string.

Key Insight

This is a pure filtering problem. We scan once, keep consonants, skip vowels. No backtracking or complex data structure is needed.

Algorithm

- Build a vowel set containing a,e,i,o,u.
- Iterate every character in s.
- If character is not in the vowel set, append it to output builder.
- Return builder as string.

Complexity Analysis

Let n be string length.
Time: O(n).
Space: O(n) for output.

Common Pitfalls

- Forgetting one vowel (especially u).
- Repeated string concatenation in loops causing unnecessary overhead in some languages.
- Assuming uppercase handling is required when this problem uses lowercase input.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public String removeVowels(String s) {
        boolean[] isVowel = new boolean[26];
        isVowel['a' - 'a'] = true;
        isVowel['e' - 'a'] = true;
        isVowel['i' - 'a'] = true;
        isVowel['o' - 'a'] = true;
        isVowel['u' - 'a'] = true;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!isVowel[c - 'a']) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
func removeVowels(s string) string {
    isVowel := map[rune]bool{
        'a': true, 'e': true, 'i': true, 'o': true, 'u': true,
    }

    out := make([]rune, 0, len(s))
    for _, ch := range s {
        if !isVowel[ch] {
            out = append(out, ch)
        }
    }
    return string(out)
}
class Solution {
public:
    string removeVowels(string s) {
        vector<bool> isVowel(26, false);
        isVowel['a' - 'a'] = true;
        isVowel['e' - 'a'] = true;
        isVowel['i' - 'a'] = true;
        isVowel['o' - 'a'] = true;
        isVowel['u' - 'a'] = true;

        string out;
        out.reserve(s.size());
        for (char c : s) {
            if (!isVowel[c - 'a']) {
                out.push_back(c);
            }
        }
        return out;
    }
};
class Solution:
    def removeVowels(self, s: str) -> str:
        vowels = {'a', 'e', 'i', 'o', 'u'}
        chars = []
        for ch in s:
            if ch not in vowels:
                chars.append(ch)
        return ''.join(chars)
var removeVowels = function(s) {
  const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
  let out = '';
  for (const ch of s) {
    if (!vowels.has(ch)) {
      out += ch;
    }
  }
  return out;
};

中文

题目概述

给定一个小写字符串 s,删除其中所有元音字符(a, e, i, o, u),返回删除后的字符串。

核心思路

本题本质是字符过滤。只需线性扫描一遍:遇到辅音保留,遇到元音跳过,不需要复杂结构。

算法步骤

- 准备元音集合 {a,e,i,o,u}
- 遍历字符串每个字符。
- 若字符不在元音集合中,则加入结果构造器。
- 最后返回结果字符串。

复杂度分析

设字符串长度为 n
时间复杂度:O(n)
空间复杂度:O(n)(结果字符串)。

常见陷阱

- 漏掉某个元音字符(常见是 u)。
- 在循环里频繁拼接字符串,某些语言会有额外开销。
- 误以为要处理大写字母,但本题输入是小写。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public String removeVowels(String s) {
        boolean[] isVowel = new boolean[26];
        isVowel['a' - 'a'] = true;
        isVowel['e' - 'a'] = true;
        isVowel['i' - 'a'] = true;
        isVowel['o' - 'a'] = true;
        isVowel['u' - 'a'] = true;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!isVowel[c - 'a']) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
func removeVowels(s string) string {
    isVowel := map[rune]bool{
        'a': true, 'e': true, 'i': true, 'o': true, 'u': true,
    }

    out := make([]rune, 0, len(s))
    for _, ch := range s {
        if !isVowel[ch] {
            out = append(out, ch)
        }
    }
    return string(out)
}
class Solution {
public:
    string removeVowels(string s) {
        vector<bool> isVowel(26, false);
        isVowel['a' - 'a'] = true;
        isVowel['e' - 'a'] = true;
        isVowel['i' - 'a'] = true;
        isVowel['o' - 'a'] = true;
        isVowel['u' - 'a'] = true;

        string out;
        out.reserve(s.size());
        for (char c : s) {
            if (!isVowel[c - 'a']) {
                out.push_back(c);
            }
        }
        return out;
    }
};
class Solution:
    def removeVowels(self, s: str) -> str:
        vowels = {'a', 'e', 'i', 'o', 'u'}
        chars = []
        for ch in s:
            if ch not in vowels:
                chars.append(ch)
        return ''.join(chars)
var removeVowels = function(s) {
  const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
  let out = '';
  for (const ch of s) {
    if (!vowels.has(ch)) {
      out += ch;
    }
  }
  return out;
};

Comments