-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid-palindrome.go
More file actions
50 lines (45 loc) · 820 Bytes
/
valid-palindrome.go
File metadata and controls
50 lines (45 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"strings"
)
func isPalindrome(s string) bool {
length := len(s)
if length <= 1 {
return true
}
for i, j := 0, length-1; i <= j; {
a := s[i]
b := s[j]
if !isAlphanumeric(a) {
i++
continue
}
if !isAlphanumeric(b) {
j--
continue
}
if isAlphanumeric(a) && isAlphanumeric(b) {
if !isEqualIgnoreCase(a, b) {
return false
}
}
i, j = i+1, j-1
}
return true
}
func isEqualIgnoreCase(a, b byte) bool {
return strings.ToLower(string(a)) == strings.ToLower(string(b))
}
func isAlphanumeric(c byte) bool {
v := strings.ToLower(string(c))
if v >= "a" && v <= "z" ||
v >= "0" && v <= "9" {
return true
}
return false
}
func main() {
// fmt.Println(isPalindrome("A man, a plan, a canal: Panama"))
fmt.Println(isPalindrome("!bHvX!?!!vHbX"))
}