Practice Tag Validator
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Return whether code is valid under these rules. It must consist of exactly one closed root tag. A closed tag has the form <NAME>content</NAME>. Tag names contain 1 through 9 uppercase letters A through Z; opening and closing names must match, and nested tags must close in the correct order. Inside a tag, content may be ordinary text, valid nested tags or CDATA. A less-than sign must begin a valid tag or a CDATA section, and tags must have their closing greater-than sign. CDATA starts with <![CDATA[ and ends at the first subsequent ]]>, and everything between these delimiters is treated as text rather than parsed as tags. CDATA is allowed only inside a tag. Any content before or after the root tag is invalid.
Source: Tag Validator on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
true
# Example 2
code = "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
true
# Example 3
code = "<A> <B> </A> </B>"
false
Constraints
Related LeetCode Problems
Practice more problems similar to Tag Validator.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.