Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

UTF-8 Validation

XPChallenge Points: 20
levelLevel: Medium

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 LengthByte Format (binary)
1 byte0xxxxxxx
2 bytes110xxxxx 10xxxxxx
3 bytes1110xxxx 10xxxxxx 10xxxxxx
4 bytes11110xxx 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.

Example 1:

Input: data = [65]

Output: true

Explanation:

65 → 01000001 which matches the 1-byte UTF-8 format.

Example 2:

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.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result