LeetCode Challenge Day 64 β 717. 1-bit and 2-bit Characters
Nitin Ahirwal / November 18, 2025
Hey folks π
This is Day 64 of my LeetCode streak π
Todayβs problem is 717 β 1-bit and 2-bit Characters.
A neat binary decoding problem:
We must determine whether the final 0 in the array is a single-bit character or part of a two-bit character.
π‘ Intuition
Characters are encoded as:
-
0β one-bit character -
10or11β two-bit character
So, the approach is to simulate decoding left-to-right:
-
If you see a
1, skip the next bit (because it's a two-bit character) -
If you see a
0, move one step
If after processing all characters, you land exactly at the last index, that last 0 is a single-bit character.
If you jump past it, then it was consumed as part of a two-bit character.
π Approach
-
Initialize
i = 0. -
Traverse the array while
i < bits.length - 1:-
If
bits[i] === 1, movei += 2. -
Else move
i += 1.
-
-
At the end, return whether
i === bits.length - 1.
A clean linear scan solves it.
π Complexity
-
Time Complexity:
O(n)β one pass through the array -
Space Complexity:
O(1)β no extra storage
π§βπ» Code (JavaScript)
/**
* @param {number[]} bits
* @return {boolean}
*/
var isOneBitCharacter = function(bits) {
let i = 0;
while (i < bits.length - 1) {
if (bits[i] === 1) {
i += 2;
} else {
i += 1;
}
}
return i === bits.length - 1;
};
π― Example
Input:
bits = [1,0,0]
Output:
true
The decoding goes: 10 (a two-bit char) β 0 (a one-bit char).
We land on the last index β itβs a standalone one-bit character.
π§ Reflection
This problem reinforces how simple simulation is often the most effective solution.
No need for dynamic programming or complex logic β just follow the encoding rules.
Key learnings:
-
Understand the encoding scheme
-
Track only what matters: your current index
-
Avoid unnecessary extra steps or structures
See you tomorrow for Day 65! π
Happy Coding π¨βπ»β¨