Tap here to switch tabs
Problem
Submissions

Problem: Tag Validator

med
30 min
Try to solve the Tag Validator problem.

Statement

Given a string code representing a code snippet, return true if it is a valid tag snippet, or false otherwise.

A code snippet is considered valid if all of the following conditions are met:

  1. The entire code must be enclosed by a single valid root tag: <TAG_NAME>TAG_CONTENT</TAG_NAME>.

  2. A valid TAG_NAME consists only of uppercase English letters, and its length must be in the range [1,9][1, 9].

  3. A start tag is formatted as <TAG_NAME>.

  4. An end tag is formatted as </TAG_NAME>, where the TAG_NAME matches the corresponding start tag.

  5. A valid TAG_CONTENT may contain valid closed tags, cdata, or any characters, provided that start tags and end tags are properly paired and nested.

  6. A cdata section begins with <![CDATA[ and ends at the first subsequent occurrence of ]]>. The enclosed text, CDATA_CONTENT, is treated as raw text and is not parsed for tags.

  7. Tags must be correctly matched and nested. A tag cannot be closed if there are open child tags inside it.

  8. Unmatched '<' characters, unmatched <![CDATA[, or unmatched ]]> are strictly invalid.

Constraints:

  • 1≤1 \leq code.length ≤500\leq 500

  • code consists of English letters, digits, '<', '>', '/', '!', '[', ']', '.', and ' '.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Tag Validator

med
30 min
Try to solve the Tag Validator problem.

Statement

Given a string code representing a code snippet, return true if it is a valid tag snippet, or false otherwise.

A code snippet is considered valid if all of the following conditions are met:

  1. The entire code must be enclosed by a single valid root tag: <TAG_NAME>TAG_CONTENT</TAG_NAME>.

  2. A valid TAG_NAME consists only of uppercase English letters, and its length must be in the range [1,9][1, 9].

  3. A start tag is formatted as <TAG_NAME>.

  4. An end tag is formatted as </TAG_NAME>, where the TAG_NAME matches the corresponding start tag.

  5. A valid TAG_CONTENT may contain valid closed tags, cdata, or any characters, provided that start tags and end tags are properly paired and nested.

  6. A cdata section begins with <![CDATA[ and ends at the first subsequent occurrence of ]]>. The enclosed text, CDATA_CONTENT, is treated as raw text and is not parsed for tags.

  7. Tags must be correctly matched and nested. A tag cannot be closed if there are open child tags inside it.

  8. Unmatched '<' characters, unmatched <![CDATA[, or unmatched ]]> are strictly invalid.

Constraints:

  • 1≤1 \leq code.length ≤500\leq 500

  • code consists of English letters, digits, '<', '>', '/', '!', '[', ']', '.', and ' '.