Write a java method that provides a String argument The meth
Write a java method that provides a String argument. The method needs to determine whether the parentheses, brackets, and curly braces are within the string, return true if they are balanced, false otherwise.
Write a java method that takes a postfix expression as a String argument. The expression can include positive or negative integers and operations ( + , – , * ) separated with white spaces. The method should return the result after evaluating the expression. You only need to implement the following operations: addition, subtraction, and multiplication.
Solution
1)
import java.util.*;
 public class SpecialCharacter {
    public static boolean isBracketMatch(String str) {
Stack<Character> stack = new Stack<Character>();
 char first_char;
 for(int i=0; i < str.length(); i++) {
 first_char = str.charAt(i);
if(first_char == \'(\')
 stack.push(first_char);
 else if(first_char == \'{\')
 stack.push(first_char);
 else if(first_char == \'[\')
 stack.push(first_char);
 else if(first_char == \')\')
 if(stack.empty())
 return false;
 else if(stack.peek() == \'(\')
 stack.pop();
 else
 return false;
 else if(first_char == \'}\')
 if(stack.empty())
 return false;
 else if(stack.peek() == \'{\')
 stack.pop();
 else
 return false;
 else if(first_char == \']\')
 if(stack.empty())
 return false;
 else if(stack.peek() == \'[\')
 stack.pop();
 else
 return false;
 }
 return stack.empty();
 }
    public static void main(String[] args) {
        SpecialCharacter ob=new SpecialCharacter();
 String s=\"[]\";
 boolean result=ob.isBracketMatch(s);
 System.out.println(\"Result :\"+result);
   }
 }
2)
import java.util.*;
public class Postfix {
    public static void main(String[] args) {
    public static int evaluatePostfix(String exp) throws Exception {
       
        Stack<Integer> stack = new Stack<>();
        exp = insertSpaces(exp);
String[] tokens = exp.split(\" \");
       for (String token: tokens) {
            if (token.length() == 0)
                continue; // extract the next token
            else if (token.charAt(0) == \'+\' || token.charAt(0) == \'-\' || token.charAt(0) == \'/\' || token.charAt(0) == \'*\') {
                    operatorSequence(stack, token.charAt(0));
            }
            else if (Character.isDigit(token.charAt(0))){
                stack.push(Integer.parseInt(token));
            }
            else
                throw new Exception(\"Invalid Expression: \");
        }
        return stack.pop();
    }
   public static void operatorSequence(
        Stack<Integer> stack, char operator) {
        int op1 = stack.pop();
        int op2 = stack.pop();
       switch (operator) {
            case \'+\': stack.push(op2 + op1); break;
            case \'-\': stack.push(op2 - op1); break;
            case \'/\': stack.push(op2 / op1); break;
            case \'*\': stack.push(op2 * op1);
        }
    }
   public static String insertSpaces(String s) {
        String res = \"\";
       for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == \'+\' || s.charAt(i) == \'-\' || s.charAt(i) == \'/\' || s.charAt(i) == \'*\')
                res += \" \" + s.charAt(i) + \" \";
            else
                res += s.charAt(i);
        }
        return res;
    }
 }


