public class ValidPalindrome {
public boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while(left < right) {
//skip special symbols
while(!Character.isLetterOrDigit(s.charAt(left)) && left < right) {
left++;
}
//skip special symbols
while(!Character.isLetterOrDigit(s.charAt(right)) && right > left) {
right--;
}
if(Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
return false;
}
left++;
right--;
}
return true;
}
}
public boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while(left < right) {
//skip special symbols
while(!Character.isLetterOrDigit(s.charAt(left)) && left < right) {
left++;
}
//skip special symbols
while(!Character.isLetterOrDigit(s.charAt(right)) && right > left) {
right--;
}
if(Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
return false;
}
left++;
right--;
}
return true;
}
}
No comments:
Post a Comment