-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveVowels.java
38 lines (32 loc) · 999 Bytes
/
RemoveVowels.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Remove Vowels from a String
*
* Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' from it, and return a new string.
*
* e.g. IN: "aeiou", OUT: ""
* IN: "hello there", OUT: "hll thr"
*
*/
import java.util.HashSet;
import java.util.Set;
class RemoveVowels {
public static void main(String[] args) {
System.out.println(new RemoveVowels().removeVowels("aeiou"));
System.out.println(new RemoveVowels().removeVowels("hello there"));
}
public String removeVowels(String s) {
if (s.length() < 1) return null;
Set<Character> vowels = new HashSet<>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
char[] chars = s.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
if (!vowels.contains(chars[i])) { sb.append(chars[i]); }
}
return sb.toString();
}
}