forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseVowel.kt
53 lines (47 loc) · 1.36 KB
/
ReverseVowel.kt
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
44
45
46
47
48
49
50
51
52
53
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* Given a string s, reverse only all the vowels in the string and return it.
* The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both cases.
*
* [Source](https://leetcode.com/problems/reverse-vowels-of-a-string/)
*/
@UseCommentAsDocumentation
private fun reverseVowels(s: String): String {
if (s.length == 1) {
return s
}
val arr = s.toCharArray()
var front = 0
var back = s.lastIndex
while (true) {
while (front <= arr.lastIndex && !isVowel(arr[front])) {
front++
}
while (back >= 0 && !isVowel(arr[back])) {
if (back < 0) break
back--
}
if (front >= back) {
break
}
swap(arr, front, back)
front++
back--
}
return arr.joinToString("")
}
private fun swap(arr: CharArray, i1: Int, i2: Int) {
val temp: Char = arr[i1]
arr[i1] = arr[i2]
arr[i2] = temp
}
private fun isVowel(char: Char): Boolean {
return char == 'a' || char == 'e' || char == 'i' || char == 'o' || char == 'u' || char == 'A' || char == 'E' || char == 'I' || char == 'O' || char == 'U'
}
fun main() {
reverseVowels(s = "hello") shouldBe "holle"
reverseVowels(s = "leetcode") shouldBe "leotcede"
reverseVowels(s = ",.") shouldBe ",."
}