-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0393-utf-8-validation.py
More file actions
31 lines (27 loc) · 802 Bytes
/
0393-utf-8-validation.py
File metadata and controls
31 lines (27 loc) · 802 Bytes
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
# time complexity: O(n)
# space complexity: O(1)
from typing import List
class Solution:
def validUtf8(self, data: List[int]) -> bool:
nBytes = 0
mask1 = 1 << 7
mask2 = 1 << 6
for num in data:
mask = 1 << 7
if nBytes == 0:
while mask & num:
nBytes += 1
mask = mask >> 1
if nBytes == 0:
continue
if nBytes == 1 or nBytes > 4:
return False
else:
if not (num & mask1 and not (num & mask2)):
return False
nBytes -= 1
return nBytes == 0
data = [197, 130, 1]
print(Solution().validUtf8(data))
data = [235, 140, 4]
print(Solution().validUtf8(data))