Leetcode Note: Valid Palindrome

Source: Internet
Author: User
Tags uppercase letter

Leetcode Note: Valid Palindrome

I. Description

Ii. problem-solving skills

The concept of palindrome originated from a class of numbers in mathematics that have the following characteristics:

Set n to any natural number. If you set the numbers of nReverse ArrangementIf the obtained natural number n1 is equal to n, n is called the return number. For example, if n = 1234321, n is called the number of replies. If n = 1234567, n is not the number of replies.

Similarly, you can define the number of replies in English or Chinese. The concept is similar to the above. This question is to check whether the string is the number of replies. Different from the digital text-back, the English sentence/phrase given in the question contains spaces and punctuation marks. Therefore, you must add another layer of judgment during algorithm design. The following functions may be used:

Isalpha () // If the parameter is a letter, a non-zero number is returned; otherwise, 0 isalnum () // If the parameter is a letter or number, a non-zero number is returned; otherwise, 0 isdigit () is returned. // If the parameter is a number (0-9), a non-zero number is returned. Otherwise, 0 is returned.

If the case sensitivity of strings is not required, you can add the following functions:

Transform (string. begin (), string. end (), string. begin (), toupper); // converts the content in the string to the uppercase letter transform (string. begin (), string. end (), string. begin (), tolower); // converts the content in the string to lowercase letters.

The description of the question. The empty string can also be considered as a return text. In addition, this question does not have complex logic problems and boundary conditions.

Iii. Sample Code

#include 
  
   #include 
   
    using std::string;class Solution{public:    bool validPalindrome(string s)    {        if (s == )            return true;        auto index_start = s.begin(), index_end = prev(s.end());        while (index_start < index_end)        {            if (!isalpha(*index_start))                index_start++;            else if (!isalpha(*index_end))                index_end--;            else if (*index_start == *index_end)            {                index_start++;                index_end--;            }            else return false;        }        return true;    }};
   
  

4. Example results

Enter the echo string:

Enter a non-return string:

 

Related Article

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.