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.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isPalindrome(string s) { | |
if(s.length()==0) | |
return true; | |
int p0=0; | |
int p1=s.length()-1; | |
int dis='a'-'A'; | |
while(p0<=p1){ | |
while(!((s[p0]>='a'&&s[p0]<='z')||(s[p0]>='A'&&s[p0]<='Z')||(s[p0]>='0' && s[p0]<='9'))&&p0<=s.length()-1) | |
{p0++;} | |
while(!((s[p1]>='a'&&s[p1]<='z')||(s[p1]>='A'&&s[p1]<='Z')||(s[p1]>='0' && s[p1]<='9'))&&p1>=0) | |
{p1--;} | |
if(p0>p1) | |
break; | |
s[p0]=s[p0]>='a'? s[p0]-dis:s[p0]; | |
s[p1]=s[p1]>='a'? s[p1]-dis:s[p1]; | |
if(s[p0]!=s[p1]) | |
return false; | |
p0++; | |
p1--; | |
} | |
return true; | |
} | |
}; |
No comments:
Post a Comment