1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mrs_skeptik [129]
3 years ago
14

30 points) Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to

determine whether the string has balanced parentheses. Your algorithm should use no more than O(1) space beyond the input; any algorithms that use space not in O(1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero credit. Balanced parentheses means that every open parenthesis has exactly one close parenthesis corresponding to it and vice versa, and that for each pair the opening parenthesis precedes the closed parenthesis. The follow- ing strings have balanced parentheses: () (()()) ((())()) 1 The following strings do not have balanced parentheses: )( (() ())(()))()) We consider the empty string to have balanced parentheses, as there is no imbalance. The input to hasBalancedParantheses will always be a string only containing "(" or ")". Your task is to complete the has Balanced Parentheses() method. Note: this can be done both iteratively or recursively, but I be- lieve most people will find the iterative version much easier. Hint: There’s a pattern for how to determine if parentheses are balanced. Try to see if you can find that pattern first before coding this method up.

Computers and Technology
1 answer:
navik [9.2K]3 years ago
5 0

Answer:

The screenshot is attached below

Explanation:

The below program check balence of paranthesis using stack.

if the input charecter is ( then push to stack

if the input charecter is ) then pop top element and compair both elements for match.

Program:

public class Main

{

static class stack // create class for stack

{

int top=-1; // initialy stack is empty so top = -1

char items[] = new char[50]; // Create an array for stack to store stack elements

 

void push(char a) // Function push element to stack

{

if (top == 49) // chack stack is full ?

{

System.out.println("Stack full");

}

else

{

items[++top] = a; // increment the array index and store the element

}

}

 

char pop() // function to return the elements from stack

{

if (top == -1) // check the stack is empty

{

System.out.println("Empty stack");

return '\0';

}

else

{

char element = items[top]; // return the top element

top--; // Decrement the array index by one

return element;

}

}

 

boolean isEmpty() // Check the stack is empty or not

{

return (top == -1) ? true : false;

}

}

static boolean isMatch(char character1, char character2) // Check the input charecters are matching pairs or not

{

if (character1 == '(' && character2 == ')') // If they match return true

return true;

else

return false;

}

 

static boolean isBalanced(String parantheses) // check the input string is balenced or not

{

char[] exp = new char[parantheses.length()]; // Create a charecter array

for (int i = 0; i < parantheses.length(); i++) { // Convert the string parantheses to charecter array

exp[i] = parantheses.charAt(i);

}

stack st=new stack(); // Declare an empty character stack    

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

{  

if (exp[i] == '('))   // if input charecter is '(' push to stack

st.push(exp[i]);

if (exp[i] == ')') // if input charecter is ')' pop top element from stack

{

if (st.isEmpty())

{

return false;

}

 

else if ( !isMatch(st.pop(), exp[i]) ) // Call isMatch function

{

return false;

}

}

 

}

 

if (st.isEmpty())

return true; //balanced

else

{

return false; //not balanced

}

}

 

 

public static void main(String[] args)

{

 

System.out.println(isBalanced("()()()")); // Output true if string is balenced

 

}

 

}

Screenshot:

You might be interested in
Ai biết cách tra code đáp án trên LMS, Moodle/Quiz không ạ??
mariarad [96]

Answer:

không nhé học đi bạn đừng có cheat hỏng người đấy

Explanation:

4 0
3 years ago
The Gas-N-Clean Service Station sells gasoline and has a car wash. Fees for the car wash are $1.25 with a gasoline purchase of $
Vesnalui [34]

Answer:

Amount Gasoline purchases                       9.7 Gallons

Price pre gallons                             $          2.89

Total gasoline cost                          $          28.03

Car wash cost                                  $          1.25

Total due                                         $          29.28

Explanation:

hope this helps

5 0
3 years ago
The greatest common divisor of two positive integers, A and B, is the largest number that can be evenly divided into both of the
lapo4ka [179]

Explanation: Surface Level

5 0
3 years ago
One tool under What-If-Analysis is Scenarios. Which best describes the purpose of setting up and using scenarios
nika2105 [10]

Answer:

Option B, to instantly compare results when data in numerous cells change to specified values

Explanation:

Scenario setting with in the What If Analysis is used to compare various scenarios with in the same excel sheet. Scenarios has set of values that are saves and updated automatically as the values with in the cell changes. Also the scenarios can be switched.

Hence, option B is correct

7 0
3 years ago
How to check if students viewed an assignment canvas.
Greeley [361]
Which app are you using?
6 0
2 years ago
Other questions:
  • Write 4 types of network_based<br>on geographical area covered<br>by them​
    15·1 answer
  • Which of the following items is not found on a web page?
    9·1 answer
  • The program has a compilation error because s is not initialized, but it is referenced in the println statement. The program has
    8·1 answer
  • In this assignment, you are provided with working code that does the following: 1. You input a sentence (containing no more than
    14·1 answer
  • What type of link is used to call this file?
    8·1 answer
  • A customer reports that recently several files cannot be accessed. The service technician decides to check the hard disk status
    11·1 answer
  • Which database item would show details such as a customer’s name, their last purchase, and other details about a customer?
    11·2 answers
  • Exercise #3: Write a c++ program that reads, from a file called numbers.in, up to 10 numbers, counts and adds the positive value
    10·2 answers
  • What is your favorite anime?
    11·1 answer
  • Please explain what is Ribbon?  give examples​
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!