Check ASCII Palindromic - Solution & Explanation
Practice this problemProblem 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
fis 102, whose 8-bit binary representation is01100110. - 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, andtare 108, 101, 101, and 116, respectively. - Their 8-bit binary representations are
01101100,01100101,01100101, and01110100. - Thus, the binary string is
01101100011001010110010101110100. - Since this binary string is not a palindrome, the output is
false.
Constraints:
1 <= s.length <= 100sconsists 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.nis small enough that we need not rewrite the test in terms of character pairs.Build
tin 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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Build Reversed Sequence and Compare | O(n) | O(n) | Quick prototype, memory is not constrained |
| Two Pointers from Both Ends | O(n) | O(1) | General case, interviews, memory-constrained systems |
Frequently Asked Questions
Is Check ASCII Palindromic easy or hard?
How to solve Check ASCII Palindromic in O(n)?
Check ASCII Palindromic Python and Java solution?
What is the best approach for Check ASCII Palindromic?
What data structure is used in Check ASCII Palindromic?
What is the time complexity of Check ASCII Palindromic?
Is Check ASCII Palindromic asked at Google, Amazon, or Meta?
Ready to solve this problem?
Practice Check ASCII Palindromic with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor