You are on page 1of 1

Parentheses Match

Problem Statement
A stack is data structure for temporary storage during a program. It is used whenever the data processing works according to a Last-In, First-Out (LIFO) order. As an example, we use a stack to help determine whether or not there are matching grouping symbols (parentheses, brackets, curly braces, etc.) in a given arithmetic expression.

Algorithm
Prompt the user to enter an expression. Ignoring all digits and operands, push the leftgrouping symbols on the stack. When a right-grouping symbol is encountered, pop the stack (what if the stack is empty?). Compare the two characters. If they don't match, return false. Somehow, determine the end of this process and return true if the expression has passed all the matches.

Assignment
Prompt the user to input a string and output whether the parentheses match. Test for matching <>, {}, [], and (). Use the stack methods in java.util.Stack<E>. Since the Java Collections classes are always classes of objects, not of primitives, you will likely be processing a stack of String objects.
public class ParenMatch { public static final String LEFT = "([{<"; public static final String RIGHT = ")]}>"; public static void main(String[] args) { } public static boolean check(String s) { } }

Test Data
input
5+7 (5+7) )5+7( ((5+7)*3) [(5+7)*]3 <{5+7}*3> (5+7)*3 5+(7*3) ((5+7)*3 [(5+7]*3) [(5+7)*3]) ([(5+7)*3]

output
5+7 is good (5+7) is good No. Bad. )5+7( ((5+7)*3) is good. [(5+7)*]3 is good. <{5+7}*3> is good. (5+7)*3 is good. 5+(7*3) is good. No. Bad. ((5+7)*3 No. Bad. [(5+7]*3) No. Bad. [(5+7)*3]) No. Bad. ([(5+7)*3]

oops! the algorithm isn't very strong

You might also like