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]
2 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]2 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
The remove() method in the Queue interface ________. Group of answer choices retrieves, but does not remove, the head of this qu
salantis [7]
<h2>Question:</h2>

The remove() method in the Queue interface ________.

A. retrieves, but does not remove, the head of this queue, returning null if this queue is empty

B. retrieves and removes the head of this queue, or null if this queue is empty

 

C. retrieves, but does not remove, the head of this queue, throwing an exception if this queue is empty

D. retrieves and removes the head of this queue and throws an exception if this queue is empty

<h2>Answer:</h2>

D. retrieves and removes the head of this queue and throws an exception if this queue is empty

<h2>Explanation:</h2>

In Java, the Queue interface is found in the java.util package and inherits from the Collection interface. It is used to store elements which are processed in the order in which they are inserted. This means that the first element will be processed first.

The Queue interface has many methods. Some of them are;

i. add(): allows elements to be added to the queue.

ii. remove(): retrieves and removes elements from the head of the queue. The head of the queue points to the first element of the queue. If the queue is empty, the remove() method throws an exception called the <em>NoSuchElementException</em>

<em />

iii. poll(): retrieves and removes the element at the head of the queue. Unlike the remove() method, if the queue is empty, a <em>null </em>is returned.

iv. peek(): retrieves but does not remove the element at the head of the queue. Just like the poll() method, a <em>null</em> is returned if the queue is empty.

7 0
2 years ago
Drag each tile to the correct box.
V125BC [204]

Answer:

Creates order and harmony → <em>color</em>

Creates an emotional impact → <em>texture</em>

Brings a feeling of depth to a design →<em> form</em>

Draws or minimizes attention → <em>space</em>

Divides space and redirects the eye → <em>shape</em>

Explanation:

The visual element that creates order and harmony is <em>color</em>

Color can be used to create visible patterns that can be recognized as being in harmony or disagreement

The visual element that creates emotional impact is <em>texture</em>

Texture can be used to convey either the emotions of the object or the artist

The visual element that brings a feeling of depth to a design is<em> form</em>

Form is used to present a 3-D appearance of an art, and therefore, it is used to show depth of a 2-D drawing

The visual element that draws or minimizes attention is <em>space</em>

The arrangement, location, and size of the space occupied by an object can be used to draw or minimize attention

The visual element that divides space and redirects the eye is <em>shape</em>

Shape is used to divide space into areas easily recognizable by the eye

5 0
2 years ago
If you ping a device, you are using the __________ protocol.
geniusboy [140]
Internet Control Messaging Protocol

If you use the -T switch, it'll be TCP
7 0
3 years ago
Read 2 more answers
What is the name of the FOLDER in which the file named "script" is contained?
yulyashka [42]

Answer:

The default location for local logon scripts is

  • the Systemroot\System32\Repl\Imports\Scripts folder

5 0
2 years ago
Help me guys pleassssssssse​
Readme [11.4K]

Answer:

is there a word bank??

Explanation:

8 0
2 years ago
Other questions:
  • 9. The best way to insert an existing Excel chart into a document is to A. use the Object command. B. click the Insert tab and c
    6·2 answers
  • Please explain external hashing, B-trees, and traversals. 3-5 sentences per
    8·1 answer
  • Which of the following should you NOT do when using CSS3 properties to create text columns for an article element? a. make the c
    12·2 answers
  • What is performance? Multiple Choice measures how quickly a system performs a process or transaction a system that is not operat
    9·2 answers
  • Tree diagrams are useful for
    12·1 answer
  • Question 4: What will be the output of the code? Show a complete analysis.
    6·1 answer
  • Please tell me what is basic HTML tags and its function please ​
    8·1 answer
  • Cerise is explaining computer viruses to her students. What is the best way for her to explain a computer virus?
    6·2 answers
  • QUESTION 4
    13·1 answer
  • ________ is the art of manipulating people into breaking normal security procedures or divulging confidential information.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!