393. UTF-8 Validation
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to validate if a given sequence of integers, where each integer represents a byte, constitutes a valid UTF-8 encoding. The core of the problem lies in understanding and correctly applying the bit-level rules for UTF-8 characters of different lengths (1 to 4 bytes).
Solution
UTF-8 Validation in Python
class Solution:
def validUtf8(self, data: List[int]) -> bool:
# Number of bytes in the current UTF-8 character that we are processing.
remaining_bytes = 0
# Iterate through each integer in the data array.
for byte in data:
# If this is the start of a new character.
if remaining_bytes == 0:
# Check how many bytes this character will have.
if (byte >> 5) == 0b110: # 2-byte character
remaining_bytes = 1
elif (byte >> 4) == 0b1110: # 3-byte character
remaining_bytes = 2
elif (byte >> 3) == 0b11110: # 4-byte character
remaining_bytes = 3
# If it's a 1-byte character, it must start with 0.
# If it starts with 1 but doesn't match any multi-byte start, it's invalid.
# This covers cases like 10xxxxxx or 11111xxx.
elif (byte >> 7) != 0:
return False
# If this is a continuation byte of a multi-byte character.
else:
# It must start with 10.
if (byte >> 6) != 0b10:
return False
# We have processed one continuation byte.
remaining_bytes -= 1
# After the loop, all characters must be complete.
# This checks for truncated multi-byte characters at the end of the data.
return remaining_bytes == 0
UTF-8 Validation in Java
class Solution {
public boolean validUtf8(int[] data) {
// This variable will store the number of continuation bytes we expect to see.
int remainingBytes = 0;
for (int val : data) {
// If remainingBytes is 0, we are at the beginning of a new character.
if (remainingBytes == 0) {
// Check the first few bits to determine the character length.
// For a 2-byte character, the first byte starts with 110.
if ((val >> 5) == 0b110) {
remainingBytes = 1;
}
// For a 3-byte character, the first byte starts with 1110.
else if ((val >> 4) == 0b1110) {
remainingBytes = 2;
}
// For a 4-byte character, the first byte starts with 11110.
else if ((val >> 3) == 0b11110) {
remainingBytes = 3;
}
// For a 1-byte character, the first bit must be 0.
// If the most significant bit is 1 but doesn't match a multi-byte start, it's invalid.
else if ((val >> 7) != 0) {
return false;
}
}
// If remainingBytes is not 0, this must be a continuation byte.
else {
// Continuation bytes must start with 10.
if ((val >> 6) != 0b10) {
return false;
}
// We've processed one continuation byte, so decrement the counter.
remainingBytes--;
}
}
// If we finished processing, there should be no remaining bytes expected.
return remainingBytes == 0;
}
}
UTF-8 Validation in C++
class Solution {
public:
bool validUtf8(vector<int>& data) {
// This variable will store the number of continuation bytes we expect to see.
int remaining_bytes = 0;
for (int byte : data) {
// If remaining_bytes is 0, we are at the beginning of a new character.
if (remaining_bytes == 0) {
// Check the first few bits to determine the character length.
// For a 2-byte character, the first byte starts with 110.
if ((byte >> 5) == 0b110) {
remaining_bytes = 1;
}
// For a 3-byte character, the first byte starts with 1110.
else if ((byte >> 4) == 0b1110) {
remaining_bytes = 2;
}
// For a 4-byte character, the first byte starts with 11110.
else if ((byte >> 3) == 0b11110) {
remaining_bytes = 3;
}
// For a 1-byte character, the first bit must be 0.
// If the most significant bit is 1, but it doesn't match any multi-byte
// start pattern (110, 1110, 11110), it's invalid. This also catches
// invalid start bytes like 10xxxxxx or 11111xxx.
else if ((byte >> 7) != 0) {
return false;
}
}
// If remaining_bytes is not 0, we are in the middle of a multi-byte character.
else {
// Continuation bytes must start with 10.
if ((byte >> 6) != 0b10) {
return false;
}
// We've processed one continuation byte, so decrement the counter.
remaining_bytes--;
}
}
// If the loop finishes, all characters must be complete.
// This is true only if remaining_bytes is 0.
// If it's non-zero, it means the data ended with a truncated character.
return remaining_bytes == 0;
}
};
UTF-8 Validation in TypeScript
function validUtf8(data: number[]): boolean {
// This variable will store the number of continuation bytes we expect to see.
let remainingBytes = 0;
for (const byte of data) {
// If remainingBytes is 0, we are at the beginning of a new character.
if (remainingBytes === 0) {
// Check the first few bits to determine the character length.
// For a 2-byte character, the first byte starts with 110.
if ((byte >> 5) === 0b110) {
remainingBytes = 1;
}
// For a 3-byte character, the first byte starts with 1110.
else if ((byte >> 4) === 0b1110) {
remainingBytes = 2;
}
// For a 4-byte character, the first byte starts with 11110.
else if ((byte >> 3) === 0b11110) {
remainingBytes = 3;
}
// For a 1-byte character, the first bit must be 0.
// If the most significant bit is 1 but doesn't match a multi-byte start, it's invalid.
else if ((byte >> 7) !== 0) {
return false;
}
}
// If remainingBytes is not 0, this must be a continuation byte.
else {
// Continuation bytes must start with 10.
if ((byte >> 6) !== 0b10) {
return false;
}
// We've processed one continuation byte, so decrement the counter.
remainingBytes--;
}
}
// If we finished processing, there should be no remaining bytes expected.
return remainingBytes === 0;
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.