coding-interview|September 11, 2020|1 min read

Valid Palindrome - Leet Code Solution

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)

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

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…

HTTP Cookies Security — Everything Developers Get Wrong

HTTP Cookies Security — Everything Developers Get Wrong

Cookies are the single most important mechanism for web authentication. Every…

Format String Vulnerabilities — The Read-Write Primitive Hiding in printf()

Format String Vulnerabilities — The Read-Write Primitive Hiding in printf()

Format string vulnerabilities are unique in the exploit world. Most memory…

Buffer Overflow Attacks — How Memory Corruption Actually Works

Buffer Overflow Attacks — How Memory Corruption Actually Works

Buffer overflows are the oldest and most consequential vulnerability class in…