FastPrepSimplified XML Validator

Simplified XML Validator

Airbnb logoAirbnb● MediumFULLTIMEOA
Learn

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) → boolean

Examples

Example 1

text = "text<a>more text</a>"return = true

The 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 = false

The closing tags overlap: b must close before a.

Example 3

text = "<invalid<>text</invalid>"return = false

The opening tag contains another < before it closes.

Constraints

  • 0 <= text.length <= 10^5.
  • text contains ASCII characters only.

More Airbnb problems

See Airbnb hiring insights
public boolean isValidSimplifiedXml(String text) {
  // Write your code here.
}
text"text<a>more text</a>"
expectedtrue
Checking account…