TopcoderArchiveVisit Topcoder
SRM28 Dec 2005
SRM 280

CompletingBrackets

Problem statement, definition, constraints, and public examples.

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

  1. "[["
    Returns: "[[]]"
    Add two ']' to the end to make this complete.
  2. "]["
    Returns: "[][]"
    Add one to the beginning and one to the end.
  3. "[[[[]]]]"
    Returns: "[[[[]]]]"
    This is already complete.
Back to all problems