UTF-8 Validation
Solution Code
class Solution:
def validUtf8(self, data: list[int]) -> bool:
n_bytes = 0
for num in data:
# Get the least significant 8 bits
bin_rep = format(num, '#010b')[-8:]
if n_bytes == 0:
# Count leading 1s
for bit in bin_rep:
if bit == '0':
break
n_bytes += 1
if n_bytes == 0:
continue
if n_bytes == 1 or n_bytes > 4:
return False
else:
# Check continuation signature
if not (bin_rep[0] == '1' and bin_rep[1] == '0'):
return False
n_bytes -= 1
return n_bytes == 0
