Skip to main content
Back to problems
Leetcode
Easy
Arrays
Bit Manipulation
Simulation
1 Bit and 2 Bit Characters

Determine whether the last character in a bit array must be a 1-bit character given a valid encoding scheme.

Acceptance 0%
Problem Statement

1 Bit and 2 Bit Characters

You are given an array of bits representing a valid encoding of characters. The encoding uses two kinds of characters:

  • 0 represents a 1-bit character.
  • 10 or 11 represents a 2-bit character.

The array is guaranteed to be a valid encoding, and the last character in the sequence is always either a 1-bit character or part of a 2-bit character.

Your task is to determine whether the last character is a 1-bit character.

In other words, return true if the final 0 stands alone as a 1-bit character, and false if it is consumed as the second bit of a 2-bit character.

Input Format

  • A binary array bits of length n.
  • bits[i] is either 0 or 1.
  • The array represents a valid encoding using the rules above.

Output Format

  • Return true if the last character is a 1-bit character.
  • Otherwise, return false.

Constraints

  • 1 <= n <= 1000
  • bits[i] is 0 or 1
  • The encoding is valid
  • The array ends with either a standalone 0 or the second bit of a 2-bit character
Examples
Sample cases returned by the problem API.

Example 1

Input

bits = [1, 0, 0]

Output

true

Explanation

The first two bits form 10, which is one 2-bit character. The final 0 is a standalone 1-bit character.

Example 2

Input

bits = [1, 1, 1, 0]

Output

false

Explanation

The bits can be parsed as 11 and 10, so the final 0 is part of a 2-bit character.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.