AlgoInsight

DSA problem

Check if a String is a Palindrome

EasyStringsPattern: Two PointersbeginnerFreeMicrosoftFacebook

Given a string s, check whether it reads the same forward and backward. Ignore case and consider only alphanumeric characters.

Use two pointers starting from both ends. Skip non-alphanumeric characters, compare characters in a case-insensitive manner, and move inward. If all compared characters match, it is a palindrome. The ignore-non-alphanumeric part is what makes this interesting in interviews - A man, a plan, a canal: Panama is a palindrome once you strip punctuation and spaces. This teaches character classification and input sanitization, which is essential in real-world input validation. A follow-up: what about Unicode? In practice you would handle multi-byte characters and normalization, but for interview purposes ASCII is sufficient.

Examples

Example 1
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: Ignoring case, spaces, and punctuation, the string reads "amanaplanacanalpanama" the same forward and backward.
Example 2
Input: s = "race a car"
Output: false
Explanation: Stripped down to "raceacar", it does not read the same in reverse.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringtwo-pointerspalindrome

Browse all DSA problems