Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Use Stack class to match balanced parentheses and brackets. Create a class, with a method: public static boolean matchParentheses(String s); It takes a String

Java

Use Stack class to match balanced parentheses and brackets. Create a class, with a method:

public static boolean matchParentheses(String s); It takes a String containing (, ), [, and ] and returns true if the parentheses are balanced.

For example: Matcher.matchParentheses("()") // returns true

Matcher.matchParentheses("([])") // returns true

Matcher.matchParentheses("([])[()]") // returns true

Matcher.matchParentheses("([)]") // returns false

Matcher.matchParentheses("(") // returns false

Matcher.matchParentheses("[]]") // returns false

I suggest implementing this by using Stack, pushing a ( or [ onto the stack whenever one of those characters is seen, and popping it off when the corresponding closing character is seen. If the wrong character is at the top of the stack, the parentheses do not match, and the method should return false. Note that Stack class can hold Character objects, but not char values, since char is a primitive type.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions

Question

Where does the right to privacy come from?

Answered: 1 week ago

Question

Explain methods of metal extraction with examples.

Answered: 1 week ago