1119. Remove Vowels from a String

2019/10/24 Leetcode

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:

  1. S consists of lowercase English letters only.
  2. 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();
}

Search

    Table of Contents