mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
61 lines
4.3 KiB
HTML
61 lines
4.3 KiB
HTML
<p>Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid.</p>
|
|
|
|
<p>A code snippet is valid if all the following rules hold:</p>
|
|
|
|
<ol>
|
|
<li>The code must be wrapped in a <b>valid closed tag</b>. Otherwise, the code is invalid.</li>
|
|
<li>A <b>closed tag</b> (not necessarily valid) has exactly the following format : <code><TAG_NAME>TAG_CONTENT</TAG_NAME></code>. Among them, <code><TAG_NAME></code> is the start tag, and <code></TAG_NAME></code> is the end tag. The TAG_NAME in start and end tags should be the same. A closed tag is <b>valid</b> if and only if the TAG_NAME and TAG_CONTENT are valid.</li>
|
|
<li>A <b>valid</b> <code>TAG_NAME</code> only contain <b>upper-case letters</b>, and has length in range [1,9]. Otherwise, the <code>TAG_NAME</code> is <b>invalid</b>.</li>
|
|
<li>A <b>valid</b> <code>TAG_CONTENT</code> may contain other <b>valid closed tags</b>, <b>cdata</b> and any characters (see note1) <b>EXCEPT</b> unmatched <code><</code>, unmatched start and end tag, and unmatched or closed tags with invalid TAG_NAME. Otherwise, the <code>TAG_CONTENT</code> is <b>invalid</b>.</li>
|
|
<li>A start tag is unmatched if no end tag exists with the same TAG_NAME, and vice versa. However, you also need to consider the issue of unbalanced when tags are nested.</li>
|
|
<li>A <code><</code> is unmatched if you cannot find a subsequent <code>></code>. And when you find a <code><</code> or <code></</code>, all the subsequent characters until the next <code>></code> should be parsed as TAG_NAME (not necessarily valid).</li>
|
|
<li>The cdata has the following format : <code><![CDATA[CDATA_CONTENT]]></code>. The range of <code>CDATA_CONTENT</code> is defined as the characters between <code><![CDATA[</code> and the <b>first subsequent</b> <code>]]></code>.</li>
|
|
<li><code>CDATA_CONTENT</code> may contain <b>any characters</b>. The function of cdata is to forbid the validator to parse <code>CDATA_CONTENT</code>, so even it has some characters that can be parsed as tag (no matter valid or invalid), you should treat it as <b>regular characters</b>.</li>
|
|
</ol>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
|
|
<strong>Output:</strong> true
|
|
<strong>Explanation:</strong>
|
|
The code is wrapped in a closed tag : <DIV> and </DIV>.
|
|
The TAG_NAME is valid, the TAG_CONTENT consists of some characters and cdata.
|
|
Although CDATA_CONTENT has an unmatched start tag with invalid TAG_NAME, it should be considered as plain text, not parsed as a tag.
|
|
So TAG_CONTENT is valid, and then the code is valid. Thus return true.
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> code = "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
|
|
<strong>Output:</strong> true
|
|
<strong>Explanation:</strong>
|
|
We first separate the code into : start_tag|tag_content|end_tag.
|
|
start_tag -> <b>"<DIV>"</b>
|
|
end_tag -> <b>"</DIV>"</b>
|
|
tag_content could also be separated into : text1|cdata|text2.
|
|
text1 -> <b>">> ![cdata[]] "</b>
|
|
cdata -> <b>"<![CDATA[<div>]>]]>"</b>, where the CDATA_CONTENT is <b>"<div>]>"</b>
|
|
text2 -> <b>"]]>>]"</b>
|
|
The reason why start_tag is NOT <b>"<DIV>>>"</b> is because of the rule 6.
|
|
The reason why cdata is NOT <b>"<![CDATA[<div>]>]]>]]>"</b> is because of the rule 7.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> code = "<A> <B> </A> </B>"
|
|
<strong>Output:</strong> false
|
|
<strong>Explanation:</strong> Unbalanced. If "<A>" is closed, then "<B>" must be unmatched, and vice versa.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= code.length <= 500</code></li>
|
|
<li><code>code</code> consists of English letters, digits, <code>'<'</code>, <code>'>'</code>, <code>'/'</code>, <code>'!'</code>, <code>'['</code>, <code>']'</code>, <code>'.'</code>, and <code>' '</code>.</li>
|
|
</ul>
|