Check if a String is a Palindrome Using Two Pointers
📖 Scenario: Imagine you are building a simple text checker that tells if a word or phrase reads the same forwards and backwards, ignoring spaces, punctuation, and letter case. This is called a palindrome.For example, "Race car" is a palindrome because if you ignore spaces and case, it reads the same backwards.
🎯 Goal: You will write a program that uses two pointers to check if a given string is a palindrome. The program will ignore spaces, punctuation, and case differences.
📋 What You'll Learn
Create a string variable with a specific sentence.
Create two pointer variables to track positions in the string.
Use a loop to move the pointers and compare characters.
Print True if the string is a palindrome, otherwise False.
💡 Why This Matters
🌍 Real World
Palindrome checks are used in text processing, data validation, and coding puzzles.
💼 Career
Understanding two-pointer techniques helps in solving string and array problems efficiently in software development.
Progress0 / 4 steps
1
Create the string to check
Create a string variable called text and set it to the exact value "A man, a plan, a canal: Panama".
DSA Python
Hint
Use quotes to create the string exactly as shown.
2
Set up two pointers
Create two integer variables called left and right. Set left to 0 and right to len(text) - 1.
DSA Python
Hint
Use len(text) to find the length of the string.
3
Use two pointers to check palindrome
Write a while loop that runs while left < right. Inside the loop, skip non-alphanumeric characters by moving left forward and right backward. Compare the lowercase characters at left and right. If they differ, set a variable is_palindrome to False and break the loop. If they match, move left forward and right backward. Before the loop, set is_palindrome to True.
DSA Python
Hint
Use isalnum() to check if a character is a letter or number.
Use lower() to ignore case differences.
4
Print the result
Write a print statement to display the value of is_palindrome.
DSA Python
Hint
The output should be True because the text is a palindrome.
Practice
(1/5)
1. What does the two-pointer technique check when verifying if a string is a palindrome?
easy
A. It counts the number of vowels in the string
B. It compares characters from the start and end moving towards the center
C. It reverses the string and compares with the original
D. It sorts the characters and checks if sorted string equals original
Solution
Step 1: Understand two-pointer technique
Two pointers start at the beginning and end of the string.
Step 2: Compare characters moving inward
Characters at both pointers are compared; pointers move closer if equal.
Final Answer:
It compares characters from the start and end moving towards the center -> Option B
Hint: Compare characters from both ends moving inward [OK]
Common Mistakes:
Thinking it reverses the string
Checking only vowels or sorting characters
Using one pointer only
2. Which of the following is the correct syntax to initialize two pointers for a palindrome check in Python?
easy
A. left = 1; right = len(s)
B. left = -1; right = len(s) + 1
C. left = len(s); right = 0
D. left = 0; right = len(s) - 1
Solution
Step 1: Identify pointer start positions
Left pointer starts at 0 (first character), right pointer at last index (length - 1).
Step 2: Check syntax correctness
left = 0; right = len(s) - 1 correctly sets left=0 and right=len(s)-1 for valid indexing.
Final Answer:
left = 0; right = len(s) - 1 -> Option D
Quick Check:
Start pointers at 0 and last index [OK]
Hint: Start left at 0 and right at length minus one [OK]
Common Mistakes:
Starting right pointer at length (out of range)
Starting left pointer at 1 (skips first char)
Using negative or invalid indices
3. What will be the output of the following code?
def is_palindrome(s):
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
print(is_palindrome('racecar'))
print(is_palindrome('hello'))
medium
A. True\nTrue
B. False\nTrue
C. True\nFalse
D. False\nFalse
Solution
Step 1: Check 'racecar' palindrome
Characters match from both ends, so returns True.
Step 2: Check 'hello' palindrome
Characters differ ('h' != 'o'), returns False.
Final Answer:
True\nFalse -> Option C
Quick Check:
'racecar' palindrome = True, 'hello' not = False [OK]
Hint: Check characters from ends; mismatch means False [OK]
Common Mistakes:
Assuming 'hello' is palindrome
Returning True without full check
Mixing up True/False outputs
4. Find the error in this palindrome check code:
def check_palindrome(s):
left, right = 0, len(s)
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
medium
A. right should be initialized as len(s) - 1
B. left should start at 1
C. The while condition should be left <= right
D. The function should return False at the end
Solution
Step 1: Check pointer initialization
Right pointer is set to len(s), which is out of string index range.
Step 2: Correct pointer initialization
Right should be len(s) - 1 to point to last character index.
Final Answer:
right should be initialized as len(s) - 1 -> Option A
Quick Check:
Right pointer must be last index, not length [OK]
Hint: Right pointer = length minus one, not length [OK]
Common Mistakes:
Using len(s) as index causes IndexError
Changing left pointer start incorrectly
Incorrect loop condition
5. Given the string s = "A man, a plan, a canal: Panama", which modification is needed in the two-pointer palindrome check to correctly identify it as a palindrome?
hard
A. Ignore non-alphanumeric characters and case during comparison
B. Reverse the string and compare directly
C. Only compare vowels in the string
D. Convert string to uppercase and compare characters directly
Solution
Step 1: Understand input complexity
String contains spaces, commas, and mixed case which affect direct comparison.
Step 2: Modify comparison logic
Skip non-alphanumeric characters and compare lowercase versions to ignore case and symbols.
Step 3: Why other options fail
Reversing or uppercase alone won't ignore spaces/punctuation; vowels only is incorrect.
Final Answer:
Ignore non-alphanumeric characters and case during comparison -> Option A
Quick Check:
Clean string and ignore case for palindrome check [OK]
Hint: Skip symbols and compare lowercase chars [OK]
Common Mistakes:
Not ignoring punctuation and spaces
Only changing case without skipping symbols
Comparing vowels only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.