Hostname: page-component-6766d58669-wvcvf Total loading time: 0 Render date: 2026-05-24T18:13:18.482Z Has data issue: false hasContentIssue false

Longest segment of balanced parentheses: an exercise in program inversion in a segment problem

Published online by Cambridge University Press:  25 November 2021

SHIN-CHENG MU
Affiliation:
Institute of Information Science, Academia Sinica, Taipei, Taiwan (e-mail: scm@iis.sinica.edu.tw)
TSUNG-JU CHIANG
Affiliation:
Institute of Information Science, Academia Sinica, Taipei, Taiwan (e-mail: mail@tsung-ju.org)
Rights & Permissions [Opens in a new window]

Abstract

Given a string of parentheses, the task is to find the longest consecutive segment that is balanced, in linear time. We find this problem interesting because it involves a combination of techniques: the usual approach for solving segment problems and a theorem for constructing the inverse of a function—through which we derive an instance of shift-reduce parsing.

Information

Type
Functional Pearl
Copyright
© The Author(s), 2021. Published by Cambridge University Press
Figure 0

Figure 1. Results of parseF and filtJust for prefixes of "())()(".

Figure 1

Figure 2. Measured running time for some input sizes.

Submit a response

Discussions

No Discussions have been published for this article.