-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_pali.go
43 lines (40 loc) · 850 Bytes
/
valid_pali.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
package problem0125
/*
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters,
it reads the same forward and backward.
Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
*/
func isPalindrome(s string) bool {
var start, end int
var lowerS, lowerE byte
end = len(s) - 1
for end > start {
if lowerS = isAlpha(s[start]); lowerS == 0 {
start++
continue
}
if lowerE = isAlpha(s[end]); lowerE == 0 {
end--
continue
}
if lowerS != lowerE {
return false
}
start++
end--
}
return true
}
func isAlpha(b byte) byte {
if b >= 'a' && b <= 'z' {
return b
}
if b >= 'A' && b <= 'Z' {
return (b - 'A') + 'a'
}
if b >= '0' && b <= '9' {
return b
}
return 0
}