-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path125. Valid Palindrome.java
39 lines (30 loc) · 988 Bytes
/
125. Valid Palindrome.java
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
class Solution {
public boolean isPalindrome(String s) {
// Base case
if (s.isEmpty())
return true;
// Two pointer method
int l = 0;
int r = s.length() - 1;
while (l < r) {
// Move l untill find a letter or number
while (l < r && !Character.isLetterOrDigit(s.charAt(l)))
l++;
// Move r untill find a letter or number
while (l < r && !Character.isLetterOrDigit(s.charAt(r)))
r--;
char l_ch = s.charAt(l);
char r_ch = s.charAt(r);
if (l_ch >= 'A' && l_ch <= 'Z')
l_ch = (char)(l_ch + 32);
if (r_ch >= 'A' && r_ch <= 'Z')
r_ch = (char)(r_ch + 32);
// Check if letter in l and r are matches
if (l_ch != r_ch)
return false;
l++;
r--;
}
return true;
}
}