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
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
โ Linear Growth
Space Complexity
O(d)
Space proportional to maximum nesting depth d, which is at most O(n) in worst case
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code
Test Cases
0 passed
0 failed
3 pending