Tutorialspoint
Problem
Solution
Submissions

Palindrome Check

Certification: Basic Level Accuracy: 75% Submissions: 4 Points: 5

Write a JavaScript function to determine if a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. For this problem, consider only alphanumeric characters and ignore case sensitivity.

Example 1
  • Input: s = "racecar"
  • Output: true
  • Explanation:
    • The string "racecar" contains only lowercase letters.
    • Reading from left to right: r-a-c-e-c-a-r.
    • Reading from right to left: r-a-c-e-c-a-r.
    • Both directions give the same sequence, so it's a palindrome.
Example 2
  • Input: s = "A man, a plan, a canal: Panama"
  • Output: true
  • Explanation:
    • The string contains letters, spaces, and punctuation.
    • Considering only alphanumeric characters: "AmanaplanacanalPanama".
    • Converting to lowercase: "amanaplanacanalpanama".
    • This reads the same forwards and backwards, so it's a palindrome.
Constraints
  • 1 ≤ s.length ≤ 2 * 10^5
  • s consists only of printable ASCII characters
  • Consider only alphanumeric characters
  • Ignore case sensitivity
  • Time Complexity: O(n)
  • Space Complexity: O(1)
StringsWalmartD. E. Shaw
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

  • Use two pointers approach: one from the beginning and one from the end
  • Skip non-alphanumeric characters when moving the pointers
  • Convert characters to lowercase for case-insensitive comparison
  • Move pointers toward each other and compare characters
  • If any pair doesn't match, return false
  • If all pairs match, return true

Steps to solve by this approach:

 Step 1: Initialize two pointers, left at start and right at end of string
 Step 2: Create helper function to check if character is alphanumeric
 Step 3: Move left pointer forward, skipping non-alphanumeric characters
 Step 4: Move right pointer backward, skipping non-alphanumeric characters
 Step 5: Compare characters at both pointers after converting to lowercase
 Step 6: If characters don't match, return false immediately
 Step 7: Continue until pointers meet or cross, then return true

Submitted Code :