bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 393. UTF-8 Validation

393. UTF-8 Validation

Leetcode Solutions

Bit Manipulation Approach

  1. Initialize a variable n_bytes to 0, which will hold the number of bytes in the current UTF-8 character.
  2. Iterate over each integer in the input array.
  3. If n_bytes is 0, determine the number of bytes in the UTF-8 character by counting the leading 1 bits using bitwise operations.
  4. If the number of leading 1 bits is greater than 4 or the byte starts with 10, it's invalid.
  5. If n_bytes is greater than 1, check if the next byte starts with 10 using bitwise operations. If not, return False.
  6. After processing the leading byte, decrement n_bytes for each continuation byte processed.
  7. If we reach the end of the array and n_bytes is 0, return True. Otherwise, return False.
UML Thumbnail

String Manipulation Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...