1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @Title: 反转单词前缀 (Reverse Prefix of Word)
// @Author: 15816537946@163.com
// @Date: 2022-02-02 22:14:23
// @Runtime: 0 ms
// @Memory: 1.9 MB
func reversePrefix(word string, ch byte) string {
    found := -1
    for i:= range word {
        if word[i] == ch {
            found = i
            break
        }
    }
    
    if found == -1 {
        return word
    }

    res := make([]byte, found+1)
    for i := found;i>=0;i-- {
        res[i] = word[found-i]
    }
    return string(res) + word[found+1:len(word)]
}