Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please explain to me how you thought about the problem and your logic behind it.

ID: 3856188 • Letter: P

Question

Please explain to me how you thought about the problem and your logic behind it.

Here is a function, with descriptions of its supposed to do. They are incorrectly implemented. Replace the incorrect implementations of this function with correct ones that use recursion in a useful way: your solution must not use the keywords while, for, or goto. You must not use global variables or variables declared with the keyword static, and you must not modify the function parameter lists. You must not use any references or pointers as parameters except for the parameters representing arrays. //str contains a single pair of parenthesis, return a new string //made of only the parenthesis and whatever those parenthesis //contain. ////Pseudocode Example: //subParen("abc(ghj)789") = > "(ghj)" //subParen("(x)7") = > "(x)" //subParen("4agh(y)") = > "(y)" // string subParen(string str) { return "*";//This is incorrect. }

Explanation / Answer


IMPLEMENTATION

import java.util.Stack;

public class StackParenthesisImplementation {
public static void main(String[] args) {
String Parenthesis = "[({})]";
char[] charParenthesis = Parenthesis.toCharArray();
boolean evalParanthesisValue = evalParanthesis(charParenthesis);
if(evalParanthesisValue == true)
System.out.println("Brackets are good");
else
System.out.println("Brackets are not good");
}
static boolean evalParanthesis(char[] brackets)
{   
boolean IsBracesOk = false;
boolean PairCount = false;
Stack<Character> stack = new Stack<Character>();
for(char brace : brackets)
{   
if(brace == '(' || brace == '{' || brace == '['){
stack.push(brace);
PairCount = false;
}
else if(!stack.isEmpty())
{
if(brace == ')' || brace == '}' || brace == ']')
{
char CharPop = stack.pop();
if((brace == ')' && CharPop == '('))
{
IsBracesOk = true; PairCount = true;
}
else if((brace == '}') && (CharPop == '{'))
{
IsBracesOk = true; PairCount = true;
}
else if((brace == ']') && (CharPop == '['))
{
IsBracesOk = true; PairCount = true;
}
else
{
IsBracesOk = false;
PairCount = false;
break;
}
}   
}
}   
if(PairCount == false)
return IsBracesOk = false;
else
return IsBracesOk = true;
}
}

public final class BalancedParanthesis {

private static final Map<Character, Character> brackets = new HashMap<Character, Character>();
static {
brackets.put('[', ']');
brackets.put('{', '}');
brackets.put('(', ')');
}

private BalancedParanthesis() {};

/**
* Returns true is parenthesis match open and close.
* Understands [], {}, () as the brackets
* It is clients responsibility to include only valid paranthesis as input.
* A false could indicate that either parenthesis did not match or input including chars other than valid paranthesis
*
* @param str the input brackets
* @return true if paranthesis match.
*/
public static boolean isBalanced(String str) {
if (str.length() == 0) {
throw new IllegalArgumentException("String length should be greater than 0");
}
// odd number would always result in false
if ((str.length() % 2) != 0) {
return false;
}

final Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < str.length(); i++) {
if (brackets.containsKey(str.charAt(i))) {
stack.push(str.charAt(i));
} else if (stack.empty() || (str.charAt(i) != brackets.get(stack.pop()))) {
return false;
}
}
return true;
}

public static void main(String[] args) {
assertEquals(true, isBalanced("{}([])"));
assertEquals(false,isBalanced("([}])"));
assertEquals(true, isBalanced("([])"));
assertEquals(true, isBalanced("()[]{}[][]"));
}
}

OR
private static Map<Character, Character> parenthesesMapLeft = new HashMap<>();
private static Map<Character, Character> parenthesesMapRight = new HashMap<>();

static {
parenthesesMapLeft.put('(', '(');
parenthesesMapRight.put(')', '(');
parenthesesMapLeft.put('[', '[');
parenthesesMapRight.put(']', '[');
parenthesesMapLeft.put('{', '{');
parenthesesMapRight.put('}', '{');
}

public static void main(String[] args) {
System.out.println("Please enter input");
Scanner scanner = new Scanner(System.in);

String str = scanner.nextLine();

System.out.println(isBalanced(str));
}

public static boolean isBalanced(String str) {

boolean result = false;
if (str.length() < 2)
return false;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {

char ch = str.charAt(i);
if (!parenthesesMapRight.containsKey(ch) && !parenthesesMapLeft.containsKey(ch)) {
continue;
}
if (parenthesesMapLeft.containsKey(ch)) {
stack.push(ch);
} else {
if (!stack.isEmpty() && stack.pop() == parenthesesMapRight.get(ch).charValue()) {
result = true;
} else {
return false;
}
}

}
if (!stack.isEmpty())
return result = false;
return result;
}
}