We have two special characters:
0
.10
or 11
).Given a binary array bits
that ends with 0
, return true
if the last character must be 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 is 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 not one-bit character.
Constraints:
1 <= bits.length <= 1000
bits[i]
is either 0
or 1
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem is like trying every single possible way to decode a secret message. We explore each path, one at a time, to see if it leads us to the correct conclusion.
Here's how the algorithm would work step-by-step:
def is_last_character_valid(bits):
number_of_bits = len(bits)
def solve(current_index):
# Base case: Reached the end of the array
if current_index == number_of_bits:
return True
# If we've gone past the end, it's an invalid path
if current_index > number_of_bits:
return False
# Try one-bit character
if bits[current_index] == 0:
if solve(current_index + 1):
return True
# Try two-bit character
if current_index + 1 < number_of_bits:
# Need to check bounds before accessing bits[current_index + 1]
if bits[current_index] == 1:
if solve(current_index + 2):
return True
return False
# Initiate recursion to explore all combinations
return solve(0)
The key is to walk through the given sequence of bits and determine the last character type directly. Since we only care about the last character, we can skip ahead when we encounter a two-bit character to efficiently determine if the last character is a one-bit character.
Here's how the algorithm would work step-by-step:
def is_last_character_one_bit_character(bits):
current_index = 0
while current_index < len(bits):
# If the current bit is 1, it's a two-bit character.
if bits[current_index] == 1:
current_index += 2
# Otherwise it is a one-bit character.
else:
current_index += 1
# Checks if it ends on the last index.
return current_index == len(bits)
Case | How to Handle |
---|---|
Null or empty input array | Return false if the array is null or empty as there's no character to analyze. |
Array with only one element: [0] or [1] | If the single element is 0, return true; if it's 1, return false because 1 must be the start of a 2-bit character but has no following bit. |
Array ending with [1] | If the array ends with a single '1', it indicates an incomplete 2-bit character, making the last complete character not a 1-bit character, so return false. |
Array ending with [1,0] or [1,1] | These are valid 2-bit characters followed by no more characters, indicating the last character is not a 1-bit character so return false. |
Array of all zeros | The last character is always a 1-bit character, so return true. |
Array starting with a long sequence of ones followed by a zero | Iterate through the array, skipping two positions for each '1' encountered, and return true if the final index reached is the second to last element. |
Large input array close to memory limits | The iterative solution uses constant space, so it scales efficiently with large inputs. |
Array consisting of repeating patterns like [1,0,1,0,1,0] | The standard iteration will correctly handle these patterns by advancing either one or two steps at a time. |