Leetcode:valid palindrome

Source: Internet
Author: User
Tags alphanumeric characters

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.

Analysis: Test instructions to give a string, check whether it is a palindrome case. Only alphanumeric characters are considered and case-insensitive is ignored.

Note 1, consider the case that the string is empty, and use it as a palindrome

2, ignoring the difference between the case, the comparison of the case before the first conversion to a consistent

3, non-alphanumeric characters to skip, filter it, get a valid string

4, the effective string before and after the corresponding position to compare and judge

The code is as follows:

Class Solution {Public:bool isstr (char &ch) {if (ch >= ' 0 ' && ch <= ' 9 ') {R          Eturn true;          } else if (ch >= ' a ' && ch <= ' Z ') {return true;             } else if (ch >= ' A ' && ch <= ' Z ') {ch + = 32;          return true;      } return false;  } bool Ispalindrome (string s) {//Start typing your C + + solution below//do not write int          Main () function int len = s.length ();          if (len = = 0) {return true;          } string str = ""; for (int i = 0; i < len; i++) {//remove illegal char, such as "?"              "/" ... if (Isstr (S[i])) {str + = s[i];          }} len = Str.length ();          int mid = (len + 1)/2; for (int i = 0; i < mid; i++) {if (Str[i]! = str[len-1-i]) {//check front and endchar return false;      }} return true;  }  };

Other solutions:

Leetcode:valid palindrome

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.