#42Document Formatting Parser
A document-processing system receives text containing nested formatting blocks. Each pair of parentheses represents a formatting block whose contents must be processed in reverse order.
For every matching pair of parentheses, reverse the text inside that block. Nested blocks are processed from the innermost block outward.
After all formatting blocks have been processed, return the final document text without any parentheses.
Real-World Applications: Nested Document Transformation; Template Processing Systems; Structured Text Parsing;
Examples
Example 1
Input: documentText = "(abcd)"
Output: "dcba"
Explanation: The text inside the parentheses is abcd. Reversing it gives dcba.
Example 2
Input: documentText = "(a(bc)d)"
Output: "dbca"
Explanation: First, the inner bc is reversed to cb. The text becomes (acbd), which is then reversed to produce dbca.
Example 3
Input: documentText = "(eg(de(of)r)oc)"
Output: "codeforge"
Explanation: First, the innermost (of) is reversed to fo. Then (de(for)r) becomes rfoed after reversing the inner section, and finally the outer sections are reversed step by step to produce codeforge.
Constraints
- 1 <= documentText.length <= 2000
- documentText contains only lowercase English letters and parentheses.
- All parentheses are balanced.
- The final result must contain only lowercase English letters.
- The result preserves every original letter exactly once.