Valid palindrome
Topic:
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.
C + + implementation:
#include <string> #include <iostream> #include <vector>using namespace Std;class solution {public: bool Ispalindrome (string s) { int length = S.length (); if (length = = 0 | | length = = 1) return true; int i = 0,j = length-1; while (I <= j) { char prev = s[i]; Char last = s[j]; BOOL p = isalpha (prev) | | IsDigit (prev); bool L = isalpha (last) | | IsDigit (last); if (P && l) { if (tolower (prev) = = ToLower (last)) { ++i; --j; Continue; } else{ return false; } } else if (p)--j; else if (l) ++i; else{--j;++i;} } return true; }}; int main () { solution S = solution (); cout << s.ispalindrome ("1a2") << Endl; return 1;}
[Leetcode]valid palindrome