Problem Statement
Correct bracket 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)" and "[X]" are also correct sequences.
- Each correct bracket sequence can be derived using the above rules.
Examples of correct bracket sequences include "", "()", "()[][]", "([]())", and "([[()]])".
You are given a
Definition
- Class:
- BracketSequenceDiv1
- Method:
- count
- Parameters:
- String
- Returns:
- long
- Method signature:
- long count(String s)
- (be sure your method is public)
Constraints
- s will contain between 1 and 40 characters, inclusive.
- Each character in s will be one of '(', ')', '[', ']'.
Examples
"()[]"
Returns: 3
There are 3 valid ways to erase some characters and obtain a correct non-empty bracket sequence: Erase nothing and obtain "()[]". Erase the first two characters and obtain "[]". Erase the last two characters and obtain "()".
"())"
Returns: 2
Here we have 2 solutions: we can either erase the middle character or the last character. Note that we count each of those ways separately, even though in both cases we get the same string "()".
"()()"
Returns: 4
"([)]"
Returns: 2
"())[]][]([]()]]()]]]"
Returns: 3854
"(((((((((((((((((((())))))))))))))))))))"
Returns: 137846528819
"[][][][][][][][][][][][][][][][][][][][]"
Returns: 24466267019
"))))))))))))((((((((((((]]]]]]]]]]][[[["
Returns: 0
"()[]()[][]()()[][][]()()()[][]()()()[]()"
Returns: 1058874671
"[]()()[][]()()()()()()[][][][][]()[][][]"
Returns: 1682926959
"[(]]]][()(][)[[](]][()([("
Returns: 3057
")]([])]))]]](()"
Returns: 47
")([[[]]((([((]("
Returns: 29
"(]([[]([][()]()"
Returns: 355
")((][)[][)[()]]"
Returns: 184
"()]([]][]()[)[([)[([))[[](())()[)([([))]"
Returns: 22416206
"]]([][]))(][(([][][[)][))](]][)][([([[(]"
Returns: 1256040
"(][(][)][][)][[()((]])](])[[)(][([])([[)"
Returns: 5724814
"][]((]))([))[]])[((][)(]]]((]][)]))))())"
Returns: 16479684
"(]((][(()][)[([[)([)])])]])](]([)]()(][("
Returns: 4178817
"([([([([([([([([([([([([([([([([([([([(["
Returns: 0
"()()()()()()()()()()()()()()()()()()()()"
Returns: 24466267019
"[([([([([([([([([([([([([([([([([([([([("
Returns: 0
"([([([(((([([([([[[[]]]]]])]]])))]))))))"
Returns: 873535479
"([([([([([([([([([([])])])])])])])])])])"
Returns: 506376221
"(((((((((((((((((((((((((((((((((((((((("
Returns: 0