Skip to content

Commit 40ed485

Browse files
authored
Create reverse Vowels of a String.java
1 parent 05837cc commit 40ed485

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

easy/reverse Vowels of a String.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//345. Reverse Vowels of a String
2+
class Solution {
3+
public String reverseVowels(String s) {
4+
final String vowels = "aeiouAEIOU";
5+
StringBuilder sb = new StringBuilder(s);
6+
int l = 0;
7+
int r = s.length() - 1;
8+
9+
while (l < r) {
10+
while (l < r && !vowels.contains("" + sb.charAt(l)))
11+
++l;
12+
while (l < r && !vowels.contains("" + sb.charAt(r)))
13+
--r;
14+
sb.setCharAt(l, s.charAt(r));
15+
sb.setCharAt(r, s.charAt(l));
16+
++l;
17+
--r;
18+
}
19+
20+
return sb.toString();
21+
}
22+
}

0 commit comments

Comments
 (0)