- Related Questions & Answers
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- Difference between 32-bit and 64-bit operating systems
- How to display the bit(1) fields in MySQL?
- Compile 32-bit program on 64-bit gcc in C and C++
- Program to find number of bit 1 in the given number in Python
- How to compile 32-bit program on 64- bit gcc in C and C++
- C# Bitwise and Bit Shift Operators
- 4-bit microprocessors
- 8-bit microprocessors
- 16-bit microprocessors
- 32-bit microprocessors
- Bit-Map Protocol
- Bit Fields in C
- Difference between Byte stuffing and Bit stuffing

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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]))

[1,0,0]

True

Advertisements