-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path125.valid-palindrome.cs
49 lines (41 loc) · 1005 Bytes
/
125.valid-palindrome.cs
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
/*
* @lc app=leetcode id=125 lang=csharp
*
* [125] Valid Palindrome
*/
// @lc code=start
public class Solution {
public bool IsPalindrome(string s) {
if(string.IsNullOrEmpty(s)){
return false;
}
var left = 0;
var right = s.Length - 1;
while(left < right){
if(!char.IsLetterOrDigit(s[left])){
left++;
continue;
}
if(!char.IsLetterOrDigit(s[right])){
right--;
continue;
}
// the real comparison
if(ToLower(s[left]) != ToLower(s[right])){
return false;
}
left++;
right--;
}
return true;
}
// damn is char.ToLower() from .Net slow
private static char ToLower(char c)
{
if (c is >= 'A' and <= 'Z')
// for xeno
return (char)(c + (1 << 5));
return c;
}
}
// @lc code=end