Valid palindrome Leetcode Java

Source: Internet
Author: User

Describe
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 a palindrome.
Note:have you consider this string might be empty? Is was a good question to ask during an
Interview.
For the purpose of this problem, we define empty string as valid palindrome.

Analysis
Go punctuation, see if it's a palindrome.
Code

1  Public classValidpalindrome {2      Public Static voidMain (string[] args) {3String str="a man, a plan, a Canal:panama";4System. out. println (Validpalindrome (str));5     }6      Public Staticboolean validpalindrome (String str) {7         if(str=="") 8             return true;9 //String regex= "/[^\\u4e00-\\u9fa5\\w]/g";TenString regex="[\\p{punct}\\p{space}]+";//go to the regular expression of punctuation, but not to "" and Chinese punctuation OneStr=str.replaceall (Regex,""); AString strlow=str.tolowercase (); - //return strlow; -         Char[] Ch=Strlow.tochararray (); the      -          for(intI=0; i<ch.length;i++) { -             intj=ch.length-1-i; -             if(i<=j) { +                if(ch[i]==Ch[j]) { -                    Continue; +}Else { A                    return false; at                } -             } -         } -         return true; -     } -}

Method 2 (reproduced)

No regular

1  Public Static BooleanIspalindrome (String s) {2                       if(S.length () ==0)3                           return true;4                       5s =s.touppercase ();6                       intLow1 = ' A ', high1 = ' Z ';7                       intLow2 = ' 0 ', high2 = ' 9 ';8                       intLow = 0, high = s.length ()-1;9                       Ten                       while(Low <High ) { One                          if((S.charat (low) <low1| | S.charat (Low) >high1) A&& (S.charat (low) <low2| | S.charat (Low) >high2)) { -low++; -                                  Continue; the                              } -                               -                          if((S.charat (high) <low1| | S.charat (High) >high1) -&& (S.charat (high) <low2| | S.charat (High) >high2)) { +high--; -                                  Continue; +                              } A                          if(S.charat (low) = =S.charat (High)) { atlow++; -high--; -}Else -                              return false; -                      } -                      return true; in                  } -         

Valid palindrome Leetcode Java

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.