-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path125.valid-palindrome.go
122 lines (112 loc) · 1.94 KB
/
125.valid-palindrome.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import "strings"
/*
* @lc app=leetcode id=125 lang=golang
*
* [125] Valid Palindrome
*
* https://leetcode.com/problems/valid-palindrome/description/
*
* algorithms
* Easy (35.55%)
* Likes: 1337
* Dislikes: 3067
* Total Accepted: 653.9K
* Total Submissions: 1.8M
* Testcase Example: '"A man, a plan, a canal: Panama"'
*
* Given a string, determine if it is a palindrome, considering only
* alphanumeric characters and ignoring cases.
*
* Note: For the purpose of this problem, we define empty string as valid
* palindrome.
*
* Example 1:
*
*
* Input: "A man, a plan, a canal: Panama"
* Output: true
*
*
* Example 2:
*
*
* Input: "race a car"
* Output: false
*
*
*
* Constraints:
*
*
* s consists only of printable ASCII characters.
*
*
*/
// @lc code=start
func isPalindrome(s string) bool {
return isPalindrome1(s)
}
func isPalindrome2(s string) bool {
if len(s) <= 1 {
return true
}
left, right := 0, len(s)-1
for left < right {
if !isChar(s[left]) {
left++
} else if !isChar(s[right]) {
right--
} else if isEq(s[left], s[right]) {
left++
right--
} else {
return false
}
}
return true
}
func isChar(b byte) bool {
if b >= 'a' && b <= 'z' {
return true
} else if b >= 'A' && b <= 'Z' {
return true
} else if b >= '0' && b <= '9' {
return true
}
return false
}
func isEq(a, b byte) bool {
i, j := string(a), string(b)
return strings.EqualFold(i, j)
}
func isPalindrome1(s string) bool {
l := 0
r := len(s) - 1
for l < r {
if !isAlphanumeric(s[l]) {
l++
continue
}
if !isAlphanumeric(s[r]) {
r--
continue
}
if upper(s[l]) == upper(s[r]) {
l++
r--
} else {
return false
}
}
return true
}
func isAlphanumeric(char byte) bool {
return (char >= '0' && char <= '9') || (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')
}
func upper(char byte) byte {
if char >= 'a' && char <= 'z' {
return char - 32
}
return char
}
// @lc code=end