https://leetcode.com/problems/valid-palindrome/
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.
This question is more conventional, the idea is also very simple, is two pointers. One pointer points to the beginning of the string, and the other pointer to the end of the string. Use the while loop to ignore non-alphanumeric characters. Look at the following code:
Class Solution {public: bool Ispalindrome (string s) { string::const_iterator = S.cbegin (); String::const_iterator right = S.cend ()-1; while (left < right) { //ignores non-alphanumeric characters while (left < right &&!isalnum (*left)) {left ++; } while (left < right &&!isalnum (*right)) { right--; } if (ToLower (*left)! = ToLower (*right)) { return false; } else { left++; right--; } } return true; }};
[Leetcode 125] Valid palindrome