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
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.
valid-palindrome/synced-solution.cppvalid-palindrome/synced-solution.pyvalid-palindrome/synced-solution.tsclass 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]
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;
}
};
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">'');
}