1119. Remove Vowels from a String - Easy

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

 

class 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');
        
        char[] chs = S.toCharArray();
        int i = 0, j = 0;
        while(j < chs.length) {
            if(set.contains(chs[j])) {
                j++;
            } else {
                chs[i++] = chs[j++];
            }
        }
        return new String(chs, 0, i);
    }
}

 

Guess you like

Origin www.cnblogs.com/fatttcat/p/11331156.html