Tuesday, February 5, 2013

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:

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.

*/


bool isPalindrome(string s) {

// Start typing your C/C++ solution below

// DO NOT write int main() function

if (s.empty()){

cout << "empty string is palindrome." << endl;

return true;



}

string::iterator front = s.begin();

string::iterator behind = s.end() - 1;

while(front < behind){

while(!isValidLetter(*front)) front++;

while(!isValidLetter(*behind)) behind--;





if(toLowerCase(*front)==toLowerCase(*behind)){



front++;

behind--;

} else {

cout << "found non-palindrome chars." << endl;

return false;



}

}

return true;



}

No comments:

Post a Comment