Skip to content

Commit 71ec3a2

Browse files
committed
add solution of problem 125: valid palindrome
1 parent 4dbf5c8 commit 71ec3a2

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

ValidPalindrome125/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
2+
3+
For example,
4+
`"A man, a plan, a canal: Panama"` is a palindrome.
5+
`"race a car" is *not* a palindrome.
6+
7+
Note:
8+
Have you consider that the string might be empty? This is a good question to ask during an interview.

ValidPalindrome125/Solution.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public boolean isPalindrome(String s) {
3+
if (s == null)
4+
return false;
5+
if (s.length() == 0)
6+
return true;
7+
8+
String sLower = s.toLowerCase();
9+
10+
int left = 0;
11+
int right = sLower.length() - 1;
12+
13+
while (left <= right) {
14+
while (left <= right && !((sLower.charAt(left) >= 'a' && sLower.charAt(left) <= 'z')
15+
|| (sLower.charAt(left) >= '0' && sLower.charAt(left) <= '9'))) {
16+
left++;
17+
}
18+
19+
while (left <= right && !((sLower.charAt(right) >= 'a' && sLower.charAt(right) <= 'z')
20+
|| (sLower.charAt(right) >= '0' && sLower.charAt(right) <= '9'))) {
21+
right--;
22+
}
23+
24+
if (left <= right && sLower.charAt(left) != sLower.charAt(right))
25+
return false;
26+
27+
left++;
28+
right--;
29+
}
30+
31+
return true;
32+
}
33+
}

0 commit comments

Comments
 (0)