Quiz
#591 Tag Validator
APPROACH
Validate a code snippet string according to these rules:
- The entire snippet must be enclosed in a single valid closed tag of the form <TAG_NAME>TAG_CONTENT</TAG_NAME>, where both tag names must match.
- A valid TAG_NAME contains only uppercase letters and has a length between 1 and 9.
- Valid TAG_CONTENT may include other valid closed tags, CDATA sections, and arbitrary characters — but no unmatched <, no mismatched tags, and no tags with invalid names.
- Any < without a following > is considered unmatched. Everything from < (or </) up to the next > is treated as a tag name (even if invalid).
- A CDATA section has the form <![CDATA[CDATA_CONTENT]]>, where CDATA_CONTENT is everything between <![CDATA[ and the first ]]>. CDATA content is always treated as plain text, never parsed.
Example 1:
Example 2:
Example 3:
What is the optimal approach for this problem?