Statistics

Problem Statement for "CorrectingParenthesization"

Problem Statement

Given a string of parentheses, we must turn it into a well formed string by changing as few characters as possible (we cannot delete or insert characters).

There are 3 kinds of parentheses: regular (), brackets [] and curly brackets {}. Each pair has an opening ('(', '[' and '{' respectively) and a closing (')', ']' and '}') character.

A well formed string of parentheses is defined by the following rules:

  • The empty string is well formed.
  • If s is a well formed string, (s), [s] and {s} are well formed strings.
  • If s and t are well formed strings, the concatenation st is a well formed string.

As examples, "([{}])", "" and "(){}[]" are well formed strings and "([}]", "([)]" and "{" are malformed strings.

Given a String s of parentheses, return the minimum number of characters that need to be changed to make it into a well formed string.

Definition

Class:
CorrectingParenthesization
Method:
getMinErrors
Parameters:
String
Returns:
int
Method signature:
int getMinErrors(String s)
(be sure your method is public)

Notes

  • Changing a character is selecting one position in the string and changing the character in that position to any other parentheses character.

Constraints

  • s will have between 0 and 50 characters, inclusive.
  • s will have an even number of characters.
  • Each character of s will be '(', '[', '{', ')', ']' or '}'.

Examples

  1. "([{}])()[]{}"

    Returns: 0

    This is already well formed.

  2. "([)]"

    Returns: 2

    With two changes you can get "([])" (there are other ways with the same number of changes).

  3. "([{}[]"

    Returns: 1

    Simply changing the second character will give you "(){}[]".

  4. ""

    Returns: 0

  5. "]["

    Returns: 2

  6. "()[]"

    Returns: 0

  7. "[(}][{}]"

    Returns: 1

  8. "((((([[[[[{{{{{}}}}}]]]]])))))"

    Returns: 0

  9. "}}}}}]]]]])))))((((([[[[[{{{{{"

    Returns: 16

  10. ")()[]{}[]{}[]{}()["

    Returns: 2

  11. "){[}]("

    Returns: 4

  12. "}]"

    Returns: 1

  13. "([{]()"

    Returns: 2

  14. "(){}[})}){}]"

    Returns: 2

  15. "[]}({[[](([(]]})"

    Returns: 5

  16. "{})({])[]}})]{{})}{{)]"

    Returns: 6

  17. "{}({}(()[[[])}(]}}{]][[(]]"

    Returns: 6

  18. "[(]{)[}]]}})}{[[)}}]{[][[][{{[[}"

    Returns: 9

  19. ")}[(({}}}}[{)}]])]]]()[]{[}[))}{}){)"

    Returns: 11

  20. "{}}{[(][]][}[]({(]{})[]]([[(})(][])][[{]{("

    Returns: 11

  21. "(]{]]]]}]){()(]}([)[((}[]])}{{{){{]{)))}(]((]]"

    Returns: 14

  22. "([{(({[{[[[{({((({{[([{{{)}}]}})}]}]])]])}})))])}]"

    Returns: 9

  23. "({{{[({([]}]})})))"

    Returns: 4

  24. "{[[((({{{(())))]]])))]"

    Returns: 5

  25. "}])}])])})}])}]]}])}])}]]{([[[{(([{{[({(([{[({[{{["

    Returns: 26

  26. "])])}}]}))}]}[{[[(([[{({(["

    Returns: 14

  27. "})]])]}]))})}]}]}}))}[{[{[[{{([[[([[({[[({"

    Returns: 22

  28. "[((({}{})()[]("

    Returns: 2

  29. "{[[()({)}[]){}][){}][)"

    Returns: 5

  30. "[([])]{}[]"

    Returns: 0

  31. "({}{}(])"

    Returns: 1

  32. "({{}{}(({[{}]]()[{{)}]]{}}](){](])[}()"

    Returns: 7

  33. "[((()[]]}()))]"

    Returns: 1

  34. "{([(]({}[))[){{}}[]}]])]]{}]){]}"

    Returns: 5

  35. "[(}([{((([])))}]))]()]"

    Returns: 2

  36. "(({}))"

    Returns: 0

  37. "{{([{}()([[]{[]{]())()])}{]}"

    Returns: 2

  38. "][((({(()][)])(}([({{}}))]()(){}{[([))()()))][{()]"

    Returns: 10

  39. "[]"

    Returns: 0

  40. "(}}[{)({[([{[][()({)}]}][{}({[({](])"

    Returns: 9

  41. "()[()([{}(()()()[]{}[[]]{}{})}()}])[]{}]{}"

    Returns: 1

  42. "[([{(}()[)]]){"

    Returns: 4

  43. "({[{{][{{{({[{{}()}{}][]})}[]}[](){{}]}()]})"

    Returns: 2

  44. "()"

    Returns: 0

  45. "{(([]}()[{}]](){}[]){){}"

    Returns: 3

  46. "{{}{([((()))][]{])}()}"

    Returns: 1

  47. "{](()[([{))[[]{]{}{}[(([}{}{)[[[]}{}]}(){()}[("

    Returns: 9

  48. "([]{[]])({[{}[]]})"

    Returns: 1

  49. "()[]{}[]{}[]{}[]}{}[]{}[]{})"

    Returns: 1

  50. "]]]]]]]]]]]]"

    Returns: 6

  51. "{{{{{{{{{{{{{{"

    Returns: 7

  52. "))))))))))))))))))))))))))))))))))))))))))))))))))"

    Returns: 25

  53. "({[[{([[{{{{(({(({{{{[[[{({{[{[(({[{{{[[[(((((((({"

    Returns: 25

  54. "{}{[(}}]()))[)(["

    Returns: 5

  55. ")([}}{([{(({})}()(({}{)]}))}}]({]}"

    Returns: 7

  56. "}[[{{{][}(){}[][]}"

    Returns: 4

  57. "{[{({}{]]}{{}{}][(]{}])}]}[("

    Returns: 4

  58. "]}{][]{}][{[}][({{[{(}[})}()]{)}]}}["

    Returns: 10

  59. "})([)]()[}[](])]"

    Returns: 5

  60. "{}"

    Returns: 0

  61. "{{(()({](([((({({][]{[]]]){}}})[]{"

    Returns: 8

  62. "[]{[]{}([[]()[){}()}{}"

    Returns: 1

  63. "[{]]([]]}[[[[{[(]{{{[]]}[){[{())(]]{]]([)(}("

    Returns: 13

  64. "({}){{"

    Returns: 1

  65. "[[][][]((([]()())))(){}]"

    Returns: 0

  66. "{}({}]}){}()"

    Returns: 1

  67. "{]()"

    Returns: 1

  68. ")()](][]()(()()]{()[))"

    Returns: 4

  69. ")]({{}})})"

    Returns: 2

  70. "{{[{[[{}[][]}}"

    Returns: 2

  71. "({(([[[()(}]]]})()"

    Returns: 2

  72. "[{(([[}[([][]({{}{}{{({[[]}(](]}]{}}))}}}])(){})}]"

    Returns: 7

  73. "({}{[[]{}[][]}]]{}))()"

    Returns: 2

  74. "[]"

    Returns: 0

  75. "{[({}}[{{{}}}]]{}[])[}(["

    Returns: 3

  76. "[)"

    Returns: 1

  77. "{{)(}()}{}{((())({([)[{{}[}[)}]}]]([))}}"

    Returns: 8

  78. "()({}{}{"

    Returns: 1

  79. "}[}(][(){)}{]})}{}{}())]"

    Returns: 6

  80. "[[]([{}()][]"

    Returns: 1

  81. "[]))[]"

    Returns: 1

  82. "{{{[[{{}{}[{}}"

    Returns: 3

  83. ")][(][]]()"

    Returns: 2

  84. ")))(({}{}}{{{}}}})({}{}}}}}}{{{{)}())}{]}{(]))(((("

    Returns: 13

  85. "({[}[)]]({[}[)]]({[}[)]]({[}[)]]({[}[)]]({[}[)]]"

    Returns: 15

  86. ")))((("

    Returns: 4

  87. ")[]]{(()))){}}}{{[[]]]}([{]}{}}]][[(()[]])))}}}["

    Returns: 9

  88. "]]]]"

    Returns: 2

  89. "(((((]]]{]}{{]]]]{{{{))[[{{{))(({]"

    Returns: 13

  90. "([{}])()[]{}(((}}}}}}][]{])((}}}{}][][()()}}))))"

    Returns: 9

  91. "{([}"

    Returns: 1

  92. "[{}]}}}}}}"

    Returns: 3

  93. "][{{"

    Returns: 3

  94. "(}){(}({))(}{(){(}(([][][(][])((()[][][](((}"

    Returns: 11

  95. ")[{{))]("

    Returns: 4

  96. "([{}[]([{}[]([{}[]([{}[]([{}[]([{}[]([{}[]([{}[]]]"

    Returns: 8

  97. "((({[{]}}]]]"

    Returns: 4

  98. "}}}}}}])])}])])}])])}])])}])])}])])}])])}])])}]}"

    Returns: 24

  99. "(((((]]]{]}{{]]]]{{{{))[[{{{))(({](((]]{"

    Returns: 16

  100. "(}{([]){][){()[]]}})({}[]]])(][{}}{{})({}){[])[](]"

    Returns: 10

  101. "((((((((((([})))))))))))([[[[[[[[[]]]]]]]]]}"

    Returns: 2

  102. "({(((((((((((((((((}}}}}}}}}}}}}}}}}[[[[}(}((}"

    Returns: 22

  103. "((({[[]})[{]]}"

    Returns: 4

  104. "())){[[[[}()))((({][[[[]"

    Returns: 8

  105. "[({]})"

    Returns: 2

  106. ")("

    Returns: 2

  107. "]]][[["

    Returns: 4

  108. "([{}[])))))]"

    Returns: 3

  109. "]["

    Returns: 2

  110. "([{}])()[]{}([{}])()[]{}([{}])()[]{}[}[](}{}{]}]"

    Returns: 3

  111. "}}}}"

    Returns: 2

  112. "(({[))}]"

    Returns: 2

  113. "][][(((((((()))))}}}}}[[[[[[[}}}]]]]]]]]]]}}{{{{"

    Returns: 10

  114. "))}})}[[}())]{])]({[{[({}}{(())[)[{})]][){({{({]"

    Returns: 14

  115. "{{{{"

    Returns: 2

  116. "([{}[()]{(}){}[[))}}{{"

    Returns: 5

  117. "{}({[[}){(][({}{))({[}{)"

    Returns: 6

  118. "{)})"

    Returns: 2

  119. "((]))()(([))"

    Returns: 2

  120. ")}[](){{{]])(){{}))))]"

    Returns: 7

  121. "[{](})"

    Returns: 3

  122. "[([{]})]"

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: