UTF-8 Validation
expert · 1000 · lc medium +32 · verified · 46.1% accepted · 947 likes · top 30%
Description
You are given an integer array data where only the low-order 8 bits of each integer matter. These bytes represent a sequence of characters encoded in UTF-8. Determine whether the byte sequence is valid.
Valid UTF-8 characters span 1 to 4 bytes according to these binary patterns:
Number of Bytes | UTF-8 Octet Sequence
| (binary)
--------------------+-----------------------------------------
1 | 0xxxxxxx
2 | 110xxxxx 10xxxxxx
3 | 1110xxxx 10xxxxxx 10xxxxxx
4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
Return true if valid, false otherwise.
Example 1:
Example 2:
Example 3:
Code