Problem statement

https://leetcode.com/problems/1-bit-and-2-bit-characters/

Solution

Just iterate our array from the beginning: if we have zero bit, we move 1 position to the right, if it is equal to one, we move two positions to the right. In the end we check if it is equal to n-1 or not.

Complexity

Time complexity is O(n), space complexity is O(1).

Code

class Solution:
    def isOneBitCharacter(self, bits):
        i, n = 0, len(bits)
        while i < n - 1:
            if bits[i] == 1: i += 2
            else: i += 1
        return i == n - 1