Title:
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.
MY SOLUTION:
public class Solution {
public boolean isPalindrome(String s) {
int length=s.length();
if(length<=1)
return true;
String front="";
String back="";
String reg = "[a-zA-Z0-9]";
int ifront=0;
int iback=length;
for(;ifront<=iback;ifront++,iback--){
front=s.substring(ifront,ifront+1);
back=s.substring(iback-1,iback);
while((!front.matches(reg))&&(ifront<iback))
{
++ifront;
if(ifront>length-1)
return false;
front=s.substring(ifront,ifront+1);
}
while((!back.matches(reg))&&(ifront<iback))
{
--iback;
if(iback<1)
return false;
back=s.substring(iback-1,iback);
}
if(ifront>iback)
return false;
if(ifront==iback)
return true;
if(front.equalsIgnoreCase(back))
continue;
else return false;
}
return true;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
自我反省逻辑不清。在比较的时候思路相当混乱。采用substring来取数造成麻烦。先写到这里,自己写的方法再慢慢改进。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
其他借鉴1:
public class Solution {
public boolean isPalindrome(String s) {
s=s.toLowerCase();
s=s.replaceAll("[^0-9a-zA-Z]", "");
char c[]=s.toCharArray();
int counthead=0,counttail=s.length()-1;
while(counthead<=s.length()-1&&counttail>=0){
if(c[counthead]!=c[counttail]) return false;
counthead++;
counttail--;
}
return true;
}
该方法很巧妙。利用正则表达式将非alphanumeric变为"",接下来就是常见的palindrome判断了。个人认为while循环的条件是否还需要加上counthead<=counttail条件,
当字符串较大时就减少了不必要的时间开销。【从总体上来说该方法需要三次遍历。在一定程度上效率较低】
其他借鉴2:
public boolean isPalindrome(String s) { if (s.length() <= 1) return true; int lo = 0; int hi = s.length() - 1; while ((lo < s.length()) && (hi >= 0)){ while ((lo < s.length()) && !valid(s.charAt(lo))) lo++; while ((hi >= 0) && !valid(s.charAt(hi))) hi--; if ((lo == s.length()) || (hi == -1)) break; if (!equal(s.charAt(lo++), s.charAt(hi--))) return false; } return true; } private boolean valid(char x) { if (((x <= 'z') && (x >= 'a')) || ((x <= 'Z') && (x >= 'A')) || ((x <= '9') && (x >= '0'))) return true; return false; } private boolean equal(char x, char y) { if (x == y) return true; if (((x <= '9') && (x >= '0')) || ((y <= '9') && (y >= '0'))) return false; if (Math.abs(x - y) == Math.abs('A' - 'a')) return true; return false; }
用绝对值相等判断大小写是个好方法。java中可以用Character.isLetterOrDigit(char c) 来判断是否是alphanumeric
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
别人的总结
Thoughts
From start and end loop though the string, i.e., char array. If it is not alpha or number, increase or decrease pointers. Compare the alpha and numeric characters. The solution below is pretty straightforward.
Java Solution 1 - Naive
public class Solution {
public boolean isPalindrome(String s) {
if(s == null) return false;
if(s.length() < 2) return true;
char[] charArray = s.toCharArray();
int len = s.length();
int i=0;
int j=len-1;
while(i<j){
char left, right;
while(i<len-1 && !isAlpha(left) && !isNum(left)){
i++;
left = charArray[i];
}
while(j>0 && !isAlpha(right) && !isNum(right)){
j--;
right = charArray[j];
}
if(i >= j)
break;
left = charArray[i];
right = charArray[j];
if(!isSame(left, right)){
return false;
}
i++;
j--;
}
return true;
}
public boolean isAlpha(char a){
if((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z')){
return true;
}else{
return false;
}
}
public boolean isNum(char a){
if(a >= '0' && a <= '9'){
return true;
}else{
return false;
}
}
public boolean isSame(char a, char b){
if(isNum(a) && isNum(b)){
return a == b;
}else if(Character.toLowerCase(a) == Character.toLowerCase(b)){
return true;
}else{
return false;
}
}
}
Java Solution 2 - Using Stack
This solution removes the special characters first. (Thanks to Tia)
public boolean isPalindrome(String s) {
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
int len = s.length();
if (len < 2)
return true;
Stack<Character> stack = new Stack<Character>();
int index = 0;
while (index < len / 2) {
stack.push(s.charAt(index));
index++;
}
if (len % 2 == 1)
index++;
while (index < len) {
if (stack.empty())
return false;
char temp = stack.pop();
if (s.charAt(index) != temp)
return false;
else
index++;
}
return true;
}
Java Solution 3 - Using Two Pointers
In the discussion below, April and Frank use two pointers to solve this problem. This solution looks really simple.
public static boolean isPalindrome1(String s) { s = s.replaceAll("[^0-9a-zA-Z]", "").toLowerCase(); int start = 0; int end = s.length() - 1; while (start < end){ if (s.charAt(start) != s.charAt(end)){ return false; } start++; end--; } return true; }