bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
HardStackLC 591

Tag Validator

Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid. A code snippet is valid if all the following rules hold:

  1. The code must be wrapped in a valid closed tag. Otherwise, the code is invalid.
  2. A closed tag (not necessarily valid) has exactly the following format : TAGCONTENT. Among them, is the start tag, and is the end tag. The TAG\NAME in start and end tags should be the same. A closed tag is valid if and only if the TAG\NAME and TAG\CONTENT are valid.
  3. A valid TAGNAME only contain upper-case letters, and has length in range 1,9. Otherwise, the TAGNAME is invalid.
  4. A valid TAGCONTENT may contain other valid closed tags, cdata and any characters (see note1) EXCEPT unmatched .
Asked at 3 companies
GoogleMicrosoftUber
Hints
  • 1.Think about which data structure fits best
  • 2.Consider the time complexity of your approach
  • 3.Look for patterns in the constraints
LeetCode