# 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

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

### Insertion Sort Algorithm

Its a kind of incremental insertion technique, where the algorithm build up…

### Longest Substring without repeating characters - Leet Code Solution

Problem Statement Given a string, find the length of the longest substring…

### Integer to Roman conversion - Leet Code Solution

Problem Statement Roman numerals are represented by seven different symbols: I…

### Container with Most Water - Leet Code Solution

Problem Statement Given n non-negative integers a1, a2, …, an , where each…

### Maximum Subarray Problem

Problem Statement You are given an array of integers. And, you have find the…

### Leetcode - Rearrange Spaces Between Words

Problem Statement You are given a string text of words that are placed among…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…