LeetCode 125 Valid Palindrome (Valid return )(*)

Source: Internet
Author: User
Tags alphanumeric characters

LeetCode 125 Valid Palindrome (Valid return )(*)
Translation

Specify a string to determine whether it is a return object. consider only the numbers and characters and ignore others. For example, "A man, a plan, a canal: Pannama" is input. "Race a car" is not a reply comment: Do you think the string may be empty? This interview is a good question. For the intention of this problem, we define a null string as a return.
Original
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.
Analysis

It is not difficult to solve the problem.

The first step is to remove the interference from the original string, that is, keep only letters, which can be determined by ascii.

Then construct a new string and then judge whether the new string is a return string.

However, I am still wrong. First of all, I have no idea about the example of the question. "aA" is also a reply.

After another modification, I still got it wrong, because the alphanumeric of the question contains letters and numbers. At the beginning, I translated letters, and later I modified the above translation.

So I decided to thoroughly reform and separate the functions. However, the code is long but clear:

Code
Class Solution {public: bool isAlpha (char c) {int ascii = (int) c; if (ascii> = 65 & ascii <= 90) | (ascii> = 97 & ascii <= 122) return true; else return false;} bool isNumber (char c) {int ascii = (int) c; if (ascii> = 48 & ascii <= 57) return true; else return false;} bool isAlphaAndNumber (char c1, char c2) {if (isAlpha (c1) & isNumber (c2) | (isAlpha (c2) & isNumber (c1) return true; Else return false;} bool isPalindrome (string s) {if (s. size () = 0) return true; string newStr = ""; for (int I = 0; I <s. size (); ++ I) {// only take out the letters and numbers in it. if (isAlpha (s [I]) | isNumber (s [I]) newStr + = s [I];} for (int I = 0; I <newStr. size ()/2; ++ I) {// both are letters and numbers if (isAlphaAndNumber (newStr [I], newStr [newStr. size ()-I-1]) return false; // both are numerical else if (isNumber (newStr [I]) & isNumbe R (newStr [newStr. size ()-I-1]) {// determine if it is the same number if (newStr [I]! = NewStr [newStr. size ()-I-1]) return false;} // both are the letters else {// determine whether it is the same letter, check whether it is case-sensitive. if (newStr [I]! = NewStr [newStr. size ()-I-1] & abs (int) newStr [I]-(int) newStr [newStr. size ()-I-1])! = 32) return false ;}} return true ;}};
Advanced

At the thought of ascii, I forgot to have toupper functions. Others wrote them ......

class Solution {public:    bool isPalindrome(string s) {        int l = 0, r = s.size() - 1;        while(l <= r){            while(!isalnum(s[l]) && l < r) l++;            while(!isalnum(s[r]) && l < r) r--;            if(toupper(s[l]) != toupper(s[r])) return false;            l++, r--;        }        return true;    }};

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.