What is Palindrome Check?
A palindrome is a string that reads the same forward and backward. The Palindrome Check algorithm compares characters from the beginning and the end of the string, moving inward until all pairs have been checked.
Example
- String: MADAM
- Compare M and M ✔
- Compare A and A ✔
- Middle character D ✔
- All comparisons matched → Palindrome
If every pair of characters matches, the string is a palindrome. If any pair differs, the algorithm immediately concludes that the string is not a palindrome.
Algorithm Steps
- Initialize two pointers: left at the beginning and right at the end.
- Compare the characters at both pointers.
- If they match, move left forward and right backward.
- If they do not match, stop — the string is not a palindrome.
- If all pairs match, the string is a palindrome.
Time Complexity
- Best Case: O(1) (first comparison fails).
- Worst Case: O(n) (all characters are checked).
Time Complexity Analysis
Using the two-pointer technique makes palindrome checking very efficient because only half of the string needs to be examined.