Problem Statement
Correct bracket sequences are strings in which each character is a '(' or a ')', the total number of opening brackets is the same as the total number of closing brackets, and each prefix contains at least as many opening as closing brackets. For example, the shortest few correct bracket sequences are "", "()", "(())", and "()()".
The subsequence of a string S is any string that can be obtained by erasing zero or more characters of S. For example, each of the strings "", "ace", and "abcde" is a subsequence of "abcde".
We will use LCS(S,T) to denote the length of a longest common subsequence of strings S and T. In other words, LCS(S,T) is the largest k such that there is a string U of length k that is both a subsequence of S and a subsequence of T. For example, LCS("abcde","bad") = 2.
You are given a
- t will have the same length as s,
- t will be a correct bracket sequence,
- t will not be equal to s,
- LCS(s,t) will be as large as possible.
Definition
- Class:
- Bracket107
- Method:
- yetanother
- Parameters:
- String
- Returns:
- int
- Method signature:
- int yetanother(String s)
- (be sure your method is public)
Notes
- You may assume that the answer for each valid test case fits into a signed 32-bit integer variable.
Constraints
- s will contain between 4 and 50 characters, inclusive.
- Each character in s will be either '(' or ')'.
- s will be a correct bracket sequence.
Examples
"(())"
Returns: 1
There is only one other correct bracket sequence of the same length.
"(())()"
Returns: 3
There are four other correct bracket sequences of the same length: "((()))", "()(())", "()()()", and "(()())". However, only in three of those four cases the length of the longest common subsequence is 5. LCS( "(())()", "()(())" ) is only 4, therefore "()(())" is not a valid choice of the string t.
"()()()"
Returns: 3
"(((())))"
Returns: 5
"((())())"
Returns: 9
"((()()((((()(())((()((())(()))))))))))"
Returns: 237
"()()()(())()()(((())))(())()()()()()()(())()(())()"
Returns: 389
"()()()()()(())()()((()))()()()()()"
Returns: 189
"(((((((((()(((((((()(()(((()))))))))))))))))))))"
Returns: 177
"(()())(((((((((((((((()((()()(()))))))))))))))))))"
Returns: 218
"(((((((()(((((((()((()((()(()())))))))))))))))))))"
Returns: 276
"((()((()))))"
Returns: 17
"()()()((((((((())()())((()()())))()(()()())))))())"
Returns: 529
"()()()()()()()()(())((()))()()()"
Returns: 153
"(((((())((()()))((((((()()((())))(((()))))))))))))"
Returns: 320
"(())(())(()((((()(((()))))))))"
Returns: 93
"()()()()()()()(()())()()(((()))())()((())()())()"
Returns: 386
"(()((((((((()((((((()((((()())))))))))))))))))))"
Returns: 220
"()(((())))()()()()(())()()()()()(()()())(()(()))()"
Returns: 398
"()()()()()()()()(())()()()()()()()()()()()()()()"
Returns: 388
"(())()()(())()()()()(()()())()(())(())()()()(())"
Returns: 379
"()()()(((((((((()(()((()))))))))))))"
Returns: 133
"(()())(()())()()()()((()))(()())(())(()(()()))()()"
Returns: 393
"()()"
Returns: 1
"()((()((()()())))(((())(((())()(())(()))()()))))"
Returns: 489
"()()()()(())(())()((()((())))()()())()(())()(())"
Returns: 373
"((()()()(())(())((((())(())))))(((((()()))))))"
Returns: 372
"()()(())()()()()()()()()()()()()()(())()"
Returns: 233
"(())()()()()()()()()()()(((()()())))()((()(())))"
Returns: 333
"(()((()()())()((()))())()(((()()))))"
Returns: 314
"()()()(((((()()((()((()())(()()))()((())))))))))"
Returns: 428
"(()(()((((((((((()))))))))))))"
Returns: 79
"()()(())()()()()()()()()()()()(())((()))()()()()()"
Returns: 408
"(((((((((((((((((())))))))))))))))))"
Returns: 33
"()(())()(()())()()((()()))()()()()(())((()))()"
Returns: 328
"()((((()))(()(()())())))"
Returns: 105
"((()(()))()())(())((()(()())))(())()(())(()())()()"
Returns: 374
"()(((()()())()))()(()())(()(()()))(())"
Returns: 229
"()()((((((())))))()()((()()))()()(((()())())))"
Returns: 432
"()()()(((((()(((()(()())(())))))))))"
Returns: 190
"(((((((((((((((((((()(((()))))))))))))))))))))))"
Returns: 89
"(((()()((((((((((((((()())))))))))))))))))"
Returns: 150
"()()()()()()()()()()()()()(())()()()()()()()()()"
Returns: 393
"()()()()()()()()()()()(()())()()"
Returns: 156
"()()(()())(())((()(((()((()(((((()))))))))))))"
Returns: 246
"(())()()()()()()()()()()()()()()()()"
Returns: 153
"(((((((((())())((()(((((((((()))))))))))))))))))"
Returns: 177
"((()(((()()))(()(()(())(()()))))))"
Returns: 239
"(()(()((((((()))(((()(((((()((()))))))))))))))))"
Returns: 265
"((((()))))()(()())(((()))())()(((()))(()))"
Returns: 188
"(())((((()))(()((((())()((()())(())())))()))))"
Returns: 380
"(()(()(()())())()()(((())()(()(((())()()))()))))"
Returns: 609
"(())(()())()(()(())((())()))()()()()(()())()()()"
Returns: 387
"()()()()()()(()()()())()(())()()()()(())()()(())()"
Returns: 458
"((((((((((((((((((((((((()))))))))))))))))))))))))"
Returns: 47
"()()()()()()()()()()()()()()()()"
Returns: 120
"((((((((((((((((()(((((())))))))))))))))))))))"
Returns: 85
"((((()()((((((((((((()))))))))))))))))"
Returns: 101
"()(()(())()()()()())()(())()()(())()((()))()(()())"
Returns: 440
"()(((())(((((()))))(((()((()()()))))))))"
Returns: 225
"()(())(())((()))((())()())(())(()()()())(()())"
Returns: 307
"()()(()()())()(((())(()())))()()"
Returns: 168
"((()()(()()((()(((())((()((((((())))))))))))))))"
Returns: 349
"()(())()()()()((()()(())(()))()())()()"
Returns: 272
"((()(((()(((()()(())(()((((((())))))))))))))))"
Returns: 293
"()(()()(()()()()))"
Returns: 75
"((((((((((((((((((((((((()))))))))))))))))))))))))"
Returns: 47
"(((()(())(())()()(((()(()(()))))))))"
Returns: 253
"((()))((())()(()))(()(()))(())(((())()()))(()())"
Returns: 292
"()(()()()((()((((()((()))((())))))))))"
Returns: 243
"(()((()())())()((((()()((()()((()())()))))))))"
Returns: 498
"()()()()()()(())(())()()()()()"
Returns: 145
"()()()()()()()()()()()()()()()()()()()()()()()"
Returns: 253
"((((((((((((((()))))))))))))))"
Returns: 27
"((((()((((((()(()(((()((((()(())))))))))))))))))))"
Returns: 277
"()()(((())))()(((()))(()))()(())(())()"
Returns: 178
"()()()()()()()()(())()()(())()()()()()(())()()"
Returns: 374
"(()()()((()))()())"
Returns: 77
"()((())(((((()(((((()())((()(((()))())))))))))))))"
Returns: 338
"()()()()(()())()(()(())())()(())()(())()()()"
Returns: 339
"(())()(())()((())()((())))(())((()))(())()(()())"
Returns: 300
"(())((())((()(()(((()(((()))))))))))"
Returns: 167
"()(((((((((()(())(()(()(()(((())))))))))))))))"
Returns: 268
"((()()((())((())))))(()())"
Returns: 98
"(())()()()()()()()(())()()()()()()()()(())()()()"
Returns: 396
"()(())()()()()(())()()()()()()()()(())"
Returns: 227
"(((()((((((()()(((((((((((()))))))))))))))))))))"
Returns: 175
"()()()()()()()()()()(())()()()(())()()((()))()()()"
Returns: 428
"((((((((((((((((((((((()))))))))))))))))))))))"
Returns: 43
"()()()()()()()()()()()()()()()()"
Returns: 120
"(((((((()((((((((((((((((())))))))))))))))))))))))"
Returns: 93
"()((((((((()))))))))"
Returns: 24
"()(((()(()((((()((((((()(((((())))))))))))))))))))"
Returns: 249
"()()()(())()()(()()()())()()()()()(()())()()()()()"
Returns: 480
"()()()()()()()()()()()()()()()(())()()()()()()()()"
Returns: 420
"((()((()(()(((((((()))))))))))))"
Returns: 113
"()(())()()()()()()()()()()()()()()()()()()()()"
Returns: 273
"((()))"
Returns: 3
"()()()()()()()()()()()()()(())()()()(())()()()()()"
Returns: 436
"(((((((((((()((())))))))))))))"
Returns: 53
"((()(()))((()()())(((()((())))((()()()())(()))))))"
Returns: 540
"()()()()()()()()()()()()()()()()()()()()()()()()()"
Returns: 300
"(((((((((((((((((((((((())))))))))))))))))))))))"
Returns: 45
"((((((()(())((())))(()(())((())))()((((())))))))))"
Returns: 368
"(())(())(())(())((()))((()()))(())(())(())(())(())"
Returns: 281
"((((()))))(()()((())()))"
Returns: 72
"(()()(()))"
Returns: 17
"()()((())())()()()()()()((((()))))()()()"
Returns: 235
"(()(()(()(()(()(()(()(()())())())())())())())())"
Returns: 704
"()(()()()()((()))())()(()())()((())())(()(()(())))"
Returns: 414
"(())()(()((())))()(())()()()(()())()((()()()))()()"
Returns: 394