Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Java: The Interactive Edition

Section 3.6 Basic Balanced Parentheses

We now turn our attention to using stacks to solve real computer science problems. You have no doubt written arithmetic expressions such as
\((5 + 6) · (7 + 8) / (4 + 3)\)
where parentheses are used to order the performance of operations. You may also have some experience programming in a language such as Lisp with constructs like
(defun square(n)
     (* n n))
This defines a function called square that will return the square of its argument n. Lisp is notorious for using lots and lots of parentheses.
In both of these examples, parentheses must appear in a balanced fashion. Balanced parentheses means that each opening symbol has a corresponding closing symbol and the pairs of parentheses are properly nested. Consider the following correctly balanced strings of parentheses:
(()()()())

(((())))

(()((())()))
Compare those with the following, which are not balanced:
((((((())

()))

(()()(()
The ability to differentiate between parentheses that are correctly balanced and those that are unbalanced is an important part of recognizing many programming language structures.
The challenge then is to write an algorithm that will read a string of parentheses from left to right and decide whether the symbols are balanced. To solve this problem we need to make an important observation. As you process symbols from left to right, the most recent opening parenthesis must match the next closing symbol (see Figure 3.6.1). Also, the first opening symbol processed may have to wait until the very last symbol for its match. Closing symbols match opening symbols in the reverse order of their appearance; they match from the inside out. This is a clue that stacks can be used to solve the problem.
Figure 3.6.1. Matching Parentheses
Once you agree that a stack is the appropriate data structure for keeping the parentheses, the statement of the algorithm is straightforward. Starting with an empty stack, process the parenthesis strings from left to right. If a symbol is an opening parenthesis, push it on the stack as a signal that a corresponding closing symbol needs to appear later. If, on the other hand, a symbol is a closing parenthesis, pop the stack. As long as it is possible to pop the stack to match every closing symbol, the parentheses remain balanced. If at any time there is no opening symbol on the stack to match a closing symbol, the string is not balanced properly. At the end of the string, when all symbols have been processed, the stack should be empty. The Java code to implement this algorithm is shown in Listing 3.6.2.
Listing 3.6.2. Solving the Balanced Parentheses Problem
This method, parenCheck, assumes that a Stack class is available and returns a boolean result as to whether the string of parentheses is balanced. If the current symbol is (, then it is pushed on the stack (lines 8–9). Note also in line 14 that pop removes a symbol from the stack. The returned value is not used since we know it must be an opening symbol seen earlier. If the stack becomes empty before we reach the end of the symbolString, then there are too many closing parentheses and the string is not balanced, so we immediately return false (line 12). If a symbol is not an open or close parenthesis, we ignore it (lines 16–18), which allows more latitude in user input. At the end (line 21), the string represents a correctly balanced sequence of parentheses as long as the stack has been completely cleaned off.
You have attempted of activities on this page.