Opening the reading…
Opening the reading…
BIT MANIPULATION › BASIC BIT CONCEPTS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →UTF-8 gives every character a visible shape in its first byte. A one-byte character starts with 0, while a multi-byte character starts with a run of 1 bits followed by a 0: 110 for two bytes, 1110 for three, or 11110 for four. Once you identify that prefix, you know exactly how many bytes belong to the current character.
Every byte after the first must start with 10. Therefore, scan from left to right, classify the current byte, and inspect exactly the required number of following bytes. A group is invalid if its starter has no allowed prefix, a continuation byte does not start with 10, or the array ends before the group is complete. Then continue at the first byte after the group.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | The outer scan advances by the length of each character, and the inner loop inspects each continuation byte exactly once. The groups are disjoint, so no byte is checked in two different groups. |
| Space | O(1) extra | The solution stores only indices, the current character length, and a few temporary values. The returned boolean is required output and is excluded from the extra-space bound; the bound remains O(1) even for the worst input shape. |
#include <vector>
using namespace std;
class Solution {
public:
bool validUtf8(vector<int>& data) {
int n = static_cast<int>(data.size());
int i = 0;
while (i < n) {
int num = data[i];
int cnt = 0;
if ((num & 0x80) == 0) {
cnt = 1;
} else if ((num & 0xE0) == 0xC0) {
cnt = 2;
} else if ((num & 0xF0) == 0xE0) {
cnt = 3;
} else if ((num & 0xF8) == 0xF0) {
cnt = 4;
} else {
return false;
}
if (i + cnt > n) {
return false;
}
for (int j = 1; j < cnt; ++j) {
if ((data[i + j] & 0xC0) != 0x80) {
return false;
}
}
i += cnt;
}
return true;
}
};The equality checks are stricter than merely counting leading 1 bits. For example, (num & 0xE0) == 0xC0 accepts only the 110xxxxx shape, while values beginning with 111 are tested against the three- and four-byte masks. The 0xF8 mask also ensures a four-byte starter has exactly 11110 at the front rather than five leading 1 bits.
You can keep the number of continuation bytes still required instead of validating one complete group inside an inner loop. A byte with a nonzero remaining count must start with 10; otherwise it begins a new character and determines a fresh count. This is not an asymptotic optimisation: it uses O(n) time and O(1) extra space like the grouped version, but it expresses the scan as a small state machine.
#include <vector>
using namespace std;
class Solution {
public:
bool validUtf8(vector<int>& data) {
int remaining = 0;
for (int byte : data) {
if (remaining > 0) {
if ((byte & 0xC0) != 0x80) {
return false;
}
--remaining;
continue;
}
if ((byte & 0x80) == 0) {
remaining = 0;
} else if ((byte & 0xE0) == 0xC0) {
remaining = 1;
} else if ((byte & 0xF0) == 0xE0) {
remaining = 2;
} else if ((byte & 0xF8) == 0xF0) {
remaining = 3;
} else {
return false;
}
}
return remaining == 0;
}
};The final remaining == 0 check is the state-machine equivalent of checking i + cnt <= n. If the loop ends with a positive value, the last character promised more continuation bytes than the array supplied. This version is convenient when you want one pass with no nested loop, while the grouped version makes each character's boundary more explicit.