Back to posts

LeetCode Challenge Day 64 β€” 717. 1-bit and 2-bit Characters

Nitin Ahirwal / November 18, 2025

LeetCode ChallengeDay 64BinarySimulationJavaScriptEasy

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

  • 10 or 11 β†’ 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

  1. Initialize i = 0.

  2. Traverse the array while i < bits.length - 1:

    • If bits[i] === 1, move i += 2.

    • Else move i += 1.

  3. 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 πŸ‘¨β€πŸ’»βœ¨