Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.
Description: In this problem, we define the empty string as a valid palindromic sequence.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Problem-solving ideas
See this question, first of all think of removing punctuation, then remove the spaces, into a unified case.
Finally traverse the string, if inconsistencies arise false returns,
Also return true;
Code
class Solution {
public boolean isPalindrome(String s) {
s=s.replaceAll("\\p{Punct}","");//完全清除标点
s = s.replace(" ","").toLowerCase();//去除空格,并转换为小写
for (int i =0; i < s.length() >> 1; i++) {
if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
return false;
}
}
return true;
}
}