TopCoder problem "CompletingBrackets" used in SRM 280 (Division I Level One , Division II Level Two)



Problem Statement

    

A series of brackets is complete if we can pair off each left bracket '[' with a right bracket ']' that occurs later in the series. Every bracket must participate in exactly one such pair.

Given a String text add the minimal number of brackets to the beginning and/or end of text to make it complete. Return the result.

 

Definition

    
Class:CompletingBrackets
Method:complete
Parameters:String
Returns:String
Method signature:String complete(String text)
(be sure your method is public)
    
 

Constraints

-text will have between 1 and 50 characters inclusive.
-text will contain only the characters '[' and ']'.
 

Examples

0)
    
"[["
Returns: "[[]]"
Add two ']' to the end to make this complete.
1)
    
"]["
Returns: "[][]"
Add one to the beginning and one to the end.
2)
    
"[[[[]]]]"
Returns: "[[[[]]]]"
This is already complete.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=5977

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8077&pm=5977

Writer:

dimkadimon

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Search, Iteration