Leetcode 393 Solution
This article provides solution to leetcode question 393 (utf-8-validation)
Access this page by simply typing in "lcs 393" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/utf-8-validation
Solution
class Solution {
public:
bool validUtf8(vector<int>& data) {
for (int i = 0; i < data.size(); i++)
{
if ((data[i] & 0b10000000) == 0)
continue;
else if ((data[i] & 0b11100000) == 0b11000000)
{
if (i + 1 >= data.size())
return false;
if ((data[i + 1] & 0b11000000) != 0b10000000)
return false;
i++;
}
else if ((data[i] & 0b11110000) == 0b11100000)
{
if (i + 2 >= data.size())
return false;
if ((data[i + 1] & 0b11000000) != 0b10000000 || (data[i + 2] & 0b11000000) != 0b10000000)
return false;
i += 2;
}
else if ((data[i] & 0b11111000) == 0b11110000)
{
if (i + 3 >= data.size())
return false;
if ((data[i + 1] & 0b11000000) != 0b10000000 || (data[i + 2] & 0b11000000) != 0b10000000 || (data[i + 3] & 0b11000000) != 0b10000000)
return false;
i += 3;
}
else
return false;
}
return true;
}
};