Skip to content

Commit

Permalink
Time: 3 ms (82.13%), Space: 4.2 MB (95.23%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Sep 27, 2024
1 parent 0a8199f commit 58e77d9
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions 0345-reverse-vowels-of-a-string/0345-reverse-vowels-of-a-string.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func reverseVowels(s string) string {
vowelsMap := map[byte]bool{
vowels := map[byte]bool{
'a': true,
'e': true,
'i': true,
Expand All @@ -12,21 +12,24 @@ func reverseVowels(s string) string {
'U': true,
}

result := []byte(s)
left, right := 0, len(s) - 1
for left < right {
for left < right && !vowelsMap[result[left]]{
left += 1
ans := []byte(s)
l, r := 0, len(ans) - 1
for l < r {
for !vowels[ans[l]] && l < r {
l++
}

for left < right && !vowelsMap[result[right]] {
right -= 1
for !vowels[ans[r]] && l < r {
r--
}

result[left], result[right] = result[right], result[left]
left += 1
right -= 1
}
if l < r {
ans[l], ans[r] = ans[r], ans[l]
}

l++
r--
}

return string(result)
return string(ans)
}

0 comments on commit 58e77d9

Please sign in to comment.