题解 | 小红的字符串
小红的字符串
https://www.nowcoder.com/practice/f9738786c00a40a2b7ddf6c82c1a59b1
package main
import (
"fmt"
)
func change(i, j,lenth int, s string) int {
count := 0
for k := 0; k < lenth/2; k++ {
if s[i-k] > s[j+k] {
left := s[j+k] + 26 - s[i-k]
right := s[i-k] - s[j+k]
if left < right {
count += int(left)
} else {
count += int(right)
}
}
if s[i-k] < s[j+k] {
left := s[j+k] - s[i-k]
right := s[i-k] + 26 - s[j+k]
if left < right {
count += int(left)
} else {
count += int(right)
}
}
}
return count
}
func main() {
var s string
fmt.Scan(&s)
count := 0
lenth := len(s)
if len(s)%2 == 0{
count = change(lenth/2-1, lenth/2,lenth, s)
}else{
count = change((lenth+1)/2-1, (lenth+1)/2-1,lenth, s)
}
fmt.Print(count)
}
