1-bit and 2-bit Characters

We have two special characters: the first character can be represented by one bit 0, and 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.

30:00

1-bit and 2-bit Characters
easy
Free written solution
Topics
Companies

We have two special characters: the first character can be represented by one bit 0, and 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.

Example 1:
Input: {"bits":[1,0,0]}
Output: true
Constraints:
  • 1≤bits.length≤10001 \leq \text{bits.length} \leq 1000

  • bits[i]\text{bits}[i] is either 0 or 1.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"bits":[1,0,0]}

Start decoding at i = 0. A 0 is a whole character; a 1 always starts a two-bit character, so it swallows the next bit.

bit stream: teal = one-bit char "0", purple = two-bit char "1x"

i▼

1
0
0

0

1

2

characters decoded

0

last 0 stands alone?

?

Variables
VariableValue
i0
n3
DepthFunction Call
Stack empty
0/4