KatInterview

717. 1-bit and 2-bit Characters

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to decode a sequence of bits according to specific rules and determine if the very last character decoded must be a one-bit character. The rules for decoding are:

Solution

1-bit and 2-bit Characters in Python

class Solution:
    def isOneBitCharacter(self, bits: List[int]) -> bool:
        i = 0
        n = len(bits)
        # We must handle the last bit separately, so we iterate until i < n - 1.
        # The loop simulates decoding the bit string.
        while i < n - 1:
            # If the current bit is 1, it must be a two-bit character (10 or 11).
            # So we skip the next bit as well by advancing the pointer by 2.
            if bits[i] == 1:
                i += 2
            else:
                # If the current bit is 0, it's a one-bit character.
                # We advance the pointer by 1.
                i += 1
        
        # After the loop, the pointer `i` will tell us how the last part of the string is decoded.
        # If `i` is exactly at the last index (n - 1), it means the decoding process
        # ended perfectly on the last bit, which must then be a one-bit character.
        # If `i` is not at `n - 1` (it would be `n`), it means the last bit was part
        # of a two-bit character starting at `n-2`.
        return i == n - 1

1-bit and 2-bit Characters in Java

class Solution {
    public boolean isOneBitCharacter(int[] bits) {
        int i = 0;
        int n = bits.length;
        // We must handle the last bit separately, so we iterate until i < n - 1.
        // The loop simulates decoding the bit string.
        while (i < n - 1) {
            // If the current bit is 1, it must be a two-bit character (10 or 11).
            // So we skip the next bit as well by advancing the pointer by 2.
            if (bits[i] == 1) {
                i += 2;
            } else {
                // If the current bit is 0, it's a one-bit character.
                // We advance the pointer by 1.
                i += 1;
            }
        }
        // After the loop, the pointer `i` will tell us how the last part of the string is decoded.
        // If `i` is exactly at the last index (n - 1), it means the decoding process
        // ended perfectly on the last bit, which must then be a one-bit character.
        // If `i` is not at `n - 1` (it would be `n`), it means the last bit was part
        // of a two-bit character starting at `n-2`.
        return i == n - 1;
    }
}

1-bit and 2-bit Characters in C++

class Solution {
public:
    bool isOneBitCharacter(vector<int>& bits) {
        int i = 0;
        int n = bits.size();
        // We must handle the last bit separately, so we iterate until i < n - 1.
        // The loop simulates decoding the bit string.
        while (i < n - 1) {
            // If the current bit is 1, it must be a two-bit character (10 or 11).
            // So we skip the next bit as well by advancing the pointer by 2.
            if (bits[i] == 1) {
                i += 2;
            } else {
                // If the current bit is 0, it's a one-bit character.
                // We advance the pointer by 1.
                i += 1;
            }
        }
        // After the loop, the pointer `i` will tell us how the last part of the string is decoded.
        // If `i` is exactly at the last index (n - 1), it means the decoding process
        // ended perfectly on the last bit, which must then be a one-bit character.
        // If `i` is not at `n - 1` (it would be `n`), it means the last bit was part
        // of a two-bit character starting at `n-2`.
        return i == n - 1;
    }
};

1-bit and 2-bit Characters in TypeScript

function isOneBitCharacter(bits: number[]): boolean {
    let i = 0;
    const n = bits.length;
    // We must handle the last bit separately, so we iterate until i < n - 1.
    // The loop simulates decoding the bit string.
    while (i < n - 1) {
        // If the current bit is 1, it must be a two-bit character (10 or 11).
        // So we skip the next bit as well by advancing the pointer by 2.
        if (bits[i] === 1) {
            i += 2;
        } else {
            // If the current bit is 0, it's a one-bit character.
            // We advance the pointer by 1.
            i += 1;
        }
    }
    // After the loop, the pointer `i` will tell us how the last part of the string is decoded.
    // If `i` is exactly at the last index (n - 1), it means the decoding process
    // ended perfectly on the last bit, which must then be a one-bit character.
    // If `i` is not at `n - 1` (it would be `n`), it means the last bit was part
    // of a two-bit character starting at `n-2`.
    return i === n - 1;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.