Simplified XML Validator
Problem statement
Given an ASCII string text, return whether it is valid under this simplified XML format.
The string consists of ordinary content and tags:
- Ordinary content may contain any ASCII character except
<and>. - An opening tag has the form
<name>. - A closing tag has the form
</name>. - A tag name is a non-empty sequence of visible ASCII characters that contains none of
<,>, or/. - Opening and closing tags must be correctly nested, with none missing or overlapping.
Text is allowed before, after, and between complete tag pairs, so the input does not need one outer root tag. The empty string is valid.
Function
isValidSimplifiedXml(text: String) → booleanExamples
Example 1
text = "text<a>more text</a>"return = trueThe opening and closing a tags match, and ordinary text is allowed outside and inside the pair.
Example 2
text = "<a>text<b>other text</a></b>"return = falseThe closing tags overlap: b must close before a.
Example 3
text = "<invalid<>text</invalid>"return = falseThe opening tag contains another < before it closes.
Constraints
0 <= text.length <= 10^5.textcontains ASCII characters only.