Problem Statement
When transmitting data over a network, the data needs to be serialized into a
sequence of bits - zeros and ones. Consider the task of serializing a sequence
of integers. The simplest way to do this is to transmit a fixed number of bits
per integer. In this problem, however, we will consider another method. First,
we will choose some sequence of bits, called s.
Next, we will convert each integer into a binary string with a length that is a positive multiple of the length of s (padding the front with zeros if neccessary). If s is a prefix of any of the binary strings, then our choice of s is invalid (as it may lead to an ambiguous decoding), and we can not use that s.
Thus, each integer can be considered as a number of groups of binary digits, where each group has a number of bits equal to the length of s. To transmit the data, we will transmit the groups of the first number, in order from left to right (most significant to least significant). If any of the groups are exactly equal to s, we will transmit those groups twice. After the entire first number is transmitted in this way, we will transmit s once, and then proceed with the next number in the same way. We do not need to transmit s after the last number is sent. For example, consider transmitting the sequence of numbers {1,3,6,9} with s = "01". First, we convert each number to binary, and pad the left with zeros so that the lengths are each divisible by 2:
{01,11,0110,1001}
Now, when we transmit the data, we have to transmit each group that is equal to s twice:
You will be given a sequence of bits generated as described, which can be found by concatenating all of the elements of bits together, in order. You will also be given a String, s, which is the s described above. Your task is to decode the sequence of bits, and return the resulting sequence. If the sequence generates ints that are greater than or equal to 231, or if the sequence is not encoded as described above, return an empty int[].
Thus, each integer can be considered as a number of groups of binary digits, where each group has a number of bits equal to the length of s. To transmit the data, we will transmit the groups of the first number, in order from left to right (most significant to least significant). If any of the groups are exactly equal to s, we will transmit those groups twice. After the entire first number is transmitted in this way, we will transmit s once, and then proceed with the next number in the same way. We do not need to transmit s after the last number is sent. For example, consider transmitting the sequence of numbers {1,3,6,9} with s = "01". First, we convert each number to binary, and pad the left with zeros so that the lengths are each divisible by 2:
{01,11,0110,1001}
Now, when we transmit the data, we have to transmit each group that is equal to s twice:
Number : 1 | | 3 | | 6 | | 9 Bits sent : 01 01 | 01 | 11 | 01 | 01 01 10 | 01 | 10 01 01One way to think about this is that s indicates that a new number is about to start, unless it occurs twice in a row.
You will be given a sequence of bits generated as described, which can be found by concatenating all of the elements of bits together, in order. You will also be given a String, s, which is the s described above. Your task is to decode the sequence of bits, and return the resulting sequence. If the sequence generates ints that are greater than or equal to 231, or if the sequence is not encoded as described above, return an empty int[].
Definition
- Class:
- SequenceDecoder
- Method:
- decode
- Parameters:
- String[], String
- Returns:
- int[]
- Method signature:
- int[] decode(String[] bits, String s)
- (be sure your method is public)
Constraints
- bits will contain between 1 and 50 elements, inclusive.
- Each element of bits will contain between 1 and 50 characters, inclusive.
- s will contain between 1 and 50 characters, inclusive.
- Each character in s and bits will be a '0' or a '1'.
Examples
{"11101101111"}"0"
Returns: { 7, 3, 15 }The sequence {7,3,15} is {111,11,1111} in binary. Inserting the zeros gives us "11101101111".{"111011011110"}"0"
Returns: { }There is no sequence that is encoded like this because of the trailing 0.{"1110110111100"}"0"
Returns: { 7, 3, 30 }30 in binary is 11110. Since the 0 gets transmitted twice (as s = 0), 30 is transmitted as 111100.{"1"}"00000000000000000000000000000000000000000000000000"
Returns: { }{"0011"}"00"
Returns: { }{"00000000000000000000000000000000000000000000000000", "00000000000000000000000000000000000000000000000000", "00000000000000000000000000000000000000000000000000", "00000000000000000000000000000000000000000000000001"}"00000000000000000000000000000000000000000000000000"
Returns: { 0, 1 }{"10000000000000000000000000000000000000000000000000"}"00000000000000000000000000000000000000000000000000"
Returns: { }{"00000"}"1"
Returns: { }Note that {0} would be encoded as "0", not "00000".{"0100000000000000000000000000000000","00000000000000000000000000000000"}"11"
Returns: { }