1119. Remove Vowels from a String
Tags: ‘String’
Given a string S
, remove the vowels 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
from it, and return the new string.
Example 1:
Input: "leetcodeisacommunityforcoders" Output: "ltcdscmmntyfrcdrs"
Example 2:
Input: "aeiou" Output: ""
Note:
S
consists of lowercase English letters only.1 <= S.length <= 1000
Solution
public String removeVowels(String S) {
Set<Character> set = new HashSet<>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
set.add('A');
set.add('E');
set.add('I');
set.add('O');
set.add('U');
StringBuilder sb = new StringBuilder();
for (char c: S.toCharArray()) {
if (!set.contains(c)) {
sb.append(c);
}
}
return sb.toString();
}