forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag-validator.py
61 lines (54 loc) · 1.62 KB
/
tag-validator.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
# Time: O(n)
# Space: O(n)
class Solution(object):
def isValid(self, code):
"""
:type code: str
:rtype: bool
"""
def validText(s, i):
j = i
i = s.find("<", i)
return i != j, i
def validCData(s, i):
if s.find("<![CDATA[", i) != i:
return False, i
j = s.find("]]>", i)
if j == -1:
return False, i
return True, j+3
def parseTagName(s, i):
if s[i] != '<':
return "", i
j = s.find('>', i)
if j == -1 or not (1 <= (j-1-i) <= 9):
return "", i
tag = s[i+1:j]
for c in tag:
if not (ord('A') <= ord(c) <= ord('Z')):
return "", i
return tag, j+1
def parseContent(s, i):
while i < len(s):
result, i = validText(s, i)
if result:
continue
result, i = validCData(s, i)
if result:
continue
result, i = validTag(s, i)
if result:
continue
break
return i
def validTag(s, i):
tag, j = parseTagName(s, i)
if not tag:
return False, i
j = parseContent(s, j)
k = j + len(tag) + 2
if k >= len(s) or s[j:k+1] != "</" + tag + ">":
return False, i
return True, k+1
result, i = validTag(code, 0)
return result and i == len(code)