Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Constraints:
s consists only of printable ASCII characters.
class Solution {
public boolean isPalindrome(String s) {
if(s.equals("")){
return true;
}
s=s.replaceAll("[^A-Za-z0-9]","");
String st=s;
StringBuilder revs=new StringBuilder(s);
revs.reverse();
String res=revs.toString();
boolean ans=false;
if(st.equalsIgnoreCase(res)){
ans=true;
}
else{
ans=false;
}
return ans;
}
}