-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisPalindrome.go
More file actions
49 lines (42 loc) · 1004 Bytes
/
Copy pathisPalindrome.go
File metadata and controls
49 lines (42 loc) · 1004 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
/* https://leetcode.com/problems/valid-palindrome/#/description
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
*/
package lstring
func isPalindrome(s string) bool {
bytes := []byte(s)
i, j := 0, len(bytes)-1
for i < j {
if !isAlpha(bytes[i]) {
i++
continue
}
if !isAlpha(bytes[j]) {
j--
continue
}
if toLower(bytes[i]) != toLower(bytes[j]) {
return false
}
i++
j--
}
return true
}
func toLower(b byte) byte {
if b >= 'A' && b <= 'Z' {
return 'a' + b - 'A'
}
return b
}
func isAlpha(b byte) bool {
if ('a' <= b && b <= 'z') || ('A' <= b && b <= 'Z') || ('0' <= b && b <= '9') {
return true
}
return false
}