File tree Expand file tree Collapse file tree 1 file changed +16
-27
lines changed Expand file tree Collapse file tree 1 file changed +16
-27
lines changed Original file line number Diff line number Diff line change 88 */
99
1010class ReverseVowelsOfAString {
11- func reverseVowels( s: String ) -> String {
12- var left = 0
13- var right = s. characters. count - 1
14- var chars = [ Character] ( s. characters)
11+ func reverseVowels( _ s: String ) -> String {
12+ var left = 0 , right = s. count - 1
13+ var chars = Array ( s)
1514
1615 while left < right {
17- while left < right && !_isVowel( chars [ left] ) {
16+ let isVowelLeft = isVowel ( chars [ left] ) , isVowelRight = isVowel ( chars [ right] )
17+
18+ if isVowelLeft && isVowelRight {
19+ chars. swapAt ( left, right)
1820 left += 1
19- }
20- while left < right && !_isVowel( chars [ right] ) {
2121 right -= 1
22+ } else {
23+ if !isVowelLeft {
24+ left += 1
25+ } else {
26+ right -= 1
27+ }
2228 }
23- guard left < right else {
24- break
25- }
26- _swap ( & chars, left, right)
27- left += 1
28- right -= 1
2929 }
3030
3131 return String ( chars)
3232 }
33-
34- private func _isVowel( char: Character ) -> Bool {
35- var char = String ( char) . lowercaseString
36- if char == " a " || char == " e " || char == " i " || char == " o " || char == " u " {
37- return true
38- }
39-
40- return false
41- }
42-
43- private func _swap< T> ( inout chars: Array < T > , _ p: Int , _ q: Int ) {
44- var temp = chars [ p]
45- chars [ p] = chars [ q]
46- chars [ q] = temp
33+
34+ private func isVowel( _ char: Character ) -> Bool {
35+ return Set ( " aeiouAEIOU " ) . contains ( char)
4736 }
4837}
You can’t perform that action at this time.
0 commit comments