Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Valid Palindrome

C++, Python and TypeScript solutions for Valid Palindrome.

The Valid Palindrome solutions are below.

Solution files

Python•valid-palindrome/synced-solution.py
class Solution:
    def isPalindrome(self, s: str) -> bool:
        filtered = ''.join(char.lower() for char in s if char.isalnum())
        return filtered == filtered[::-1]
C++•valid-palindrome/synced-solution.cpp
class Solution {
public:
    bool isPalindrome(string s) {
        string filtered;
        for (char c : s) {
            if (isalnum(c)) {
                filtered += tolower(c);
            }
        }
        string reversed = filtered;
        reverse(reversed.begin(), reversed.end());
        return filtered == reversed;
    }
};
TypeScript•valid-palindrome/synced-solution.ts
function isPalindrome(s: string): boolean {
    const filtered = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
    return filtered === filtered.split('').reverse().join('');
}