Skip to content

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.

Hard#591
LeetCode Problem

Tag Validator

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

Input
code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
Output
true

# Example 2

Input
code = "<DIV>>>  ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
Output
true

# Example 3

Input
code = "<A>  <B> </A>   </B>"
Output
false

Constraints

1 <= code.length <= 500
code consists of English letters, digits, '<', '>', '/', '!', '[', ']', '.', and ' '.

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.