1
$\begingroup$

Here is the description of question

A bracket is considered to be any one of the following characters: (, ), {, }, [ or ]. Now two brackets are considered to be a matched pair if the an opening bracket e.g. (, [, or { occurs to the left of a closing bracket e.g. ), ] or } of the exact same type. There are three types of matched pairs of brackets: [], () and {}.

A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example, {([)]} is not balanced because the contents in between and are not balanced.

Given a string of brackets, determine whether each sequence of brackets is balanced & how many swaps are needed to make it balanced. If a string is balanced, return 0. If the string cannot be balanced return -1. Else return the number of swaps needed to balance the string.

Can someone please tell how to approach this ??

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.