I've been asked this question in a java dev interview.
Given a string entirely made of square brackets [
and ]
, find if it has the pattern [*][*][*]
inside. (the *
here represents a 0 or more balanced bracket strings). meaning, check if it has 3 or more of[]
inside at-least any one of[]
.
Examples:
for [[][[][][]]]
expected answer is true as it has [][][]
inside it.
for [[]][[][]]
expected answer is false as it has less than 3 consecutive []
inside any one of []
for [[[]][][[]]]
expected answer is true as it has [..][][..]
inside it. It doesn't matter if there are 0 or more []
inside a []
.
IMPORTANT: The input string is already balanced. It doesn't need to checked for balance.
The question is to find a specific nested pattern inside an already balanced string. The input to this problem is a "balanced" expression. The output is a boolean answer specifying whether it contains the specified pattern of brackets yes or no.