Connection Status:
Competition Arena > CompletingBrackets
SRM 280 · 2005-12-28 · by dimkadimon · Simple Search, Iteration
Class Name: CompletingBrackets
Return Type: String
Method Name: complete
Arg Types: (string)
Problem Statement

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.

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.

3)
"][]["
Returns: "[][][]"
4)
"[]"
Returns: "[]"
27)
"]]]]]]]]]]]]]]]]]]]]]]]]][[[[[[[[[[[[[[[[[[[[[[[[["
Returns: "[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]][[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]"

Long output

28)
"[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]"
Returns: "[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]"

long and complete

Submissions are judged against all 74 archived test cases, of which 7 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class CompletingBrackets with a public method string complete(string text) · 74 test cases · 2 s / 256 MB per case

Submitting as anonymous