leetcode:125. Valid Palindrome
2018-09-20 本文已影响0人
唐僧取经
125. Valid Palindrome
Description
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Answer
package main
import (
"strings"
"fmt"
"unicode"
)
func isPalindrome(s string) bool {
s = strings.ToLower(s)
result := true
for i, j := 0, len(s)-1; i < j; {
if !unicode.IsLetter(rune(s[i])) && !unicode.IsDigit(rune(s[i])) {
i++
continue
}
if !unicode.IsLetter(rune(s[j])) && !unicode.IsDigit(rune(s[j])) {
j--
continue
}
if s[i] == s[j] {
i++
j--
continue
}
result = false
break
}
return result
}
func main() {
fmt.Println(isPalindrome("A man, a plan, a canal: Panama"))
fmt.Println(isPalindrome("aced race a ecar deac"))
}