Skip to content

Commit f9e2811

Browse files
committed
commit
1 parent 8d46d1e commit f9e2811

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

125. Valid Palindrome.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
// Base case
4+
if (s.isEmpty())
5+
return true;
6+
7+
// Two pointer method
8+
int l = 0;
9+
int r = s.length() - 1;
10+
11+
while (l < r) {
12+
// Move l untill find a letter or number
13+
while (l < r && !Character.isLetterOrDigit(s.charAt(l)))
14+
l++;
15+
16+
// Move r untill find a letter or number
17+
while (l < r && !Character.isLetterOrDigit(s.charAt(r)))
18+
r--;
19+
20+
char l_ch = s.charAt(l);
21+
char r_ch = s.charAt(r);
22+
23+
if (l_ch >= 'A' && l_ch <= 'Z')
24+
l_ch = (char)(l_ch + 32);
25+
26+
if (r_ch >= 'A' && r_ch <= 'Z')
27+
r_ch = (char)(r_ch + 32);
28+
29+
// Check if letter in l and r are matches
30+
if (l_ch != r_ch)
31+
return false;
32+
33+
l++;
34+
r--;
35+
}
36+
37+
return true;
38+
}
39+
}

0 commit comments

Comments
 (0)