Advertisement

1-bit and 2-bit Characters - LeetCode 717 Solution

1-bit and 2-bit Characters - Complete Solution Guide

1-bit and 2-bit Characters is LeetCode problem 717, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

We have two special characters: The first character can be represented by one bit 0 . The second character can be represented by two bits ( 10 or 11 ). Given a binary array bits that ends with 0 , return true if the last character must be a one-bit character. Example 1: Input: bits = [1,0,0] Output: true Explanation: The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character. Example 2: Input: bits = [1,1,1,0] Output: false Explanation: The o

Detailed Explanation

The problem asks us to determine if the last character in a given binary array `bits` is a one-bit character (represented by '0'). The array `bits` contains characters that can be either one-bit ('0') or two-bit ('10' or '11'). The array is guaranteed to end with '0'. The task is to return `true` if the very last character is a one-bit character and `false` otherwise. For instance, if `bits = [1, 0, 0]`, it can be decoded as a two-bit character ('10') followed by a one-bit character ('0'). Thus, the answer should be `true`. If `bits = [1, 1, 1, 0]`, it's decoded as two two-bit characters ('11' and '10'), so the last character is part of a two-bit character, meaning the answer is `false`.

Solution Approach

The provided Python, C, and Java solutions implement two different but correct approaches. The Python/C solution iterates from the beginning of the array, simulating the decoding process. The Java and C++ solutions iterate backward from the second to last element to count the number of consecutive 1s.

Step-by-Step Algorithm

  1. Step 1 (Python/C): Initialize an index `i` to 0.
  2. Step 2 (Python/C): Iterate through the array while `i` is less than `len(bits) - 1`.
  3. Step 3 (Python/C): If `bits[i]` is 1 (a two-bit character), increment `i` by 2.
  4. Step 4 (Python/C): If `bits[i]` is 0 (a one-bit character), increment `i` by 1.
  5. Step 5 (Python/C): After the loop, return `true` if `i` is equal to `len(bits) - 1`, indicating that the last character is a one-bit character. Otherwise, return `false`.
  6. Step 1 (Java/C++): Start from the second to last element (index = `bits.length - 2`).
  7. Step 2 (Java/C++): Iterate backwards while the current element is 1 and the index is greater than or equal to 0.
  8. Step 3 (Java/C++): Count how many consecutive 1s are before the last 0. The last bit is a one-bit character if the count is even.

Key Insights

  • Insight 1: The core idea is to simulate the decoding process from the beginning or the end of the array.
  • Insight 2: We can iterate through the array, incrementing the index by 1 if we encounter '0' (one-bit character) and by 2 if we encounter '1' (two-bit character).
  • Insight 3: Alternatively, we can iterate from the second to last element backwards. If we encounter multiple consecutive 1s before the last 0, an odd number means the last zero is part of 2-bit character and hence can not be a one-bit character. Even number means it is a one-bit character.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array.

Companies

Asked at: IXL, Quora.