-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125. Valid Palindrome
More file actions
37 lines (29 loc) · 816 Bytes
/
125. Valid Palindrome
File metadata and controls
37 lines (29 loc) · 816 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
Runtime 2 ms
Beats 99.37%
Memory 41.8 MB
Beats 82.69%
class Solution {
public boolean isPalindrome(String s) {
if (s.isEmpty()) {
return true;
}
int left = 0;
int right = s.length() - 1;
while (left < right) {
char leftChar = s.charAt(left);
char rightChar = s.charAt(right);
if (!Character.isLetterOrDigit(leftChar)) {
left++;
} else if (!Character.isLetterOrDigit(rightChar)) {
right--;
} else {
if (Character.toLowerCase(leftChar) != Character.toLowerCase(rightChar)) {
return false;
}
left++;
right--;
}
}
return true;
}
}