Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

DSA›Strings›Valid Palindrome
EasyStrings

Valid Palindrome

stringtwo-pointers

Problem

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward.

Given a string s, return true if it is a palindrome, or false otherwise.

Examples

Example 1

Input: s = "A man, a plan, a canal: Panama"

Output: true

Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2

Input: s = "race a car"

Output: false

Constraints

  • •1 <= s.length <= 2*10^5

Hints

Hint 1

Two pointers from both ends, skipping non-alphanumeric characters as you go — no need to build a separate cleaned string first.

Hint 2

A simpler-to-write brute force: build a fully cleaned, lowercased copy of the string, then compare it directly against its own reverse — correct, but costs O(n) extra space the two-pointer version avoids entirely.

Solutions

public boolean isPalindromeBruteForce(String s) {
    StringBuilder cleaned = new StringBuilder();
    for (char c : s.toCharArray()) {
        if (Character.isLetterOrDigit(c)) cleaned.append(Character.toLowerCase(c));
    }
    String forward = cleaned.toString();
    String backward = cleaned.reverse().toString();
    return forward.equals(backward);
}

Time: O(n) · Space: O(n) for the cleaned/reversed strings