Question

You are given a string containing a sequence of open and close brackets. and isOpen, isClose,...

You are given a string containing a sequence of open and close brackets. and isOpen, isClose, isMatching methods as defined below. Write a recursive algorithm in pseudocode for the method isBalanced that takes the  string and a stack of characters as the input and checks if the given input string contains balanced brackets. For example "(][)" would be considered not balanced where "[()]" would be considered balanced.

The recursive isBalanced method utilizes  isOpen, isClose, isMatching methods as defined below.

static String open = "([<{";
static String close = ")]>}";

static boolean isOpen(char ch)
{
    return open.indexOf(ch) != -1;
}

static boolean isClose(char ch)
{
    return close.indexOf(ch) != -1;
}

static boolean isMatching(char chOpen, char chClose)
{
    return open.indexOf(chOpen) == close.indexOf(chClose);
}

static boolean isBalanced(String input, String<Character> stack) 
{

// write pseudocode for the recursive algorithm here

}

Homework Answers

Answer #1
static boolean isBalanced(String input, String<Character> stack) 
{
    if input[0] == '\0' //end of String
    {
        if (stack.isEmpty())
            return true;
        else
            return false;
        
    }
    if (isOpen(input[0]))
    {
        stack.push(input[0])
        return isBalanced(input[1:length(input)],stack)
    }
    else if(isClose(input[0]))
    {
        if (isMatching(stack.pop(),input[0]))
            return isBalanced(input[1:length(input)],stack)
        else
            return false;
    }
    
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below: f o o --------------------------------------------------------------------- public class StringExplode { // TODO - write your code below this comment. // You will need to write a method...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
This must be answered not advance methods, focusing on String method. We are working on Ch...
This must be answered not advance methods, focusing on String method. We are working on Ch 9 in Think Java 1st Ed.I need the program to be bare bones and the coding need to be done the long way with no advanced code. in this lab you will have two methods with headings: - public static int countNumberSigns(String tweetText) - public static int countHashtags(String tweetText) 'String tweetText' means the method is expectiong a string value as an input to it....
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
Given the following method: public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) ==...
Given the following method: public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) == s.charAt(1) || check(s.substring(1))); } This method will return true if and only if: 1. s.charAt(0) == s.charAt(1) 2. s starts with two or more of the same characters 3. s ends with two or more of the same characters 4. s contains two or more of the same characters 5. s contains two or more of the same character in a row
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
You are given the following Java files: TestCharacterStack.java that contains class LLStack with class stackNode listed...
You are given the following Java files: TestCharacterStack.java that contains class LLStack with class stackNode listed as inner class.    You are given the following Java file: TestCharacterStack.java inside the class add the method below:         public boolean ParenthesesMatching (String text) takes a string text, and parenthesis whether its parenthesis are "balanced." Hint: for left parenthesis, push onto stack; for right parenthesis, pop from stack and check whether popped element matches right parenthesis, else return false. If returned value is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • To the TwoDArray, add a method called transpose() that generates the transpose of a 2D array...
    asked 13 minutes ago
  • How could your result from GC (retention time, percent area, etc.) be affected by these following...
    asked 23 minutes ago
  • QUESTION 17 What are the tasks in Logical Network Design phase? (Select five. ) Design a...
    asked 25 minutes ago
  • What is the temperature of N2 gas if the average speed (actually the root-mean-square speed) of...
    asked 34 minutes ago
  • Question One: Basic security concepts and terminology                         (2 marks) Computer security is the protection of...
    asked 47 minutes ago
  • In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the...
    asked 55 minutes ago
  • the determination of aspirin in commercial preparations experment explain why the FeCl3-KCl-HCl solution was ased as...
    asked 1 hour ago
  • Describe important events and influences in the life of Wolfgang Amadeus Mozart. What styles, genres, and...
    asked 1 hour ago
  • 3.12 Grade Statistics Write a python module "school.py" that prints school information (first 3 lines of...
    asked 1 hour ago
  • Using python, please explain. The factorial of an integer N is the product of the integers...
    asked 1 hour ago
  • alamoto Co. manufactures a single product that goes through two processes — mixing and cooking. The...
    asked 1 hour ago
  • QUESTION 21 _______ is the ease of use with which network users can access the network...
    asked 1 hour ago