Question

In: Computer Science

Write a method called isMatch that takes a string of parentheses and check if all parentheses...

Write a method called isMatch that takes a string of parentheses and check if all parentheses match correctly. The parameter string might consist of other characters. Ignore other characters, just check the () {} [] public static boolean isMatch(String expressionLine); Use a JCF ArrayDeque or LinkedList as a stack to store all open or left parentheses.

Use following main method to check your method.

public static void main(String[] args)

{

String[] expression = new String[]{"{5*(x+2)+(y-1);}", "32*(20+(x[i]*3)-1", "((){([][])})", "({}((0))", "{([]})", "{}())", "{"};

for (String st: expression) System.out.println(st + " is " + isMatch(st));

}

Solutions

Expert Solution

Stack.java

import java.util.LinkedList;

public class Stack {

   private LinkedList<Character> list = new LinkedList<>();

   public void push(char ch) {

       list.add(ch);

   }

   public char pop() {
       if (list.isEmpty()) {
           System.out.println("Underflow error");
           return '\0';
       } else {

           int size = list.size();

           char ch = list.get(size - 1);
           list.remove(size - 1);
           return ch;
       }
   }

   public LinkedList<Character> getList() {
       return list;
   }

   public void setList(LinkedList<Character> list) {
       this.list = list;
   }
  
  

}

Paranthesis.java

public class Paranthesis {

   static boolean isMatch(String str) {

       char[] arr = str.toCharArray();

       Stack stack = new Stack();

       for (int i = 0; i < arr.length; i++) {

           if (arr[i] == '{' || arr[i] == '(' || arr[i] == '[')
               stack.push(arr[i]);
           //System.out.println(stack.getList().toString());

           if (arr[i] == '}' || arr[i] == ')' || arr[i] == ']') {

               if (stack.getList().isEmpty()) {
                   return false;
               }

               else {
                   char ch = stack.pop();
                   //System.out.println("Stack pop is " + ch);
                   //System.out.println("Array is " + arr[i]);

                   if (ch == '(' && arr[i] == ')')
                       continue;
                   else if (ch == '{' && arr[i] == '}')
                       continue;
                   else if (ch == '[' && arr[i] == ']')
                       continue;
                   else
                       return false;

               }
           }

       }

       if (stack.getList().isEmpty())
           return true;
       else {
           return false;
       }
   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       String[] expression = new String[] { "{5*(x+2)+(y-1);}", "32*(20+(x[i]*3)-1", "((){([][])})", "({}((0))",
               "{([]})", "{}())", "{" };

       for (String string : expression)
           System.out.println(string + " is " + isMatch(string));

   }

}

Output

{5*(x+2)+(y-1);} is true
32*(20+(x[i]*3)-1 is false
((){([][])}) is true
({}((0)) is false
{([]}) is false
{}()) is false
{ is false

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful


Related Solutions

Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds...
Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds and returns the total number of seconds For example, if the input is “0:02:20” it would return 140 If the input string is missing the hours component it should still work For example, input of “10:45” would return 645 Write an overloaded form of the method that takes in only hours and minutes and returns the total number of seconds It should leverage the...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT