Categories
interview

UTF-8 Validation – Java

Hint UTF-8 ranges between 1 to 4 bytes (8-bits).

Runtime: O(n)

class Solution {
    public boolean validUtf8(int[] data) {
        int n = data.length;
        int skip = 0b10000000;
        int check = 0;
        for (int currByte: data) {
            if (check > 0) {
                if ((currByte & skip) == skip)
                    check--;
                else
                    return false;
            } else {
                check = getHeadType(currByte);
                if (check < 0) return false;
            }
        }
        return check == 0;
    }

    public int getHeadType(int num) {
        if ((num & 0b11110000) == 0b11110000 && (num & 0b00001000) != 0b00001000) return 3;
        if ((num & 0b11100000) == 0b11100000 && (num & 0b00010000) != 0b00010000) return 2;
        if ((num & 0b11000000) == 0b11000000 && (num & 0b00100000) != 0b00100000) return 1;
        if ((num & 0b10000000) == 0b10000000) return -1; //error
        return 0;
    }
}