Statistics

Problem Statement for "ParenthesesDiv2Easy"

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 "(((())))".


We can define the depth of a correct parentheses sequence recursively as follows:
  • The empty string "" has depth 0.
  • If the depth of "X" is x and the depth of "Y" is y then the depth of "XY" is max(x,y).
  • If the depth of "X" is x then the depth of "(X)" is x+1.
For example, the depth of "(((())))" is 4, the depth of "()()()" is 1, and the depth of "(())()" is 2.

Note that the depth of each correct parentheses sequence is uniquely defined using the above rules.
For example, when evaluating the depth of "()()()" it does not matter whether we take X = "()" and Y = "()()" or we take X = "()()" and Y = "()", the result will be the same in both cases.

Given a String s that is a correct parentheses sequence, calculate and return the depth of s.

Definition

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

Constraints

  • s will contain between 2 and 50 characters, inclusive.
  • Each character in s will be '(' or ')'.
  • s will be a correct parentheses sequence.

Examples

  1. "(())"

    Returns: 2

    The depth of "" is 0. Therefore, the depth of "()" is 1. Next, the depth of "(())" is the depth of "()" plus 1, which makes it 1+1 = 2.

  2. "()()()"

    Returns: 1

    The depth of "()()" is the maximum of the depth of "()" and the depth of "()". In other words, it is max(1,1) = 1. The depth of "()()()" is the maximum of the depth of "()()" and the depth of "()". Hence, this also equals max(1,1) = 1.

  3. "(())()"

    Returns: 2

    The answer is max(2, 1) = 2.

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

    Returns: 4

  5. "()"

    Returns: 1

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

    Returns: 2

  7. "(())"

    Returns: 2

  8. "()(()())((()))()()(())()()()()()"

    Returns: 3

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

    Returns: 22

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

    Returns: 2

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

    Returns: 11

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

    Returns: 21

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

    Returns: 2

  14. "(()()(())())()(()()((((()(()))))))"

    Returns: 7

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

    Returns: 2

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

    Returns: 2

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

    Returns: 6

  18. "(())"

    Returns: 2

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

    Returns: 2

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

    Returns: 3

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

    Returns: 10

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

    Returns: 17

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

    Returns: 4

  24. "()"

    Returns: 1

  25. "()"

    Returns: 1

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

    Returns: 3

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

    Returns: 1

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

    Returns: 3

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

    Returns: 4

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

    Returns: 4

  31. "()(())"

    Returns: 2

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

    Returns: 5

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

    Returns: 5

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

    Returns: 4

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

    Returns: 2

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

    Returns: 5

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

    Returns: 3

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

    Returns: 8

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

    Returns: 5

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

    Returns: 2

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

    Returns: 5

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

    Returns: 3

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

    Returns: 4

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

    Returns: 3

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

    Returns: 6

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

    Returns: 7

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

    Returns: 5

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

    Returns: 4


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: