Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created November 18, 2025 22:17
Show Gist options
  • Select an option

  • Save Ifihan/2a36cd01624feae8e70442ec24f6c2f4 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/2a36cd01624feae8e70442ec24f6c2f4 to your computer and use it in GitHub Desktop.
1-bit and 2-bit Characters

Question

Approach

I scan the array from left to right. • If I see a 1, it must start a two-bit character, so I skip the next index (i += 2). • If I see a 0, it is a one-bit character, so I move one step (i += 1). At the end, if the pointer stops exactly at the last index, then the last character is a one-bit character.

Implementation

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

Complexities

  • Time: O(n)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment