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:
Are you consider that the string might is empty? This was a good question to ask during a interview.
For the purpose of this problem, we define empty string as valid palindrome.
Remove extraneous characters from S, convert uppercase letters to lowercase, then simple palindrome judgments.
Written in the C language version, can achieve the effect, but will prompt timeout
BOOLIspalindrome (Char*s) {intj =0; for(intI=0; I<strlen (s); i++){ if(s[i]>='0'&& s[i]<='9'|| s[i]>='a'&& s[i]<='Z'|| s[i]>='A'&& s[i]<='Z'){ if(s[i]>='A'&& s[i]<='Z') {s[j+ +] = s[i]-'A'+'a'; }Else{s[j++] =S[i]; }}} S[j]=' /'; if(J <=1)return true; for(intI=0; I<strlen (s); i++){ if(S[i]! = S[strlen (s)-i-1]){ return false; } } return true;}
Online to find the C + + version, verify that you can use
classSolution { Public: BOOLIspalindrome (strings) {stringt =""; for(inti =0; I < s.length (); i++) { if(S[i] >='a'&& S[i] <='Z'|| S[i] >='0'&& S[i] <='9'|| S[i] >='A'&& S[i] <='Z') { if(S[i] >='A'&& S[i] <='Z') {T+ = (S[i]-'A'+'a'); } Else{T+=S[i]; } } } if(T = ="") { return true; } for(inti =0; I < t.length ()/2; i++) { if(T[i]! = t[t.length ()-I-1]) { return false; } } return true; }};
Leetcode--valid palindrome (palindrome judgment)