# Valid Palindrome - Leet Code Solution

September 11, 2020

## Problem Statement

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

``````Input: "A man, a plan, a canal: Panama"
Output: true

Input: "race a car"
Output: false``````

## Solution

Please note the special conditions:

1. Ignore case
2. Ignore any non-alphanumeric character

Lets run our simple two pointer system where:

• One pointer will start from left, while other start from extreme right end.
• Lets move left and right pointers untill they are pointing to a non-alphanumeric character
• Compare characters at both left and right position, check must be case-insensitive

### Code

``````public static boolean isAlphanumeric(char c) {
return Character.isDigit(c) || Character.isLetter(c);
}
public boolean isPalindrome(String s) {
if (s.length() == 0) return true;

int l = 0;
int r = s.length()-1;

while (l < r) {
while (!isAlphanumeric(s.charAt(l)) && l < r) {
l++;
}
while (!isAlphanumeric(s.charAt(r)) && l < r) {
r--;
}

if (Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r))) {
return false;
}

l++;
r--;
}

return true;
}``````

### Complexity

Its `O(n)`

## Similar Posts

### Max Priority Queue Implementation with Heap Data structure

Max Priority Queue is a data structure which manage a list of keys(values). And…

### What is Heap Data Structure

Its a tree based data structure which is a complete binary tree(all nodes have…

### Longest Common Prefix - Leet Code Solution

Problem Statement Write a function to find the longest common prefix string…

### Maximum Length of Subarray With Positive Product - Leet Code Solution

Problem Statement Maximum Length of Subarray With Positive Product. Given an…

### How to prepare for your next Coding Interview

Here are some tips while preparing for your coding interviews. 1. Do study or…

### Binary Tree - Level Order Traversal

Problem Statement Given a Binary tree, print out nodes in level order traversal…

## Latest Posts

### Authenticating Strapi backend with Next.js and next-auth using credentials and jwt

Introduction Strapi is a backend system provides basic crud operations with…

### How to create Repository using Github Rest API, Configure Visibility and Assign a Team as Readonly

Introduction I had to create many repositories in an Github organization. I…

### How to Download multiple Youtube Videos using Nodejs and Show a Progress Bar

Introduction I was trying to download some youtube videos for my kids. As I have…

### Python - Some useful Pytest Commands

Introduction In this post, we will explore some useful command line options for…

### Python - How to apply patch to Python and Install Python via Pyenv

Introduction In this post, we will see how we can apply a patch to Python and…

### How to Install packages from command line and Dockerfile with Chocolatey

Introduction We will introduce a Package Manager for Windows: . In automations…