欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

LeetCode //C - 393. UTF-8 Validation

最编程 2024-10-02 08:38:29
...

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:

Number of Bytes   |        UTF-8 Octet Sequence
                  |              (binary)

----------------------------------±----------------------------------------------------------------------------------------------------

       1          |   0xxxxxxx
       2          |   110xxxxx 10xxxxxx
       3          |   1110xxxx 10xxxxxx 10xxxxxx
      4           |   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

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:

Input: data = [197,130,1]
Output: true
Explanation: data represents the octet sequence: 11000101 10000010 00000001.
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.

Example 2:

Input: data = [235,140,4]
Output: false
Explanation: data represented the octet sequence: 11101011 10001100 00000100.
The first 3 bits are all one’s and the 4th bit is 0 means it is a 3-bytes character.
The next byte is a continuation byte which starts with 10 and that’s correct.
But the second continuation byte does not start with 10, so it is invalid.

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;
}