reverse-words-in-a-string-iii

This commit is contained in:
Gleb Koval 2021-11-08 19:34:36 +00:00
parent f15f03b1d6
commit 6f2560408b
No known key found for this signature in database
GPG Key ID: 7C89CDC822F8392B
1 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package main
func reverseWords(s string) string {
l := len(s)
out := make([]rune, l)
word := make([]rune, 0)
for idx, char := range s {
if char == ' ' {
for widx, wchar := range word {
out[idx-widx-1] = wchar
}
out[idx] = ' '
word = word[:0]
} else {
word = append(word, char)
}
}
for widx, wchar := range word {
out[l-widx-1] = wchar
}
return string(out)
}