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 −
Let us see the following implementation to get better understanding −
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 ob = Solution() print(ob.isOneBitCharacter([1,0,0]))