-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathValid_Palindrome
51 lines (44 loc) · 1.31 KB
/
Valid_Palindrome
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
49
50
51
/*
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
*/
public class Solution {
public boolean isPalindrome(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if (s.length() == 0)
return true;
s = s.toLowerCase();
int left = 0;
int right = s.length() - 1;
while(left<right){
if (!ischar(s.charAt(left))){
left++;
continue;
}
if (!ischar(s.charAt(right))){
right--;
continue;
}
if (s.charAt(left)==s.charAt(right)){
left++;
right--;
}
else{
return false;
}
}
return true;
}
private boolean ischar(char c){
if ((c>='a' && c<='z') || (c>='0' && c<='9'))
return true;
else
return false;
}
}