Skip to main content

Check ASCII Palindromic - Solution & Explanation

Practice this problem

Problem Statement

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:

  • The ASCII value of f is 102, whose 8-bit binary representation is 01100110.
  • Thus, the binary string is 0110011001100110.
  • Since this binary string is a palindrome, the output is true.

Example 2:

Input: s = "leet"

Output: false

Explanation:

  • The ASCII values of l, e, e, and t are 108, 101, 101, and 116, respectively.
  • Their 8-bit binary representations are 01101100, 01100101, 01100101, and 01110100.
  • Thus, the binary string is 01101100011001010110010101110100.
  • Since this binary string is not a palindrome, the output is false.

 

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters.

Approach Overview

Problem Overview: You get a string and must decide whether its character codes read the same forwards and backwards. Compare the ASCII value at each position with the value at the mirrored position. If every pair matches, return true.

Approach 1: Build Reversed Sequence and Compare (O(n) time, O(n) space)

Convert the string into a list of ASCII codes using ord() or charCodeAt(). Reverse that list, then compare the two sequences element by element. This is the easiest approach to write and verify. It allocates a second array of size n, so it uses extra memory. Use it when you want a quick correct answer and memory is not a concern.

Approach 2: Two Pointers from Both Ends (O(n) time, O(1) space)

Place left at index 0 and right at index n-1. At each step, read the ASCII codes at both pointers. If they differ, return false immediately. Otherwise, move left forward and right backward until they meet. The key insight is that you only need to check n/2 pairs, and each comparison is a constant-time integer check, so you never build a transformed copy of the input. This is the approach to use in production code and in interviews where memory matters.

Recommended for interviews: Start with the reversed-sequence version to show you understand the palindrome definition. Then move to two pointers to cut space to O(1) and explain why early termination on the first mismatch keeps the worst case at O(n). The brute force shows understanding, but the two-pointer version shows you can optimize a string problem cleanly. Mention that the same pattern extends to bit manipulation problems where you compare character codes directly without converting types.

Solution

Thinking

Each character expands to a fixed 8-bit string, including leading zeros, and we test whether the concatenation is a palindrome. n is small enough that we need not rewrite the test in terms of character pairs.

Build t in order and compare it with its reverse.

Following the problem statement, we replace each character of s with the 8-bit binary representation of its ASCII value (including leading zeros), concatenate them in order to obtain a binary string t, and then check whether t is a palindrome.

The time complexity is O(n) and the space complexity is O(n), where n is the length of s.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Build Reversed Sequence and CompareO(n)O(n)Quick prototype, memory is not constrained
Two Pointers from Both EndsO(n)O(1)General case, interviews, memory-constrained systems

Frequently Asked Questions

Is Check ASCII Palindromic easy or hard?
It is an easy problem with a 60.2 percent acceptance rate on FleetCode. The core idea is the same as checking a standard palindrome, and the only extra step is comparing character codes instead of characters. Most candidates can solve it in a few minutes once they know the two-pointer pattern.
How to solve Check ASCII Palindromic in O(n)?
Use two pointers. Set one pointer at the start and one at the end, compare <code>ord(s[left])</code> with <code>ord(s[right])</code>, and move both inward while they match. If they never differ before the pointers meet, the answer is true. This avoids building any extra array, so the space stays at O(1).
Check ASCII Palindromic Python and Java solution?
In Python, use two indices with <code>ord(s[i])</code> to compare codes while moving inward from both ends. In Java, use <code>int</code> indices with <code>s.charAt(i)</code>, which returns a char that widens to its ASCII integer value on comparison. Both versions run in O(n) time and O(1) space.
What is the best approach for Check ASCII Palindromic?
The best approach is two pointers from both ends. Compare the ASCII codes at the left and right indices, move inward on each match, and return false on the first mismatch. This runs in O(n) time and uses O(1) extra space.
What data structure is used in Check ASCII Palindromic?
The problem uses a string as input and relies on integer character codes from ASCII. The optimal solution uses two integer indices as pointers and needs no auxiliary data structure. The simpler solution uses a temporary array or list to hold the reversed sequence.
What is the time complexity of Check ASCII Palindromic?
The time complexity is O(n), where n is the length of the string, because each character is visited at most once. The two-pointer version checks about n/2 pairs. The reversed-sequence version also runs in O(n) but needs O(n) extra space.
Is Check ASCII Palindromic asked at Google, Amazon, or Meta?
FleetCode's tags for this problem are Two Pointers, String, and Bit Manipulation, but the platform does not list company-specific interview data for it. Two-pointer string problems like this appear often in screening rounds at large tech companies, so practice the O(1) space version.

Ready to solve this problem?

Practice Check ASCII Palindromic with our built-in code editor and test cases.

Practice on FleetCode