Question: Problem 3. Consider a sequence of n brackets, where each bracket is either opening (namely [) or closing (]). The sequence is legal if, intuitively,

 Problem 3. Consider a sequence of n brackets, where each bracket

Problem 3. Consider a sequence of n brackets, where each bracket is either opening (namely "[") or closing ("]"). The sequence is legal if, intuitively, every opening bracket finds its closing counterpart. For example, [[]]] is legal but [[]]] is not. Formally, a legal sequence is such that, one can continuously remove two adjacent brackets [] until all the brackets have disappeared. Suppose that the sequence is stored in an array of length n, where each bracket is stored in a cell. Give an algorithm to check whether the sequence is legal in O(n) time

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!