bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 591. Tag Validator

591. Tag Validator

Leetcode Solutions

Stack-Based Tag Validation

  1. Initialize an empty stack to keep track of open tags.
  2. Iterate over the input string character by character.
  3. If you encounter a '<', check the following character to determine if it's the start of a tag, an end tag, or a CDATA section.
  4. If it's the start of a tag, validate the tag name and push it onto the stack.
  5. If it's an end tag, validate the tag name and check if it matches the top of the stack. If it does, pop the stack.
  6. If it's a CDATA section, skip over its content.
  7. If you encounter any invalid tag name, unmatched tag, or improperly formatted CDATA section, return false.
  8. After processing the entire string, check if the stack is empty. If it is, return true; otherwise, return false.
UML Thumbnail

Regex-Based Tag Validation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...