valid palindrome

valid palindrome

Not hard

put the string all to left, change the upcase to lowcase. Numbers stay and ignore theothers.

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int l = s.length();
 5         if ( 0 == l ) {
 6             return true;
 7         }
 8         int i = 0;
 9         int j = 0;
10         while ( i < l ) {
11             if ( ( s[i] >= 'a' && s[i] <= 'z' ) || ( s[i] >= '0' && s[i] <= '9' ) ) {
12                 s[j++] = s[i++];
13             } else {
14                 if ( s[i] >= 'A' && s[i] <= 'Z' ) {
15                     s[j++] = s[i++]-'A'+'a';
16                 }
17                 else {
18                     i++;
19                 }
20             }
21         }
22         i = j-1;
23         j = 0;
24         while ( j <= i ) {
25             if ( s[i] != s[j] ) {
26                 return false;
27             }
28             i--;
29             j++;
30         }
31         return true;
32      }
33 };

Loading Disqus comments...
Table of Contents