Similar Problems

Similar Problems not available

1 Bit And 2 Bit Characters - Leetcode Solution

Companies:

LeetCode:  1 Bit And 2 Bit Characters Leetcode Solution

Difficulty: Easy

Topics: array  

Problem Statement:

We have a sequence of bits, where each bit is either 0 or 1. We can represent this sequence as a binary string bit_str.

Return true if the last bit in the sequence is 0.

Also, given that the character represented by 1 bit is either '0' or '1' and the character represented by 2 bits is either '10', '11', or '12', return true if and only if the last character in the given string is 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 must be a one-bit character.

Example 2:

Input: bits = [1,1,1,0] Output: false Explanation: The only way to decode it is two-bit character and two-bit character. So the last character is the second last element which is not a one-bit character.

Approach:

We can start by iterating through the array of bits and parsing them one by one. We can maintain a boolean value to tell us whether to parse the current bit as a one-bit or a two-bit character. Once we parse a one-bit character, we can simply move on to the next bit as the last character must be a one-bit character. On the other hand, if we parse a two-bit character, we must skip the next bit, as the last character can't be a two-bit character.

Solution:

We will use a loop to parse the bits one by one. We will maintain a boolean variable named "is_two_bit" to keep track of whether the current bit should be parsed as a one-bit or a two-bit character. We will set this variable to false initially.

We will then iterate over the bits, parsing each bit one by one. If the current bit is 0, and there are no more bits left to parse, then we return true, as the last bit in the sequence is 0.

If the current bit is 0, and there are still more bits to parse, we simply skip to the next bit.

If the current bit is 1, and is_two_bit is false, we parse it as a one-bit character, and move on to the next bit. If the current bit is 1, and is_two_bit is true, we parse it as a two-bit character, and skip the next bit.

At the end of the loop, if we have successfully parsed all bits without encountering any errors, we return true, as the last character in the sequence is a one-bit character.

Python Implementation:

Here's the python implementation of the above approach.

def isOneBitCharacter(bits): is_two_bit = False # Setting to False initially i = 0 # Initializing index to 0

while i < len(bits):
    if i == len(bits) - 1: # If last element is reached
        return True

    if bits[i] == 0: # Current bit is 0
        i += 1

    else: # Current bit is 1
        is_two_bit = not is_two_bit # Inverting the boolean variable

        if is_two_bit:
            i += 2
        else:
            i += 1

return False

Overall Complexity: O(N), where N is the length of the bits list.

1 Bit And 2 Bit Characters Solution Code

1