In: Computer Science
Question 6 20 pts
Compose the body of the following Java method:
/* * Accepts an array of Character consisting of "{[()]}" characters and * returns true if all pairs of parentheses match. Returns false if * parenthesis expression is malformed. Braces are allowed to occur * inside brackets, and brackets are allowed to occur inside parentheses. */ public boolean parenthesesMatch(Character [] input) {
/*If you any query do comment in the comment section else like the solution*/
public boolean parenthesesMatch(Character [] input)
{
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < input.length; i++)
{
char c = input[i];
if (c == '(' || c=='{' || c=='[' ) {
stack.push(c);
}
else if (c == ')' ||c== '}' || c==']')
{
if (stack.size() == 0)
{
return false;
}
char p = stack.pop();
if (p == '(' && c==')' || p=='{' && c=='}' || p=='[' && c==']')
{
continue;
}
else
return false;
}
}
if (stack.size() != 0) {
return false;
}
return true;
}