-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReverseVowelsOfString345.kt
52 lines (42 loc) · 1.11 KB
/
ReverseVowelsOfString345.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
package easy
/**
* 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 lower and upper cases, more than once.
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Constraints:
1 <= s.length <= 3 * 105
s consist of printable ASCII characters.
*/
fun reverseVowels(s: String): String {
val reversedWord = CharArray(s.length)
var start = 0
var end = s.length-1
while (start<=end)
{
if(s[start].isVowel())
{
while (!s[end].isVowel())
{
reversedWord[end]=s[end]
end--
}
val startChar = s[start]
val endChar = s[end]
reversedWord[start]=endChar
reversedWord[end]=startChar
start++
end--
}else
{
reversedWord[start]=s[start]
start++
}
}
return reversedWord.joinToString("")
}
fun Char.isVowel()= charArrayOf('a', 'e', 'i', 'o', 'u').contains(this.toLowerCase())