1368. UTF-8 Validation Using Bit Masking

MediumBit ManipulationBit ManipulationMaskingValidation

Given an array of integers where only the lowest 8 bits of each entry are meaningful, decide whether the data forms a valid UTF-8 encoding. A character occupies one byte starting with 0, or two to four bytes whose leading byte starts with 110, 1110 or 11110 and whose remaining bytes all start with 10.

Input: A JSON object {"data": [<integers>]} where each entry represents one byte.

Output: Return true if the whole array is a valid UTF-8 encoding, otherwise false.

Examples

Example 1
Input: {"data":[197,130,1]}
Output: true
Explanation: A two-byte character followed by an ASCII byte -> true.
Example 2
Input: {"data":[235,140,4]}
Output: false
Explanation: The three-byte lead is not followed by two valid continuations -> false.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →