πŸ“˜ Day 3 – Teacher's Day Challenge | Valid Palindrome Using Two Pointers

TanishiTanishi
2 min read

πŸ—“οΈ Date: July 27, 2025
πŸ”’ Problems Solved Today: 1
🎯 Focus Topic: Strings, Two Pointer Technique, Character Validation

🧩 Platform: Leetcode


πŸ”— Table of Contents


🧩 Problem 1 – Valid Palindrome

πŸ“š Difficulty: Easy
🧠 Concepts: Two Pointers, String Cleaning, Alphanumeric Check


πŸ“„ Problem Statement (Summary):
Given a string s, return true if it is a palindrome after:

  • Converting to lowercase

  • Removing all non-alphanumeric characters

Examples:

  • Input: "A man, a plan, a canal: Panama" β†’ Output: true

  • Input: "race a car" β†’ Output: false

  • Input: " " β†’ Output: true (Empty string after cleanup is a palindrome)


πŸ’‘ Approach:

  • Convert all characters to lowercase

  • Use two pointers from start and end

  • Skip non-alphanumeric characters using a helper function

  • Compare characters and move inward

  • If any mismatch found, return false; else return true


πŸ§ͺ Code (C++):

class Solution {
public:
    bool isAlphaNum(char ch) {
        return (ch >= 'A' && ch <= 'Z' || 
                ch >= 'a' && ch <= 'z' || 
                ch >= '0' && ch <= '9');
    }

    bool isPalindrome(string s) {
        for (int i = 0; i < s.length(); i++) {
            s[i] = tolower(s[i]);
        }

        int st = 0, e = s.length() - 1;

        while (st <= e) {
            if (!isAlphaNum(s[st])) {
                st++;
                continue;
            }
            if (!isAlphaNum(s[e])) {
                e--;
                continue;
            }
            if (s[st] != s[e]) {
                return false;
            }
            st++, e--;
        }
        return true;
    }
};

πŸ•’ Time Complexity: O(n)
πŸ“¦ Space Complexity: O(1) (modifies in-place)

πŸ“Έ Submission Screenshot:


βœ… Key Takeaways:

  • Lowercasing and character filtering is essential pre-processing

  • Two pointer pattern is efficient for palindrome check

  • Use helper method for clean alphanumeric validation

  • Don’t forget empty string edge case – it’s valid


πŸ“ˆ Daily Summary

MetricValue
Problems Solved1
Topics CoveredTwo Pointers, Alphanumeric Filtering
Tools UsedC++
Day3 / 30

🏷️ Tags:
#leetcode #43DaysChallenge #palindrome #twopointers #cpp #strings #validPalindrome #dsa

0
Subscribe to my newsletter

Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanishi
Tanishi