1-bit and 2-bit Characters - Problem
We have two special characters:
- The first character can be represented by one bit
0 - The second character can be represented by two bits (
10or11)
Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.
Note: The array is guaranteed to end with 0.
Input & Output
Example 1 — Two-bit then One-bit
$
Input:
bits = [1,0,0]
›
Output:
true
💡 Note:
The first character is a two-bit character "10", and the second character is a one-bit character "0". So the last character is a one-bit character.
Example 2 — Two consecutive 1's
$
Input:
bits = [1,1,0]
›
Output:
false
💡 Note:
The only way to decode this is as one two-bit character "11" followed by "0". However, this means the last 0 cannot be reached as a separate one-bit character through proper parsing.
Example 3 — Single character
$
Input:
bits = [0]
›
Output:
true
💡 Note:
The array contains only one bit "0", which is a one-bit character.
Constraints
- 1 ≤ bits.length ≤ 1000
- bits[i] is either 0 or 1
- bits[bits.length - 1] == 0
Visualization
Tap to expand
Understanding the Visualization
1
Input
Binary array ending with 0
2
Parse
Follow encoding: 0→1 step, 1→2 steps
3
Check
Land exactly on last 0?
Key Takeaway
🎯 Key Insight: Parse from start following encoding rules - if we land exactly on the last 0, it's a one-bit character
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code