Skip to content

Latest commit

 

History

History
78 lines (53 loc) · 1.78 KB

File metadata and controls

78 lines (53 loc) · 1.78 KB

English Version

题目描述

给你一个字符串 S,请你删去其中的所有元音字母( 'a''e''i''o''u'),并返回这个新字符串。

 

示例 1:

输入:"leetcodeisacommunityforcoders"
输出:"ltcdscmmntyfrcdrs"

示例 2:

输入:"aeiou"
输出:""

 

提示:

  1. S 仅由小写英文字母组成。
  2. 1 <= S.length <= 1000

解法

Python3

class Solution:
    def removeVowels(self, s: str) -> str:
        res = []
        for c in s:
            if c not in {'a', 'e', 'i', 'o', 'u'}:
                res.append(c)
        return ''.join(res)

Java

class Solution {
    public String removeVowels(String s) {
        StringBuilder res = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
                res.append(c);
            }
        }
        return res.toString();
    }
}

...