# Stack is a data structure where data is accessed Solved

30.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` zip` solution files instantly, after Payment

## Description

5/5 - (1 vote)

A stack is a data structure where data is accessed using the LIFO (last in first out) principle. In this problem, use a stack to check whether a string has balanced parenthesesÂ (,Â )Â and bracketsÂ {,Â },Â [,Â ], or not.

A string that has balanced parentheses and brackets will be said to beÂ balanced. Any character that isÂ notÂ one ofÂ (,Â ),Â [,Â ],Â {, orÂ }Â is not important when deciding if a string is balanced and can be ignored.

We will define balanced as follows. A stringÂ strÂ is balanced

• ifÂ strÂ does not contain a parenthesis or bracket symbol, or
• strÂ consists of a balanced string surrounded by opening and closing parentheses or matching brackets. That is,Â strÂ isÂ (b),Â {b}Â orÂ [b], whereÂ bÂ is any balanced string, or
• ifÂ strÂ is the concatenation of any two balanced strings. That is,Â strÂ isÂ bc, whereÂ bÂ andÂ cÂ are any balanced strings.

You will complete the providedÂ BalancedÂ class that has two static methodsÂ isBalanced(String)Â andÂ numberOfBalancedStrings(String[]).

YourÂ isBalancedÂ method must use theÂ java.util.StackÂ class (in a way that solves the problem) to receive any grades for this problem.

http://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

Examples

The following strings have balanced parentheses

• (),Â ()()
• cat,Â c(at),Â (hello)(kitty)
• if( ((x-y) < 4) || (x > 12))
• ()(((s)))()()()()(x()((y))(x))()(ccccc(w))ssss()

The following strings doÂ notÂ have balances parentheses

• ),Â )(a),Â )a(

The following strings have balanced parentheses and brackets

• a,Â [],Â {},Â [()],Â []{}({}),
• for(int i=0; i<12; i+=1){x[i]+=f(1);}

The following strings doÂ notÂ have balanced parentheses and brackets

• (],Â {),Â [},Â [},Â (],Â ({)}h,Â [(]())
• for(int i=0; i<12; i+=1){

Note: You will receive partial marks if you code only works for parentheses (and not brackets).

• 10809605.zip