-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_393_Utf-8Validation.cpp
47 lines (39 loc) · 1.04 KB
/
_393_Utf-8Validation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Source - https://leetcode.com/problems/utf-8-validation/
Author - Shivam Arora
*/
#include <bits/stdc++.h>
using namespace std;
bool validUtf8(vector<int>& data) {
int count = 0;
for(int i = 0; i < data.size(); i++) {
if(count != 0) {
if((data[i] >> 6) == 0b10)
count--;
else
return false;
} else {
if((data[i] >> 7) == 0b0)
count = 0;
else if((data[i] >> 5) == 0b110)
count = 1;
else if((data[i] >> 4) == 0b1110)
count = 2;
else if((data[i] >> 3) == 0b11110)
count = 3;
else
return false;
}
}
return count == 0;
}
int main()
{
int n;
cout<<"Enter number of elements: ";
cin>>n;
vector<int> arr(n);
cout<<"Enter elements: ";
for(int i = 0; i < n; i++)
cin>>arr[i];
cout<<"Does the array represent a valid sequence of UTF-8 characters? "<<boolalpha<<validUtf8(arr)<<endl;
}