# 1-bit and 2-bit Characters in Python

PythonServer Side ProgrammingProgramming

Suppose we have two special characters. The first character can be represented by one bit 0. And the second character can be represented by two bits like (10 or 11). So, if we have a string represented by several bits. We have to check whether the last character must be a one-bit character or not. The given string will always end with a zero.

So, if the input is like [1,0,0], then the output will be True, as the only way to decode it is twobit character (10) and one-bit character (0). So, the last character is one-bit character.

To solve this, we will follow these steps −

• while size of bits > 1, do
• current := first element of bits, then delete first element from bits
• if current is same as 1, then
• delete first element from bits
• if size of bits is same as 0, then
• return False
• return true when bits[0] is same as 0, otherwise false

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def isOneBitCharacter(self, bits):
while len(bits) > 1:
current = bits.pop(0)
if current == 1:
bits.pop(0)
if len(bits) == 0:
return False
return bits[0] == 0
ob = Solution()
print(ob.isOneBitCharacter([1,0,0]))

## Input

[1,0,0]

## Output

True
Published on 04-Jul-2020 09:51:34