Skip to main content

393 - UTF-8 Validation

Details

KeyValue
Linkhttps://leetcode.com/problems/utf-8-validation/submissions/
LanguagePython 3
Runtime148 ms, faster than 76.75% of Python3 online submissions for UTF-8 Validation
Memory Usage14.2 MB, less than 70.76% of Python3 online submissions for UTF-8 Validation
DatastructuresList[int]
AlgorithmsString Manipulation
ComplexityTime: O(N) Memory: O(N)

Procedure

  1. ...

Code

class Solution:
def validUtf8(self, data: List[int]) -> bool:
count = 0

for byte in data:
if byte >= 128 and byte <= 191:
if not count: return False
count -= 1
else:
if count: return False
if byte < 128: continue
elif byte < 224: count = 1
elif byte < 240: count = 2
elif byte < 248: count = 3
else: return False

return count == 0