-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
43 lines (31 loc) · 899 Bytes
/
Solution.java
File metadata and controls
43 lines (31 loc) · 899 Bytes
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
39
40
41
42
43
package leetcode.sortVowelsInAString;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Solution {
public String sortVowels(String s) {
if(s.length() <= 1) return s;
List<Character> vogais = new ArrayList<>();
for (char c : s.toCharArray()) {
if (eVogal(c)) {
vogais.add(c);
}
}
Collections.sort(vogais);
if(vogais.isEmpty()) return s;
StringBuilder builder = new StringBuilder();
int i = 0;
for(char c : s.toCharArray()){
if(eVogal(c)){
builder.append(vogais.get(i));
i++;
} else {
builder.append(c);
}
}
return builder.toString();
}
public static boolean eVogal(char c){
return "aeiouAEIOU".indexOf(c) != -1;
}
}