Day 7: I4G 10 days of Code Challenge.

LeetCode problem 393: UTF-8 validation

Today's challenge was honestly a lot😪.

I have come to realize that a lot of times, the bulk of the solution to algorithm problems lies in understanding properly the question or the problem posed

As usual, I spent a lot of time trying to understand the question. When I thought I had understood the question and tried to start a solution, I found out that I still had a lot of knowledge gaps.

The Question:

day 7 q1.png

Day 7 q.png

It will take a lot of reading and rereading to understand this but like I said earlier the entire idea behind the challenge was to develop or create an algorithm that can return true if the encoding is actual proper UTF 8 encoding and return false if it is not

Thought process

My initial idea was to convert the data list into a string, so every single element becomes a string and then to begin to check the number of 1s present in each string. This was very problematic because there were many variations of the octet binary numbers, and the algorithm did not solve all possible tests.

Having used the rules given to determine when a binary octet code is a proper UTF 8 code encoding, I decided to develop the algorithm based on the decimal equivalent of these binary codes or from a decimal point of view.

The rules were: 1-byte UTF 8 code <==> 0xxxxxxx in binary <==> a number between 0 and 127 (upper and lower bound included)

2-byte UTF 8 code <==> 110xxxxx 10xxxxxx in binary <==> a number between 192 and 223, AND a number between 128 and 191 as continuation

3-byte UTF 8 code <==> 1110xxxx 10xxxxxx 10xxxxxx <==> a number between 224 and 239, AND 2 numbers between 128 and 191 as continuation

4-byte UTF 8 code <==> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx <==> a number between 240 and 247, AND 3 numbers between 128 and 191 as continuation.

Solution:

Day 8.png

Time complexity: 0(n)

Ciao🦢

Â