UTF-8 Validation

Problem Id: 393 Difficulty: Medium


Intuition

Solution


class Solution:
    def validUtf8(self, data: List[int]) -> bool:
        for i in data:
            if i < 0 or i > 255:
                return False

        i = 0
        while i < len(data):
            c = data[i]
            if c >> 7 == 0:
                i += 1
            else:
                if c > (((1 << 5) - 1) << 3):
                    return False
                if c >> 6 == 2:
                    return False
                ones = 0
                while (c >> (7 - ones)) % 2 == 1:
                    ones += 1
                if ones > 4:
                    return False
                shift = 1
                while shift < ones:
                    if shift + i >= len(data):
                        return False
                    tmp = data[shift + i]
                    if tmp >> 6 != 2:
                        return False
                    shift += 1
                i += ones
        return True