ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Parentheses.h

The longest balanced strings of parentheses inside a text of \( n \) characters, where open and close are the two parentheses and every other character is neither. More...

Files

file  ADS/string/Parentheses.h
 The longest balanced strings of parentheses inside a text of \( n \) characters, where open and close are the two parentheses and every other character is neither.

Functions

std::size_t ADS::longestBalancedSubsequence (std::string_view s, char open='(', char close=')')
 the length of a longest subsequence of \( s \) that is a balanced string of the parentheses open and close
std::pair< std::size_t, std::size_t > ADS::longestBalancedSubstring (std::string_view s, char open='(', char close=')')
 where the leftmost of the longest balanced substrings of \( s \) starts and its length, \( ( 0 , 0 ) \) if there is none

Detailed description

The longest balanced strings of parentheses inside a text of \( n \) characters, where open and close are the two parentheses and every other character is neither.

The definitions, the invariants and the costs of the area are in Theory of strings.

Function documentation

◆ longestBalancedSubsequence()

std::size_t ADS::longestBalancedSubsequence ( std::string_view s,
char open = '(',
char close = ')' )
inlinenodiscard

the length of a longest subsequence of \( s \) that is a balanced string of the parentheses open and close

In pseudocode (chapter 13, by a greedy scan instead of the recursion on the intervals), each close matched with the nearest open still pending:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestBalancedSubsequence}( s )\quad \triangleright\ \text{each close matched to an earlier open} \cr 2 & \quad \mathit{pairs} \gets 0;\ \mathit{pending} \gets 0\quad \triangleright\ \text{the opens not yet matched} \cr 3 & \quad \textbf{foreach}\ c \in s \text{, from the left}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ c = \mathit{close}\ \textbf{and} \ \mathit{pending} > 0\ \textbf{then}\ ( \mathit{pending} , \mathit{pairs} ) \gets ( \mathit{pending} - 1 , \mathit{pairs} + 1 ) \cr 5 & \qquad \textbf{else if}\ c = \mathit{open}\ \textbf{then} \ \mathit{pending} \gets \mathit{pending} + 1 \cr 6 & \quad \textbf{return}\ 2\, \mathit{pairs} \end{array}} \]

Precondition
open differs from close

◆ longestBalancedSubstring()

std::pair< std::size_t, std::size_t > ADS::longestBalancedSubstring ( std::string_view s,
char open = '(',
char close = ')' )
inlinenodiscard

where the leftmost of the longest balanced substrings of \( s \) starts and its length, \( ( 0 , 0 ) \) if there is none

In pseudocode (chapter 13, by a stack), \( O \) the places of the opens not matched yet and \( \mathit{base} \) the start of the current balanced run:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestBalancedSubstring}( s ) \cr 2 & \quad O \gets \text{an empty stack of the places of the opens}; \ \mathit{base} \gets 0;\ ( \mathit{start} , \ell ) \gets ( 0 , 0 ) \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do}\quad \triangleright\ \mathit{base} \text{ the first place after an unmatched close} \cr 4 & \qquad \textbf{if}\ s[ i ] = \mathit{open}\ \textbf{then} \ \mathrm{push}( O , i ) \cr 5 & \qquad \textbf{else if}\ s[ i ] = \mathit{close}\ \textbf{and}\ O = \emptyset\ \textbf{then}\ \mathit{base} \gets i + 1 \cr 6 & \qquad \textbf{else if}\ s[ i ] = \mathit{close}\ \textbf{then} \cr 7 & \qquad\quad \mathrm{pop}( O );\ a \gets \mathrm{top}( O ) + 1 \text{ if } O \ne \emptyset \text{, else } \mathit{base}\quad \triangleright\ s[ a .. i ] \text{ is balanced} \cr 8 & \qquad\quad \textbf{if}\ \ell < i + 1 - a\ \textbf{then}\ ( \mathit{start} , \ell ) \gets ( a , i + 1 - a ) \cr 9 & \qquad \textbf{else}\ O \gets \emptyset;\ \mathit{base} \gets i + 1\quad \triangleright \ \text{a character of no pair breaks every substring} \cr 10 & \quad \textbf{return}\ ( \mathit{start} , \ell ) \end{array}} \]

Precondition
open differs from close
Postcondition
\( s[ \mathit{start} .. \mathit{start} + \mathit{length} ) \) is balanced