393. UTF-8 Validation

Given an integer array data representing the data, return whether it is a valid UTF-8 encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters).

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

  1. For a 1-byte character, the first bit is a 0, followed by its Unicode code.
  2. For an n-bytes character, the first n bits are all one’s, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10.

This is how the UTF-8 encoding would work:

x denotes a bit in the binary form of a byte that may be either 0 or 1.

Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.
 

Example 1:
Example 2:
Constraints:
  • 1 < = d a t a . l e n g t h < = 2 ∗ 1 0 4 1 <= data.length <= 2 * 10^4 1<=data.length<=2104
  • 0 <= data[i] <= 255

From: LeetCode
Link: 393. UTF-8 Validation


Solution:

Ideas:

1. bytesToProcess Variable:

  • This variable keeps track of how many continuation bytes are expected after a multi-byte character. If bytesToProcess is 0, we’re expecting the start of a new UTF-8 character. If bytesToProcess is non-zero, we’re expecting continuation bytes.

2. Bitwise Checks:

  • For the first byte of a character, the code checks how many leading 1s are present to determine the length of the character (1, 2, 3, or 4 bytes).
  • For continuation bytes, the code checks if the byte starts with 10 using currentByte >> 6 == 0b10.

3. Handling Continuation Bytes:

  • If bytesToProcess is not zero, the code checks if the current byte starts with 10. If not, the UTF-8 encoding is invalid.

4. Final Check:

  • After processing all bytes, if bytesToProcess is non-zero, it means there were not enough continuation bytes, and the encoding is invalid.
Code:
bool validUtf8(int* data, int dataSize) {
    int bytesToProcess = 0;  // Tracks the number of continuation bytes expected

    for (int i = 0; i < dataSize; i++) {
        int currentByte = data[i];

        if (bytesToProcess == 0) {
            // Determine how many bytes are in this UTF-8 character
            if ((currentByte >> 5) == 0b110) {
                // 110xxxxx -> 2-byte character
                bytesToProcess = 1;
            } else if ((currentByte >> 4) == 0b1110) {
                // 1110xxxx -> 3-byte character
                bytesToProcess = 2;
            } else if ((currentByte >> 3) == 0b11110) {
                // 11110xxx -> 4-byte character
                bytesToProcess = 3;
            } else if ((currentByte >> 7) == 0b0) {
                // 0xxxxxxx -> 1-byte character (valid, continue to next)
                bytesToProcess = 0;
            } else {
                // Invalid UTF-8 start byte
                return false;
            }
        } else {
            // We are expecting a continuation byte: it must start with "10"
            if ((currentByte >> 6) != 0b10) {
                return false;
            }
            bytesToProcess--;
        }
    }

    // After all bytes are processed, there should be no more continuation bytes pending
    return bytesToProcess == 0;
}
10-02 10:20