1-bit and 2-bit Characters - Problem
1-bit and 2-bit Characters

Imagine you're decoding a binary message that uses a special encoding scheme. In this system, there are only two types of characters:

โ€ข 0 represents a 1-bit character
โ€ข 10 or 11 represent 2-bit characters

Given a binary array bits that always ends with 0, your task is to determine whether the last character in the decoded message must be the 1-bit character (represented by 0).

The Challenge: Since the array always ends with 0, this final 0 could either be:
1. A standalone 1-bit character, OR
2. The second bit of a 2-bit character that started with 1

Return true if the last character must be a 1-bit character, false otherwise.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 0, 0]
โ€บ Output: true
๐Ÿ’ก Note: Parsing from left: '1' must start a 2-bit character, so we have '10' + '0'. The last character is the 1-bit character '0'.
example_2.py โ€” Two-bit Ending
$ Input: [1, 1, 1, 0]
โ€บ Output: false
๐Ÿ’ก Note: Parsing from left: '11' (2-bit) + '10' (2-bit). The final '0' is actually the second bit of the last 2-bit character '10', not a standalone 1-bit character.
example_3.py โ€” Single Character
$ Input: [0]
โ€บ Output: true
๐Ÿ’ก Note: The array contains only one bit '0', which can only represent a 1-bit character.

Visualization

Tap to expand
Binary Character Decoding ExamplesExample 1: [1, 0, 0]100โ†’ Last is 1-bit โœ“Example 2: [1, 1, 1, 0]1110โ†’ Last is 2-bit โœ—Parsing Process for [1, 0, 1, 1, 0]:Step 1: See '1' at index 0 โ†’ Must be 2-bit char โ†’ Read '10'Step 2: See '1' at index 2 โ†’ Must be 2-bit char โ†’ Read '11'Step 3: Reach index 4 (last bit) โ†’ '0' is standalone 1-bit charResult: true (last character is 1-bit)Key Insight Visualization๐Ÿ’ก Greedy parsing works because:โ€ข '1' can ONLY start a 2-bit character (never standalone)โ€ข '0' can be either standalone OR second bit of 2-bit char
Understanding the Visualization
1
Identify the encoding rules
0 = 1-bit character, 10 or 11 = 2-bit characters
2
Parse from left to right
When you see '1', you must read the next bit too (2-bit char). When you see '0' at start, it's a complete 1-bit character
3
Track your position
Keep track of where you are after parsing each complete character
4
Check the final position
If you end exactly at the last bit, it's a 1-bit character. If you overshoot, the last '0' was part of a 2-bit character
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because binary character parsing has a deterministic left-to-right structure - there's exactly one valid way to decode any given sequence.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n)

In worst case, we explore 2 possibilities at each position, leading to exponential branching

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion stack depth can go up to n levels in worst case

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค bits.length โ‰ค 1000
  • bits[i] is either 0 or 1
  • The array always ends with 0
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen