FastPrepDeepest Nested Substrings

Deepest Nested Substrings

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given a balanced string containing lowercase letters and the bracket pairs (), [], and {}, return the contents of every bracket pair at the maximum nesting depth.

Return results from left to right. The returned content excludes the surrounding brackets. An empty deepest pair contributes the empty string.

Function

deepestNestedSubstrings(expression: String) → String[]

Examples

Example 1

expression = "a[bc]def{cd}"return = ["bc","cd"]

Both pairs are at depth one, the maximum depth.

Example 2

expression = "ran(n(d))o(m())"return = ["d",""]

The pairs around d and the empty string are the depth-two pairs.

Example 3

expression = "x{a[b(c)d]e}y"return = ["c"]

The innermost parentheses are at depth three.

Constraints

  • 0 <= expression.length <= 10^5.
  • The expression contains lowercase English letters and balanced, correctly matched brackets.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] deepestNestedSubstrings(String expression) {
  // Write your code here.
}
expression"a[bc]def{cd}"
expected["bc", "cd"]
Checking account…