Valid Palindrome — Two Pointer Approach
Problem
We are given a string s. We need to check whether the string is a palindrome or not.
While checking the palindrome:
- We don’t consider spaces or special characters.
- We don’t care about uppercase or lowercase letters.
For example:
"A man, a plan, a canal: Panama"This is a palindrome because after removing spaces and special characters and ignoring the case, it becomes:
"amanaplanacanalpanama"Approach
First, I create a new string variable called s1.
string s1 = "";Then I loop through all the characters of the original string s.
for (char c : s)For each character, I check whether it is a letter or a number using the isalnum() function.
if (isalnum(c))isalnum() returns true if the character is an alphabet or a number. Otherwise, it returns false.
So, spaces and special characters will not be stored in s1.
I also convert every valid character to lowercase using tolower() and store it in s1.
s1 += tolower(c);For example:
Original:
"A man, a plan!"After filtering and converting to lowercase:
"amanaplan"Now s1 contains only the characters that we actually need to compare.
Two Pointer Approach
After creating the new string, I use the Two Pointer approach.
I create two pointers:
int st = 0, end = s1.size() - 1;Here:
stpoints to the first character.endpoints to the last character.
Then I use a while loop:
while (st < end)The loop continues until the two pointers meet or cross each other.
Inside the loop, I compare the characters at both pointers.
if (s1[st] != s1[end])If the first character and last character are different, then the string cannot be a palindrome.
Get Tanvir Ahmed Tuhin’s stories in your inbox
Join Medium for free to get updates from this writer.
So, I immediately return false.
return false;On the other hand, if both characters are the same, I move both pointers toward the center.
st++;
end--;For example:
s1 = "madam"m a d a m
↑ ↑
st endFirst:
m == mSo we move the pointers:
m a d a m
↑ ↑
st endAgain:
a == aThen we move them again and reach the middle.
If the loop finishes without finding any different pair of characters, that means all corresponding characters are the same.
Therefore, the string is a palindrome, so we return:
return true;C++ Solution
class Solution {
public:
bool isPalindrome(string s) {string s1 = ""; for (char c : s) {
if (isalnum(c)) {
s1 += tolower(c);
}
} int st = 0, end = s1.size() - 1; while (st < end) {
if (s1[st] != s1[end]) {
return false;
} else {
st++;
end--;
}
} return true;
}
};
Complexity
Let n be the length of the original string.
Time Complexity: O(n)
We traverse the string once to create s1, and then traverse the valid characters again using two pointers.
So the overall time complexity is:
O(n)Space Complexity: O(n)
We create a new string s1 to store the valid characters.
Therefore, the space complexity is:
O(n)Conclusion
The main idea is simple:
- Remove spaces and special characters using
isalnum(). - Convert characters to lowercase using
tolower(). - Use two pointers, one from the beginning and one from the end.
- Compare both characters.
- If any pair is different, return
false. - If all pairs are the same, return
true.
This makes the palindrome check simple and easy to understand using the Two Pointer technique.






