Tutorialspoint
Problem
Solution
Submissions

Valid Palindrome

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 5

Write a C program to determine if a string is a palindrome, considering only alphanumeric characters and ignoring cases. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). The function should return true if the string is a palindrome and false otherwise.

Example 1
  • Input: s = "A man, a plan, a canal: Panama"
  • Output: true
  • Explanation: After filtering out non-alphanumeric characters and converting to lowercase: "amanaplanacanalpanama". This reads the same backward as forward, making it a palindrome.
Example 2
  • Input: s = "race a car"
  • Output: false
  • Explanation: After filtering out non-alphanumeric characters and converting to lowercase: "raceacar". This does not read the same backward as forward, so it's not a palindrome.
Constraints
  • 1 ≤ s.length ≤ 2 * 10^5
  • s consists only of printable ASCII characters
  • Time Complexity: O(n) where n is the length of the string
  • Space Complexity: O(1) as we're doing it in-place
StringsGoldman SachsTutorix
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 technique, one starting from the beginning and one from the end
  • Skip non-alphanumeric characters
  • Convert each character to lowercase before comparison
  • Compare characters from both ends moving inward
  • Return false if any comparison fails, otherwise return true

Steps to solve by this approach:

 Step 1: Initialize two pointers, left starting at the beginning of the string and right starting at the end.

 Step 2: Move the left pointer forward while the current character is not alphanumeric.
 Step 3: Move the right pointer backward while the current character is not alphanumeric.
 Step 4: Compare the characters at both pointers after converting them to lowercase.
 Step 5: If the characters don't match, return false immediately.
 Step 6: Move the left pointer forward and the right pointer backward.
 Step 7: Repeat until the pointers meet or cross each other. If we reach this point without returning false, the string is a palindrome.

Submitted Code :