Tutorialspoint
Problem
Solution
Submissions

Palindrome Check

Certification: Basic Level Accuracy: 69.44% Submissions: 36 Points: 5

Write a Java program to implement the isPalindrome(String s) function, which checks if a string is a palindrome by considering only alphanumeric characters and ignoring case.

Example 1
  • Input: s = "A man, a plan, a canal: Panama"
  • Output: true
  • Explanation:
    • Cleaned string = "amanaplanacanalpanama"
    • Same forwards and backwards → true
Example 2
  • Input: s = "race a car"
  • Output: false
  • Explanation:
    • Cleaned string = "raceacar"
    • Reverse = "racaecar" → not same → false
Constraints
  • 1 ≤ s.length ≤ 2 * 10^5
  • The string consists of ASCII characters
  • Time Complexity: O(n)
  • Space Complexity: O(1)
StringsEYPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Clean the string by removing non-alphanumeric characters and converting to lowercase
  • Use two pointers, one at the start and one at the end of the cleaned string
  • Compare characters at both pointers
  • Move the pointers toward the center
  • If any characters don't match, the string is not a palindrome

Steps to solve by this approach:

 Step 1: Initialize two pointers, left at the start of the string (index 0) and right at the end (index length-1).
 Step 2: Skip non-alphanumeric characters by moving the left pointer to the right.
 Step 3: Skip non-alphanumeric characters by moving the right pointer to the left.
 Step 4: Compare the characters at the left and right pointers (ignoring case).
 Step 5: If the characters don't match, return false.
 Step 6: Move the pointers toward the center (increment left, decrement right).
 Step 7: If all characters match until the pointers meet or cross, return true.

Submitted Code :