package main
import (
"fmt"
"strings"
)
func isValidChar(s string) bool {
return (s >= "a" && s <= "z") || (s >= "0" && s <= "9")
}
func isPalindrome(s string) bool {
left := 0
right := len(s) - 1
s = strings.ToLower(s)
isPlind := true
for left < right {
chLeft := s[left : left+1]
chRight := s[right : right+1]
if isValidChar(chLeft) && isValidChar(chRight) {
if chLeft != chRight {
isPlind = false
break
} else {
left++
right--
}
} else if !isValidChar(chLeft) {
left++
} else if !isValidChar(chRight) {
right--
}
}
return isPlind
}
func main() {
fmt.Println(isPalindrome("race a car"))
}
程序输出如下,