2026年3月29日 星期日

[HackerRank] Check Palindrome by Filtering Non-Letters

Check Palindrome by Filtering Non-Letters

Difficulty: Easy

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!a

Output

1

Explanation

- 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'  true

Input 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

Z

Sample Output 0

1

Sample Input 1

abc123cba

Sample Output 1

1 


Consideration:

1. Two Pointers: Left index, Right index to compare the alphabetic characters.

2. If not alphabetic characters, jump to the next index. (Left Index ++, Right Index --)

3. case insensitive: compare the distance with 'A' or 'a'.


Code:

/*
 * Complete the 'isAlphabeticPalindrome' function below.
 *
 * The function is expected to return a BOOLEAN.
 * The function accepts STRING code as parameter.
 */

bool isAlphabeticPalindrome(string code) {
    int n = code.size();
    //cout << "n = " << n << "\n";
    int i = 0, j = n-1;
    if (n==0 || n==1) return 1;
   
    while (i<j) {
        //cout << "i = " << i << "\n";
        //cout << "j = " << j << "\n";
        if (((code[i]>='A') && (code[i] <= 'Z')) || ((code[i] >= 'a') && (code[i] <= 'z'))) {          
            if (((code[j]>='A') && (code[j] <= 'Z')) || ((code[j] >= 'a') && (code[j] <= 'z'))) {
                if (code[i]==code[j]) {
                    i++;
                    j--;
                }
                else if (code[i]>code[j]) {
                    if (code[i]>='a') {
                        if ((code[j]-'A')==(code[i]-'a')){
                            i++;
                            j--;
                        }
                        else {
                            //cout << "i = " << i << "[" << code[i] << "]" << "\n";
                            //cout << "j = " << j << "[" << code[j] << "]" << "\n";  
                            return 0;
                        }
                    }
                    else {
                        //cout << "i = " << i << "[" << code[i] << "]" << "\n";
                        //cout << "j = " << j << "[" << code[j] << "]"<< "\n";
                        return 0;
                    }
                }
                else if (code[i]<code[j]) {
                    if (code[j]>='a') {
                        if ((code[i]-'A')==(code[j]-'a')){
                            i++;
                            j--;
                        }
                        else {
                            //cout << "i = " << i << "[" << code[i] << "]" << "\n";
                            //cout << "j = " << j << "[" << code[j] << "]" << "\n";                            
                            return 0;
                        }
                    }
                    else {
                        //cout << "i = " << i << "[" << code[i] << "]"<< "\n";
                        //cout << "j = " << j << "[" << code[j] << "]"<< "\n";
                        return 0;
                    }                    
                }
            }
            else j--;
        }
        else i++;
    }
    return 1;
}