1-bit and 2-bit Characters (LeetCode 717): Solution and Explanation
Read the bits like a decoder: a 0 is one character, a 1 eats the next bit too. Jump through the array and return whether the pointer stops exactly on the final 0.
Intuition
A 0 can only be the one-bit character, because every two-bit character starts with 1. A 1 can only be the start of a two-bit character. So reading from the left never needs a guess.
That turns the problem into a walk: from index i, the next character starts at i + 1 if bits[i] is 0, or at i + 2 if bits[i] is 1.
The final 0 is its own character exactly when some character starts on it, which means the walk lands on index n - 1 instead of jumping over it.
Approach: Jumping decoder pointer
- Set i = 0.
- While i < n - 1, add bits[i] + 1 to i.
- Return i == n - 1.
Step-by-step example
bits = [1,0,0]
- i = 0, n - 1 = 2.
- bits[0] = 1 starts the two-bit character 10, so i jumps to 2.
- i = 2 is not less than 2, so the loop ends.
- i equals n - 1, so the final 0 is a one-bit character. Return true.
Code: Python, Java, C++
1class Solution:
2 def isOneBitCharacter(self, bits: List[int]) -> bool:
3 i = 0
4 n = len(bits)
5 while i < n - 1:
6 i += bits[i] + 1
7 return i == n - 11class Solution {
2 public boolean isOneBitCharacter(int[] bits) {
3 int i = 0;
4 int n = bits.length;
5 while (i < n - 1) {
6 i += bits[i] + 1;
7 }
8 return i == n - 1;
9 }
10}1class Solution {
2public:
3 bool isOneBitCharacter(vector<int>& bits) {
4 int i = 0;
5 int n = bits.size();
6 while (i < n - 1) {
7 i += bits[i] + 1;
8 }
9 return i == n - 1;
10 }
11};Time and space complexity
O(n)
Each jump moves the pointer forward by 1 or 2, so there are at most n jumps.
O(1)
Only the pointer is stored.
Other approaches
Count the ones before the last zero (time O(n), space O(1))
Count the run of 1s directly before the final 0, stopping at the previous 0 or the start. A 0 always ends a character, so decoding restarts cleanly after it. If the run length is even, the 1s pair up among themselves and the final 0 stands alone; if odd, the last 1 pairs with the final 0.
Common mistakes
- Looping while i < n instead of i < n - 1. Then the final 0 is always read, and you cannot tell whether it was reached on its own.
- Looking only at bits[n - 2]. In [1,1,0] the bit before the last is 1, yet that 1 is the second half of 11, so the answer is true.
- Trying to decode from the right. The meaning of a bit depends on what came before it, so the scan has to start from the left, or use the parity trick.
Frequently asked questions
Why is there only one way to decode the bits?
The codes 0, 10 and 11 form a prefix code: no code is the start of another. A 0 at the start of a character must be the one-bit code, and a 1 must start a two-bit code, so each step is forced.
Why does the parity of the trailing ones work?
Any 0 before the final run of ones closes a character, so decoding of that run starts fresh. The ones then pair up two at a time. An even count leaves the final 0 alone, an odd count leaves one 1 that must pair with it.
What if bits has length 1?
It is just [0]. The loop never runs, i = 0 = n - 1, and the answer is true.