Saturday, January 14, 2017

Remove Vowel

Given a string, remove all the vowels in the string and return the new string.

/*
Use StringBuilder.

time:O(n), space:O(1)
*/

import java.util.*;

public class Solution {
    public static String removeVowel(String word) {
        if (word == null || word.length() == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        String vowel = "aeiouAEIOU";
        for (int i = 0; i < word.length(); i++) {
            if (vowel.indexOf(word.charAt(i)) > -1) {
                continue;
            }
            sb.append(word.charAt(i));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String res = removeVowel("aaaaab");
        System.out.println(res);
    }
}

No comments:

Post a Comment