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 (10 or 11)

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
1-bit and 2-bit Characters ProblemEncoding Rules:0 → one-bit char10 → two-bit char11 → two-bit charExample: [1,0,0]100"10" = two-bit0one-bit charParse: "10" + "0" → last 0 is reachableResult: true
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
Asked in
Google 15 Facebook 12
28.1K Views
Medium Frequency
~15 min Avg. Time
892 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