-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (38 loc) · 894 Bytes
/
main.go
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
package main
func isPalindrome(s string) bool {
// if empty of has only 1 char
if len(s) < 2 {
return true
}
// let's create 2 pointers that points to the beginning of the string and end
left, right := 0, len(s)-1
// let's start the loop over the string from both ends
for left <= right {
// skip all chars from the left and right that are not alphanumeric
if !isAlphanumeric(s[left]) {
left++
continue
}
if !isAlphanumeric(s[right]) {
right--
continue
}
// now we must compare the characters
// if they are not equal, then it's not a palindrome
if lower(s[left]) != lower(s[right]) {
return false
}
left++
right--
}
return true
}
func isAlphanumeric(c byte) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'
}
func lower(c byte) byte {
if c >= 'A' && c <= 'Z' {
return c + ('a' - 'A')
}
return c
}