Check Palindrome by Filtering Non-Letters
Origin: Check Palindrome by Filtering Non-Letters
Given a string containing letters, digits, and symbols, determine if it reads the same forwards and backwards when considering only alphabetic characters (case-insensitive).
Example
Input
code = A1b2B!aOutput
1Explanation
- Step 1: Extract only letters → ['A','b','B','a']
- Step 2: Convert to lowercase → ['a','b','b','a']
- Step 3: Compare sequence forward and backward: 'abba' == 'abba' → trueInput Format
- A string code containing letters (A–Z, a–z), digits (0–9), and symbols
Constraints
- 0 ⇐ code.length ⇐ 1000
- For all 0 ⇐ i < code.length: 33 ⇐ ASCII(code[i]) ⇐ 126
- code contains only printable ASCII characters (letters, digits, symbols)
Output Format
- Return a boolean value: 1 if true & 0 if false. Sample Input 0
ZSample Output 0
1Sample Input 1
abc123cbaSample Output 1
1Resolution
function isAlphabeticPalindrome(code: string): boolean {
// Write your code here
if (!code)
return false;
const arr = code
.toLocaleLowerCase()
.split('')
.filter(s => /[a-z]/i.test(s));
const reArr = [...arr].reverse();
// const reArr = arr.toReversed(); es2023+
return reArr.join() === arr.join();
}