Valid Palindrome

LeetCode Q 125 - Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:
Input: "A man, a plan, a canal: Panama" ; Output: true

Example 2:
Input: "race a car" ; Output: false

Solution

Code:

public boolean isPalindrome(String s) {
	
	if (s == null || s.length() == 0)
		return true;
	
	int i = 0, j = s.length() - 1;
	
	while (i < j) {
	
		if (!Character.isLetterOrDigit(s.charAt(i))) {
			i++; continue;
		}
		
		if (!Character.isLetterOrDigit(s.charAt(j))) {
			j--; continue;
		}
		
		if (Character.toUpperCase(s.charAt(i)) != 
			Character.toUpperCase(s.charAt(j)))
			return false;
		else {
			i++; j--;
		}
	}
	
	return true;
}

   Reprint policy


《Valid Palindrome》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
 Previous
Wiggle Sort II Wiggle Sort II
LeetCode Q 324 - Wiggle Sort IIGiven an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] <
2019-05-09 Tong Shi
Next 
3Sum Closest 3Sum Closest
LeetCode Q 16 - 3Sum ClosestGiven an array nums of n integers and an integer target, find three integers in nums such th
2019-05-09 Tong Shi
  TOC