2
def validUtf8(self, data: List[int]) -> bool:3
# Keep track of how many continuation bytes are left4
# Start at 0 since we are not expecting any continuation bytes at the beginning.7
if cont_bytes_left == 0:8
# If we don't expect any continuation bytes9
# then there are 4 valid case for the current byte10
# byte >> 5 gives us the first 3 bits (8 bits - 5 = 3).11
if byte >> 5 == 0b110:12
# After seeing a byte that starts with 110,13
# we expect to see one continuation byte15
elif byte >> 4 == 0b1110:17
elif byte >> 3 == 0b11110:19
# finally if the first bit isn't 0 then it's invalid23
# If we are expecting a continuation byte there is only one valid case.24
# It's invalid if the continuation byte doesn't start with 1029
# Only valid if we aren't expecting any more continuation bytes30
return cont_bytes_left == 0