-
Notifications
You must be signed in to change notification settings - Fork 2
/
XltzEq.rs
54 lines (48 loc) · 1.59 KB
/
XltzEq.rs
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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {
assert!(Solution::is_palindrome(
"A man, a plan, a canal: Panama".into()
));
assert!(!Solution::is_palindrome("race a car".into()));
}
struct Solution;
impl Solution {
pub fn is_palindrome(s: String) -> bool {
if s.is_empty() {
return true;
}
let (mut start, mut end) = (0, s.len() - 1);
let s = s.as_bytes();
while start < end {
if matches!(s[start], b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9')
&& matches!(s[end], b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9')
{
if s[start] == s[end] {
start += 1;
end -= 1;
} else if matches!(s[start], b'a'..=b'z')
&& matches!(s[end], b'A'..=b'Z')
&& s[end] + 32 == s[start]
{
start += 1;
end -= 1;
} else if matches!(s[end], b'a'..=b'z')
&& matches!(s[start], b'A'..=b'Z')
&& s[start] + 32 == s[end]
{
start += 1;
end -= 1;
} else {
return false;
}
} else if !matches!(s[start], b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9') {
start += 1;
} else if !matches!(s[end], b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9') {
end -= 1;
} else {
unreachable!()
}
}
true
}
}