From the course: Nail Your Java Interview

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: Matching parentheses

Solution: Matching parentheses - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Solution: Matching parentheses

(upbeat music) - [Instructor] Let's create a function that can determine if a piece of text has matching parentheses. As always, the first step is to set up some tester input. We'll use them in our matching parentheses function and see whether or not each string has matching parentheses. Through a visual check, we can see that the first four strings have matching parentheses, or matching block symbols, and the last four strings do not have matching parentheses. Let's take a look at our function. It's called matchingParentheses and it takes in a string as input. It returns a Boolean that represents whether or not the input string has matching parentheses. To start off, we create some data structures. The stack will help us keep track of the parentheses symbols as we iterate throughout the string. The map gives us a correspondence between a given open symbol and a closed symbol, since a square bracket cannot close an…

Contents