Single-Nested Valid Bracket Substrings
Problem statement
Given a valid bracket sequence s built from round, square, and curly brackets, return every substring that forms one single nested segment.
Each opening bracket and its matching closing bracket define exactly one such segment. A segment may contain any number of complete child segments, but a concatenation of sibling segments is not itself one segment unless another matching pair encloses it.
Return the segments in increasing order of their opening positions. If equal substring values occur at different positions, retain every occurrence.
Function
singleNestedSegments(s: String) → String[]Examples
Example 1
s = "()[()]{[][]}"return = ["()","[()]","()","{[][]}","[]","[]"]The opening positions occur in the order 0, 2, 3, 6, 7, 9. The two equal [] segments come from different matched pairs, so both remain in the output.
Example 2
s = "{}{[]}"return = ["{}","{[]}","[]"]The whole string is a concatenation of two top-level segments, so it is not returned as one segment. Its three matched pairs contribute {}, {[]}, and [].
Example 3
s = "{[()]}"return = ["{[()]}","[()]","()"]The three opening brackets appear from outermost to innermost, so their matched substrings are returned in that order.
Constraints
1 <= s.length <= 2000.scontains only(,),[,],{, and}.sis a valid bracket sequence and may contain multiple top-level segments.