1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tag-validator.py 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
# 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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助