1119. Remove Vowels from a String - Easy

断了今生、忘了曾经 提交于 2019-11-26 19:36:31

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);
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!