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"IsNotA 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.
The identification of the return string. Only the numbers and letters are required.
Personal thoughts:
1. construct a new string S1, which only contains letters and numbers of the original string and converts all letters to lowercase letters.
2. construct another string S2, which is the reverse string of S1.
3. judge whether S1 and S2 are equal. If they are equal, it indicates that they are input strings. Otherwise, they are not
Code:
1 # include <string> 2 // # include <cctype> 3 // # include <iostream> 4 5 using namespace STD; 6 7 class solution {8 public: 9 bool ispalindrome (string s) {10 if (S. empty () 11 {12 Return true; 13} 14 15 string S1 (""); 16 17 for (INT I = 0; I <S. length (); ++ I) 18 {19 if (s [I] <= '9' & S [I]> = '0 ') // number 20 {21 s1.insert (s1.end (), s [I]); 22} 23 else if (s [I] <= 'Z' & S [I]> = 'A') // uppercase letter 24 {25 s1.insert (S1. End (), tolower (s [I]); 26} 27 else if (s [I] <= 'Z' & S [I]> = 'A ') // lowercase letter 28 {29 s1.insert (s1.end (), s [I]); 30} 31} 32 33 string S2 (""); 34 35 for (INT I = s1.length ()-1; I> = 0; -- I) 36 {37 s2.insert (s2.end (), S1 [I]); 38} 39 40 if (! S1.compare (S2) 41 {42 return true; 43} 44 else45 {46 Return false; 47} 48} 49}; 50/* 51 int main () 52 {53 string S ("A man, a plan, a canal: Panama"); 54 55 solution so; 56 cout <so. ispalindrome (s) <Endl; 57 58 system ("pause"); 59 60 return 0; 61} 62 */View code
This idea is simple and straightforward, but it is not very good. The time consumption O (N) and space consumption O (n) can actually save space consumption. The idea is as follows:
1. First, convert the letters in the original string to lowercase letters.
2. Then, set two pointers, one pointing to the header and the other pointing to the tail. If both the header and the tail pointer point to a number or letter (when a pointer points to another character, skip this character, point to the next character until it points to a number or letter), and then compare. If the two pointers are the same, take one step in the middle until the first and last pointers meet each other. This indicates that the string is a reply string, if a difference occurs during the comparison, it indicates that the string is not a background string.
Code:
1 #include <string> 2 3 using namespace std; 4 5 class Solution { 6 public: 7 bool isPalindrome(string s) { 8 if (s.empty()) 9 {10 return true;11 }12 13 for (string::iterator it = s.begin(); it != s.end(); ++it)14 {15 *it = tolower(*it);16 }17 18 string::iterator head = s.begin(), tail = prev(s.end());19 20 while(head < tail)21 {22 if (!isalnum(*head))23 {24 ++head;25 }26 else if (!isalnum(*tail))27 {28 --tail;29 }30 else if(*head != *tail)31 {32 return false;33 }34 else35 {36 ++head;37 --tail;38 }39 40 }41 42 return true;43 }44 };View code
Leetcode-valid palindrome