A balanced bracket sequence is a string consisting only of and and satisfying the following conditions:
- The empty string is a balanced bracket sequence.
- If is a balanced bracket sequence, then is also a balanced bracket sequence.
- If are both balanced bracket sequences, then is also a balanced bracket sequence.
For example, is a balanced bracket sequence, whereas is not.
Sometimes there are multiple kinds of brackets, such as . Such variant bracket sequences have definitions similar to those of ordinary bracket sequences.
This article introduces classic problems related to bracket sequences.
Note: in English, a left bracket is generally called an opening bracket, while a right bracket is called a closing bracket.
Checking validity
The classic way to determine whether is a balanced bracket sequence is based on a greedy idea. The same algorithm also applies to variant bracket sequences.
We maintain a stack and consider in order:
- If is a right bracket, the stack is nonempty, and the top element of the stack is the left bracket corresponding to , pop the top element.
- If the above conditions are not met, push onto the stack.
After traversing the entire , if the stack is empty, then is a balanced bracket sequence; otherwise, it is not. The time complexity is .
Counting balanced bracket sequences
Consider finding, for length , the number of balanced bracket sequences ; call this number . We may enumerate the position of the bracket matching ; suppose it is . It divides the entire sequence into two shorter balanced bracket sequences. Therefore,
This is also the recurrence for the Catalan numbers. In other words, .
Naturally, the method for counting variant balanced bracket sequences is similar. Suppose there are different types of brackets; then .
Lexicographic successor
Given a balanced bracket sequence , we want to find, among all valid bracket sequences of length sorted in ascending lexicographic order, the next valid bracket sequence after . In this problem, we regard the lexicographic order of a left bracket as smaller than that of a right bracket, and do not consider variant bracket sequences.
We need to find the largest such that is a left bracket. Then change it into a right bracket and reconstruct the part . In addition, must satisfy the condition that the number of left brackets in is greater than the number of right brackets.
Suppose that after is changed into a right bracket, the number of left brackets in exceeds the number of right brackets by . Then we make 's last characters right brackets, and fill in the form , because this filling gives the smallest lexicographic order.
The time complexity of this algorithm is .
Reference implementation
bool next_balanced_sequence(string& s) {
int n = s.size();
int depth = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '(')
depth--;
else
depth++;
if (s[i] == '(' && depth > 0) {
depth--;
int open = (n - i - 1 - depth) / 2;
int close = n - i - 1 - open;
string next =
s.substr(0, i) + ')' + string(open, '(') + string(close, ')');
s.swap(next);
return true;
}
}
return false;
}
Computing lexicographic rank
Given a balanced bracket sequence , we want to find its lexicographic rank.
Consider finding, relative to , the number of bracket sequences that are lexicographically smaller.
Suppose that and $\forall 1\le j Its English translation Balanced bracket sequences. The Russian version is licensed under Public Domain + Leave a Link; the English version is licensed under CC-BY-SA 4.0.**