Determine whether the last character in a bit array must be a 1-bit character given a valid encoding scheme.
1 Bit and 2 Bit Characters
You are given an array of bits representing a valid encoding of characters. The encoding uses two kinds of characters:
0represents a 1-bit character.10or11represents a 2-bit character.
The array is guaranteed to be a valid encoding, and the last character in the sequence is always either a 1-bit character or part of a 2-bit character.
Your task is to determine whether the last character is a 1-bit character.
In other words, return true if the final 0 stands alone as a 1-bit character, and false if it is consumed as the second bit of a 2-bit character.
Input Format
- A binary array
bitsof lengthn. bits[i]is either0or1.- The array represents a valid encoding using the rules above.
Output Format
- Return
trueif the last character is a 1-bit character. - Otherwise, return
false.
Constraints
1 <= n <= 1000bits[i]is0or1- The encoding is valid
- The array ends with either a standalone
0or the second bit of a 2-bit character
Example 1
Input
bits = [1, 0, 0]
Output
true
Explanation
The first two bits form 10, which is one 2-bit character. The final 0 is a standalone 1-bit character.
Example 2
Input
bits = [1, 1, 1, 0]
Output
false
Explanation
The bits can be parsed as 11 and 10, so the final 0 is part of a 2-bit character.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.