Statistics

Problem Statement for "RemovingParenthesis"

Problem Statement

Correct parentheses sequences can be defined recursively as follows:

  • The empty string "" is a correct sequence.
  • If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
  • If "X" is a correct sequence, then "(X)" is a correct sequence.
  • Each correct parentheses sequence can be derived using the above rules.

Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".

You are given a String s that is guaranteed to be a correct parentheses sequence. A removal is an action that consists of two steps:

  1. Remove the first opening parenthesis in s.
  2. Remove one closing parenthesis in s. After you do so, s must again be a correct parentheses sequence.

Compute and return the number of distinct ways in which s can be reduced to an empty string by performing consecutive removals. Two ways are considered distinct if there is a step in which you remove a different closing parenthesis. (See Example 1 for clarification.) It is guaranteed that the correct return value will always fit into a 32-bit signed integer.

Definition

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

Constraints

  • s will have between 2 and 20 characters, inclusive.
  • s will be a correct parentheses sequence.

Examples

  1. "()()()()()"

    Returns: 1

    In each removal we have to choose the leftmost closing parenthesis.

  2. "(((())))"

    Returns: 24

    In each removal we can choose any closing parenthesis we want. Note that these count as distint choices, even though all choices lead to the same string. Thus, there are 4*3*2*1 = 24 different sequences of removals that change s into an empty string.

  3. "((()()()))"

    Returns: 54

    Below is one of the 54 possible sequences of removals. Remember that in each step we also remove the first opening parenthesis. Remove the fourth closing parenthesis: "(()()())" Remove the second closing parenthesis: "()(())" Remove the first closing parenthesis: "(())" Remove the second closing parenthesis: "()" Remove the first closing parenthesis: ""

  4. "(())(())(())"

    Returns: 8

  5. "((()))(()(()))((()))"

    Returns: 432

  6. "(((((((((())))))))))"

    Returns: 3628800

  7. "()()()()()()()()()()"

    Returns: 1

  8. "()"

    Returns: 1

  9. "()(())((()))(((())))"

    Returns: 288

  10. "()()()(())()()(())"

    Returns: 4

  11. "((()((()()))))"

    Returns: 1800

  12. "()()()()()(()()()())"

    Returns: 16

  13. "()()()()(()()())"

    Returns: 8

  14. "()"

    Returns: 1

  15. "()()()(())"

    Returns: 2

  16. "()()((()()()()))(())"

    Returns: 324

  17. "()()()(())()"

    Returns: 2

  18. "(())"

    Returns: 2

  19. "((()(((())(())))))"

    Returns: 64800

  20. "(()(()()())())"

    Returns: 216

  21. "()()()()(())"

    Returns: 2

  22. "()()()()"

    Returns: 1

  23. "()()()()(()())(())"

    Returns: 8

  24. "(((()((())()))()))"

    Returns: 43200

  25. "((((((()(())))))))"

    Returns: 282240

  26. "()()()()()((()))"

    Returns: 6

  27. "((((()(())))()))"

    Returns: 10800

  28. "()()()()()()()(())"

    Returns: 2

  29. "()()()(()())(())(())"

    Returns: 16

  30. "()(((()))(((()()))))"

    Returns: 14400

  31. "(()(())()((()))())"

    Returns: 1152

  32. "((((())((()(()))))))"

    Returns: 604800

  33. "(())((()(())(())))"

    Returns: 1728

  34. "(((()((())()))))()"

    Returns: 14400

  35. "()()()()(())(())"

    Returns: 4

  36. "()(())(())(()()())"

    Returns: 32

  37. "(())(())(())(()())"

    Returns: 32

  38. "((()(())))((())())"

    Returns: 864

  39. "()()()()()(())()()"

    Returns: 2

  40. "(())()()()(())(())"

    Returns: 8

  41. "(((()((())))()))"

    Returns: 8640

  42. "()()(())(()()(()))"

    Returns: 48

  43. "(((((())))(())))"

    Returns: 8640

  44. "()()()()()(())(())"

    Returns: 4

  45. "()()()()(()()(()))"

    Returns: 24

  46. "()()(())(())()"

    Returns: 4

  47. "()()()(())(()())"

    Returns: 8

  48. "()()(())(()(())())"

    Returns: 48

  49. "()()()()(()()())"

    Returns: 8

  50. "(((()((((())))))()))"

    Returns: 483840

  51. "((()()()))"

    Returns: 54


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: