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