Track your progress, run tests, and get AI feedback
Run code and see test results
Submit and get AI-powered feedback
Track which problems you've solved
Syntax Validation: Nested Parentheses and Contextual Brackets
This problem challenges you to validate the syntax of a simplified programming construct involving multiple types of nested delimiters. You are given a string representing a snippet of "MacroScript" code. The task is to determine if all delimiters are correctly matched, properly nested, and adhere to specific contextual rules for a special "macro block" delimiter. This problem is a classic application of stack data structures for parsing and syntax validation.
Implement a function isValidMacroScript that takes a string s representing a MacroScript snippet. The function should return true if the string has a valid sequence of delimiters according to the rules below, and false otherwise.
The delimiters are:
( and )[ and ]{ and }| and | (The same character | serves as both opening and closing.)Rules for validity:
|...| can only contain standard parentheses () and square brackets []. It cannot contain curly braces {}. If a curly brace { or } is encountered within a |...| block, the string is invalid.Input:
s: A string consisting of various characters, including the specified delimiters.
Output:
boolean: true if the input string s is valid according to the rules, false otherwise.
Function Signature (TypeScript):
function isValidMacroScript(s: string): boolean
Function Signature (JavaScript):
function isValidMacroScript(s: string): boolean
| be handled if it's not part of a pair?
| character acts as both an opening and a closing delimiter for macro blocks. If you encounter | and the top of the stack is also |, it signifies a closing | for the current macro block. Otherwise, it acts as an opening |.| characters count as matching if they are not the top of the stack?
| must match the most recently opened | (which would be at the top of the stack). For example, (|[]) is invalid because ) is attempting to close |.Examples
Example 1
Input:
s = "([{}])"
Output:
true
Note:
All standard delimiters are matched and properly nested. No macro blocks are present, so the special rule is not violated.
Example 2
Input:
s = "|[a]b(c)|"
Output:
true
Note:
The macro block |...| contains only square brackets and parentheses, which is allowed. All delimiters are matched and nested correctly.
Example 3
Input:
s = "|{a}|"
Output:
false
Note:
The macro block |...| contains curly braces {}, which violates the special rule (macro blocks cannot contain curly braces).
Example 4
Input:
s = "([)]"
Output:
false
Note:
The square bracket [ is not closed before the parenthesis ( is closed. This is an example of incorrect nesting.
Example 5
Input:
s = "|(|)"
Output:
false
Note:
When processing |(|):
|: Push |. Stack: [|](: Push (. Stack: [|, (]|: This acts as an opening | (stack top is (). Push |. Stack: [|, (, |]): Mismatches with stack top |. Invalid.Constraints
1 <= s.length <= 10^4s will only contain standard ASCII characters.(, ), [, ], {, }, and |.