Valid Palindrome - Leet Code Solution

Gorav Singal

September 11, 2020

TL;DR

Two pointers from both ends — skip non-alphanumeric characters, compare case-insensitively. O(n) time, O(1) space.

Valid Palindrome - Leet Code Solution

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)

Share

Related Posts

Replace all spaces in a string with %20

Replace all spaces in a string with %20

Problem Statement Replace all spaces in a string with ‘%20’ (three characters…

Valid Anagrams - Leet Code Solution

Valid Anagrams - Leet Code Solution

Problem Statement Given two strings s and t , write a function to determine if t…

First Unique Character in a String - Leet Code Solution

First Unique Character in a String - Leet Code Solution

Problem Statement Given a string, find the first non-repeating character in it…

Reverse String - Leet Code Solution

Reverse String - Leet Code Solution

Problem Statement Write a function that reverses a string. The input string is…

Rotate Image - Leet Code Solution

Rotate Image - Leet Code Solution

Problem Statement You are given an n x n 2D matrix representing an image, rotate…

Validate Sudoku - Leet Code Solution

Validate Sudoku - Leet Code Solution

Problem Statement Determine if a 9x9 Sudoku board is valid. Only the filled…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Security Ticketing and Incident Response

Security Ticketing and Incident Response

The worst time to figure out your incident response process is during an…