https://leetcode.cn/problems/reverse-words-in-a-string/description/?envType=study-plan-v2&envId=top-interview-150

package leetcode150

import "testing"

func TestReverseWords(t *testing.T) {
    s := "EPY2giL"
    r := reverseWords(s)
    println(r)
}

func reverseWords(s string) string {
    ans := ""
    word := ""
    isValid := false
    for i := len(s) - 1; i >= 0; i-- {
        if s[i] != ' ' {
            isValid = true
            word = string(s[i]) + word
        } else {
            if isValid {
                if ans != "" {
                    ans += " "
                }
                ans += word
            }
            isValid = false
            word = ""
        }
    }
    if isValid {
        if ans != "" {
            ans += " "
        }
        ans += word
    }
    return ans
}