Ctrl K

Markup Snippet Check

Hard
You get a markup snippet snippet. Return true if it is well formed under the rules below, and false otherwise. - A **tag name** is 1 to 9 uppercase Latin letters. - An **opening tag** is <NAME> and a **closing tag** is </NAME>. Every < (except one that starts a raw block) begins a tag, and everything up to the next > is taken as its name (after the / for a closing tag). If there is no > or the name is not allowed, the snippet is malformed. - An **element** is an opening tag, its content and a closing tag with the same name. The content may hold any text, other elements and raw blocks; tags must be properly nested. - A **raw block** starts with <![CDATA[ and ends at the first ]]> after it. Everything inside is plain text, even if it looks like tags. A block with no ]]> makes the snippet malformed. - The whole snippet must be **exactly one element**: no text, blocks or other elements before or after it. A > outside of tags is plain text.
Example 1
snippet='<NOTE>buy milk <B>today</B></NOTE>' → True

The outer NOTE tag is closed, and inside it there is text and a properly nested B tag.

Example 2
snippet='<P><![CDATA[<a> is not a tag]]></P>' → True

Everything between <![CDATA[ and ]]> is plain text, so <a> with its invalid name breaks nothing.

Example 3
snippet='<A><B></A></B>' → False

The tags overlap: A is closed before the B nested inside it.

Constraints
  • 1 ≤ len(snippet) ≤ 500
  • snippet consists of Latin letters, digits, spaces and the characters <, >, /, !, [, ], .
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Balanced Bracket Stretch HardPlus-Minus Calculator With Brackets HardClear the Marble Row HardElement Tally From a Formula HardA Calculator With Brackets and Variables Hard
loading Python…
Input
snippet='<NOTE>buy milk <B>today</B></NOTE>'
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 218 tests on the server, hidden ones included.