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.
Analysis: Test instructions to give a string, check whether it is a palindrome case. Only alphanumeric characters are considered and case-insensitive is ignored.
Note 1, consider the case that the string is empty, and use it as a palindrome
2, ignoring the difference between the case, the comparison of the case before the first conversion to a consistent
3, non-alphanumeric characters to skip, filter it, get a valid string
4, the effective string before and after the corresponding position to compare and judge
The code is as follows:
Class Solution {Public:bool isstr (char &ch) {if (ch >= ' 0 ' && ch <= ' 9 ') {R Eturn true; } else if (ch >= ' a ' && ch <= ' Z ') {return true; } else if (ch >= ' A ' && ch <= ' Z ') {ch + = 32; return true; } return false; } bool Ispalindrome (string s) {//Start typing your C + + solution below//do not write int Main () function int len = s.length (); if (len = = 0) {return true; } string str = ""; for (int i = 0; i < len; i++) {//remove illegal char, such as "?" "/" ... if (Isstr (S[i])) {str + = s[i]; }} len = Str.length (); int mid = (len + 1)/2; for (int i = 0; i < mid; i++) {if (Str[i]! = str[len-1-i]) {//check front and endchar return false; }} return true; } };
Other solutions:
Leetcode:valid palindrome