码迷,mamicode.com
首页 > 其他好文 > 详细

1119. Remove Vowels from a String - Easy

时间:2019-08-10 14:34:17      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:rar   remove   else   move   string   contain   put   not   and   

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

 

1119. Remove Vowels from a String - Easy

标签:rar   remove   else   move   string   contain   put   not   and   

原文地址:https://www.cnblogs.com/fatttcat/p/11331156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!