Tag Validator - Problem

Imagine you're building a code validator for a markup language parser! Your task is to determine whether a given string represents valid code according to strict formatting rules.

The Challenge: Given a string representing code, validate it according to these rules:

  • Root Rule: Code must be wrapped in a valid closed tag like <HTML>...</HTML>
  • Tag Format: <TAG_NAME>CONTENT</TAG_NAME> where opening and closing tags match exactly
  • Valid Tag Names: Only uppercase letters A-Z, length 1-9 characters
  • Content Rules: Can contain other valid tags, CDATA sections, or regular text, but NO unmatched brackets or invalid tags
  • CDATA Sections: <![CDATA[anything goes here]]> - content is treated as literal text, not parsed
  • No Unmatched Elements: Every < must have a matching >, every opening tag needs its closing tag

Think of it like validating XML/HTML structure, but with stricter rules!

Input & Output

valid_basic.py โ€” Basic Valid Tag
$ Input: code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
โ€บ Output: true
๐Ÿ’ก Note: The code is wrapped in valid
tags, contains text content and a CDATA section with invalid tags inside (which are treated as literal text).
invalid_root.py โ€” Invalid Root
$ Input: code = "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
โ€บ Output: false
๐Ÿ’ก Note: While wrapped in DIV tags, the content contains unmatched brackets and malformed CDATA, making it invalid.
invalid_no_root.py โ€” No Root Tag
$ Input: code = "<A> <B> </A> </B>"
โ€บ Output: false
๐Ÿ’ก Note: The tags are not properly nested - appears before , violating the nested structure rule.

Visualization

Tap to expand
Tag Validation ProcessSTARTOpening TagPush to StackCDATA FoundSkip ContentClosing TagPop & VerifyVALIDStack Visualization:HTMLโ† Root tagBODYโ† Nested tagDIVโ† Current tagRules:โœ“ Tag names: 1-9 uppercase lettersโœ“ Proper nesting (LIFO order)โœ“ CDATA skips parsingโœ“ No unmatched bracketsโœ“ Content only inside tagsโœ“ Stack empty at end๐Ÿ’ก Key Insight:Use a stack to maintain the LIFO (Last In, First Out) order of nested tags, ensuring proper closing sequence while treating CDATA as atomic, unparsed content blocks.
Understanding the Visualization
1
Start Inspection
Begin with empty checklist, expecting to find opening tag
2
Opening Tag Found
Add tag to checklist of sections that need to be closed
3
Handle Special Content
Skip over sealed CDATA boxes without inspecting contents
4
Closing Tag Found
Check if it matches most recent unclosed section, then mark as complete
5
Final Validation
Ensure all sections have been properly closed (empty checklist)
Key Takeaway
๐ŸŽฏ Key Insight: The stack naturally maintains the nested structure of tags, ensuring that closing tags match their corresponding opening tags in the correct LIFO order, while CDATA sections are treated as atomic units.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We process each character exactly once, with constant time operations for stack push/pop

n
2n
โœ“ Linear Growth
Space Complexity
O(d)

Space proportional to maximum nesting depth d, which is at most O(n) in worst case

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค code.length โ‰ค 500
  • code consists of English letters, digits, '<', '>', '/', '!', '[', ']', '.', and ' '.
  • Valid tag names contain only uppercase letters A-Z with length 1-9
  • CDATA content can contain any characters between
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
26.5K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen
Algorithm Visualization
Pinch to zoom โ€ข Tap outside to close
Test Cases
0 passed
0 failed
3 pending

Select Compiler

Choose a programming language

Compiler list would appear here...