Valid Palindrome

Author: Nikhitha
Category: C++ | Problem Solving | LeetCode
Topic: String Manipulation + Two Pointer Technique
π Problem Statement
Given a string
s
, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
β Example Inputs & Outputs
Input | Output |
"A man, a plan, a canal: Panama" | true |
"race a car" | false |
"" | true |
π‘ What Is a Palindrome?
A palindrome is a word or sentence that reads the same forwards and backwards, ignoring punctuation, spaces, and letter casing.
π§ͺ Solution 1: Brute Force (Clean, Reversed Comparison)
βοΈ Intuition:
Filter the input string to remove all non-alphanumeric characters.
Convert everything to lowercase for case-insensitive comparison.
Reverse the filtered string.
If the original filtered string equals the reversed one β itβs a palindrome!
π Code:
class Solution {
public:
bool isPalindrome(string s) {
int n = s.size();
if(s == "")
return true;
string result = "";
for (int i = 0; i < n; i++) {
if (isalnum(s[i]))
result += tolower(s[i]);
}
string reversal = result;
reverse(reversal.begin(), reversal.end());
return result == reversal;
}
};
β±οΈ Time & Space Complexity:
Time: O(n)
Space: O(n) β for the
result
andreversal
strings.
βοΈ Key Functions Used:
isalnum(c)
β checks if characterc
is a letter or digit.tolower(c)
β converts characterc
to lowercase.reverse(str.begin(), str.end())
β reverses the string.
π Solution 2: Optimized β Two Pointer Technique
βοΈ Intuition:
Letβs be memory-smart! Why create new strings when we can just compare characters from both ends?
Use two pointers:
left
at start,right
at end.Skip non-alphanumeric characters.
Convert both chars to lowercase and compare.
If all pairs match β itβs a palindrome.
π Code:
class Solution {
public:
bool isPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
while (left < right && !isalnum(s[left])) left++;
while (left < right && !isalnum(s[right])) right--;
if (tolower(s[left]) != tolower(s[right]))
return false;
left++;
right--;
}
return true;
}
};
π§ Key Concepts:
Concept | Purpose |
Two Pointers | Compare characters from both ends |
isalnum() | Ignore spaces, punctuation, etc. |
tolower() | Make comparison case-insensitive |
In-place logic | Saves space by not creating new strings |
β±οΈ Time & Space Complexity:
Time: O(n)
Space: O(1) β no extra strings, pure pointer logic!
π Final Thoughts
Feature | Brute Force | Optimized (Two Pointers) |
Simplicity | β Easy to understand | π§ Slightly more logic-heavy |
Time Complexity | O(n) | O(n) |
Space Complexity | O(n) | β O(1) |
Ideal For | Learning phase, quick demo | Production-level usage |
If you're prepping for coding interviews or placements, go with the two pointer version β it's clean, memory-efficient, and shows you understand core algorithmic patterns.
π§ͺ Test Cases to Try
Solution sol;
cout << sol.isPalindrome("race a car") << endl; // false
cout << sol.isPalindrome(" ") << endl; // true
cout << sol.isPalindrome("0P") << endl; // false
cout << sol.isPalindrome("madam") << endl; // true
problem link: https://leetcode.com/problems/valid-palindrome/description/
Subscribe to my newsletter
Read articles from Nikhitha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nikhitha
Nikhitha
π Software Developer | DSA Enthusiast | Problem Solver πΉ Sharing daily DSA solutions, coding insights, and interview prep πΉ Passionate about writing optimized & bug-free code