Skip to content

1119. Remove Vowels from a String

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  string removeVowels(string s) {
    s.erase(remove_if(s.begin(), s.end(), [&](char c) { return isVowel(c); }),
            s.end());
    return s;
  }

  bool isVowel(char c) {
    static constexpr string_view kVowels = "aeiou";
    return kVowels.find(c) != string_view::npos;
  }
};
1
2
3
4
5
class Solution {
  public String removeVowels(String s) {
    return s.replaceAll("[aeiou]", "");
  }
}
1
2
3
class Solution:
  def removeVowels(self, s: str) -> str:
    return re.sub('a|e|i|o|u', '', s)