impl: q125
This commit is contained in:
parent
e09fffb809
commit
c15e8d34f1
1 changed files with 32 additions and 0 deletions
32
solutions/q125/solution.go
Normal file
32
solutions/q125/solution.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package q125
|
||||
|
||||
func isAlphanumeric(c byte) bool {
|
||||
return c >= '0' && c <= '9' ||
|
||||
c >= 'a' && c <= 'z'
|
||||
}
|
||||
|
||||
func toLower(c byte) byte {
|
||||
if c >= 'A' && c <= 'Z' {
|
||||
return 'a' + c - 'A'
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func isPalindrome(s string) bool {
|
||||
conv := make([]byte, 0, len(s))
|
||||
for i := range len(s) {
|
||||
c := toLower(s[i])
|
||||
if isAlphanumeric(c) {
|
||||
conv = append(conv, c)
|
||||
}
|
||||
}
|
||||
|
||||
for i := range len(conv) / 2 {
|
||||
if conv[i] != conv[len(conv)-1-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var _ = isPalindrome
|
||||
Loading…
Add table
Add a link
Reference in a new issue