Valid Palindrome

xiaoxiao2021-02-28  63

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.

代码如下:

public class ValidPalindrome { public boolean isPalindrome(String s) { int low=0,high=s.length()-1; while(low<high){ char clow = s.charAt(low); char chigh = s.charAt(high); if(!Character.isLetterOrDigit(clow)) low++; else if(!Character.isLetterOrDigit(chigh)) high--; else{ if(Character.toLowerCase(clow) != Character.toLowerCase(chigh)) return false; low++; high--; } } return true; } }

转载请注明原文地址: https://www.6miu.com/read-59660.html

最新回复(0)