Valid Palindrome Solution for Cognizant
Company: Cognizant · Difficulty: Easy · Topics: String, Two Pointers
Problem Statement
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.
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
Explanation: "raceacar" is not a palindrome.
Approach
Use two pointers (left and right) to compare characters from both ends, skipping non-alphanumeric characters.
Time and Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)