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.

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4       if (s == "")
 5             return true;
 6         int len = s.size();
 7         transform(s.begin(), s.end(), s.begin(), ::tolower);
 8         string s1="";
 9         for (int i = 0; i < len; i++)
10         {
11             if (isalnum(s[i]))
12             {
13                 s1 += s[i];
14             }
15         }
16         string s2 = "";
17         s2 = s1;
18         //transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
19         //transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
20         reverse(s1.begin(), s1.end());
21         cout << s2 << " " << s1 << endl;
22         if (s2 == s1)
23             return true;
24         else
25             return false;
26     }
27 };

 

posted on 2017-07-17 09:26  无惧风云  阅读(206)  评论(0)    收藏  举报