LeetCode-in-Java

4030. Check ASCII Palindromic

Easy

You are given a string s consisting of lowercase English letters.

Construct a binary string by replacing each character in s with the 8-bit binary representation of its ASCII value, including leading zeros, while preserving the original order of the characters.

Return true if the resulting binary string is a palindrome. Otherwise, return false.

Example 1:

Input: s = “ff”

Output: true

Explanation:

Example 2:

Input: s = “leet”

Output: false

Explanation:

Constraints:

Solution

public class Solution {
    public boolean isPalindromic(String s) {
        int n = s.length();
        for (int i = 0; i <= n / 2; i++) {
            int lptr = s.charAt(i);
            int rptr = s.charAt(n - i - 1);
            for (int j = 0; j < 8; j++) {
                int left = (lptr >> j) & 1;
                int right = (rptr >> (7 - j)) & 1;
                if (left != right) {
                    return false;
                }
            }
        }
        return true;
    }
}