Leetcode------Valid Palindrome

Source: Internet
Author: User
Tags alphanumeric characters

Title: Valid palindrome
Pass Rate: 21.9%
Difficulty: Simple

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.

Before doing a palindrome number of judgments, palindrome number than this topic more difficult, because palindrome number can not use extra space, if let the extra space becomes the subject, the problem is to remove punctuation, and to ignore the case, then use Java is very simple. It's OK to replace all non-alphanumeric letters with regular expressions to null. Then it is from both ends to judge, as long as it is not equal to return false, if not the first function, then the implementation of a judgment is an alphanumeric function and a case conversion function can be directly read the code:

1  Public classSolution {2      Public BooleanIspalindrome (String s) {3         intLen=s.replaceall ("[^a-za-z0-9]", "" "). Length ();4String Tmp=s.replaceall ("[^a-za-z0-9]", ""). toLowerCase (); 5         if(len==0)return true;6          for(inti=0;i<len/2;i++){7             if(Tmp.charat (i)!=tmp.charat (len-1-i))return false;8         }9         return true;Ten          One     } A}

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.