BackmediumStack Adobe PayPal

Validating XML Tags in Space Mission Logs Solution

Problem Statement

Given a sequence of XML tags from space mission logs, determine whether the sequence is valid, meaning every opening tag can be matched with a corresponding closing tag that occurs after it, and the tags are properly nested.

Example 1
Input
<pilot><crew></crew></pilot>
Output
true

Explanation: Step-by-step: 1. Initialize an empty stack. 2. Iterate through the input string. 3. If an opening tag is encountered, push it onto the stack. 4. If a closing tag is encountered, check if the top of the stack matches the closing tag. If it does, pop the stack. 5. After iterating through the entire string, check if the stack is empty. If it is, the input string is valid.

Example 2
Input
<pilot><crew></pilot></crew>
Output
false

Explanation: Step-by-step: 1. Initialize an empty stack. 2. Iterate through the input string. 3. If an opening tag is encountered, push it onto the stack. 4. If a closing tag is encountered, check if the top of the stack matches the closing tag. If it does, pop the stack. 5. After iterating through the entire string, check if the stack is empty. If it is not, the input string is invalid.

Constraints

  • The input sequence will contain at most 1000 tags.
  • The input sequence will only contain valid XML tag names and '<' and '>' characters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free