15 Jun 2024C++ / Python / TypeScriptEasy

Valid Palindrome

Collected C++, Python, TypeScript solutions for valid palindrome. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Valid Palindrome. the solution files are available below.

solution files

  • C++ valid-palindrome/synced-solution.cpp
  • Python valid-palindrome/synced-solution.py
  • TypeScript valid-palindrome/synced-solution.ts

Solution files

Pythonvalid-palindrome/synced-solution.py
class Solution:
    def isPalindrome(self, s: str) -> bool:
        filtered = class="syntax-string">''.join(char.lower() for char in s if char.isalnum())
        return filtered == filtered[::-class="syntax-number">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;
    }
};
TypeScriptvalid-palindrome/synced-solution.ts
function isPalindrome(s: string): boolean {
    const filtered = s.replace(/[^a-zA-Z0-class="syntax-number">9]/g, class="syntax-string">'').toLowerCase();
    return filtered === filtered.split(class="syntax-string">'').reverse().join(class="syntax-string">'');
}