You are given an array data, where each value represents a single byte. The task is to determine whether these bytes together form a correct UTF-8 encoded sequence.
A UTF-8 character can be represented using 1 to 4 bytes, and each character follows specific bit patterns:
| Character Length | Byte Format (binary) |
|---|---|
| 1 byte | 0xxxxxxx |
| 2 bytes | 110xxxxx 10xxxxxx |
| 3 bytes | 1110xxxx 10xxxxxx 10xxxxxx |
| 4 bytes | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
The first byte determines how many total bytes the character uses.
Every continuation byte must start with 10.
You must return true if the entire array follows valid UTF-8 encoding rules; otherwise, return false.
Input: data = [65]
Output: true
Explanation:
65 → 01000001 which matches the 1-byte UTF-8 format.
Input: data = [240, 162, 138, 147]
Output: true
Explanation:
240 → 11110000 → indicates a 4-byte character. All following bytes begin with 10, so the sequence is valid.
Accepted:
Submission: