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
Marina86 [1]
3 years ago
12

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

hether 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 following strings have balanced parentheses: () (()()) ((())())
The following strings do not have balanced parentheses: )( (() ())(()))())
We consider the empty string to have balanced parentheses, as there is no imbalance. Your program should accept as input a single string containing only the characters ) and (, and output a single line stating true or false.

Your task is to complete the hasBalancedParentheses() method.
Computers and Technology
1 answer:
Whitepunk [10]3 years ago
8 0

Answer:

Code is completed below

Explanation:

Source Code in Java:

class Parenthesis

{

boolean hasBalancedParentheses(String eq) //method to check and return if a set of parenthesis is balanced or not

{

int count=0; //to store count of current unclosed opening brackets

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

{

if(eq.charAt(i)=='(')

count++;

else if(eq.charAt(i)==')')

count--;

if(count<0) //if count falls below zero, there were more closing brackets than opening brackets till this point

return false;

}

if(count>0) //if count is still more than zero, there were less closing brackets than opening brackets

return false;

return true; //if program reaches this point, it has passed all the tests

}

public static void main(String args[])

{

//testing the function

Parenthesis ob=new Parenthesis();

System.out.println(ob.hasBalancedParentheses("()(()())((())())"));

System.out.println(ob.hasBalancedParentheses(")((()())(()))())"));

}

}

You might be interested in
Mark of athena ar answers
Dmitriy789 [7]

Answer:

true

Explanation:

thats how I understood it

7 0
4 years ago
Mai lee wants to add a graphic demonstrating a process in her document. which of the following commands should she click in the
Aleks04 [339]

Answer:

B. SmartArt

hope that helped <3

4 0
3 years ago
Where should fire extinguishers be stored on a boat?
fomenos

<span>Fire extinguishers should be stored in a boat at the engine area and near the operator. A fire extinguisher should be put near the engine because the engine of the boat is a fire hazardous place and this is the area where a fire could start.  The fire extinguisher should be installed properly using a mounting bracket to hold the extinguisher in place. There are different types of extinguishers that should be installed in the boat, these are type A, B, and C extinguishers. Type A extinguishers can turn off the fire caused by combustible solids like wood and paper. Type B extinguishers can turn off fire caused by gasolines, oil, and greases. While Type C extinguishers can turn off fire caused by an electrical problem.</span>

5 0
3 years ago
Determine the distance between point (x1, y1) and point (x2, y2), and assign the result to points Distance. The calculation is:
Nadusha1986 [10]

<u>Question:</u>

Snapshot of the question has been attached to this response.

<u>Answer:</u>

<u></u>

import java.util.Scanner;

import java.lang.Math;

public class CoordinateGeometry{

    public static void main(String []args){

       double x1 = 1.0;

       double y1 = 2.0;

       double x2 = 1.0;

       double y2 = 5.0;

       double pointsDistance = 0.0;

       

       //Declare a variable to hold the result of (x2 - x1)²

       double x;

       

       //Solve (x2 - x1)^2 and store result in the variable x

       x = Math.pow(x2 - x1, 2);

       

       //Declare a variable to hold the result of (y2 - y1)²

       double y;

       

       //Solve (y2 - y1)^2 and store result in the variable y

       y = Math.pow(y2 - y1, 2);

       

       //Now pointsDistance = SquareRootOf(x + y)

       pointsDistance = Math.sqrt(x + y);

       

       System.out.println("Points distance: ");

       System.out.println(pointsDistance);

       

       return;

    }

}

<u>Sample Output:</u>

Points distance:  

3.0

<u>Explanation:</u>

The above code has been written in Java and it contains comments explaining important lines of the code. Please go through the comments.

The snapshots of the program and a sample output have been attached to this response.

4 0
4 years ago
Please help me!! 10 Points are waiting!!
prisoha [69]

Google

https://google.com

Google has a large index of keywords and where those words can be found. Google uses a trademarked algorithm called PageRank, which assigns each Web page a relevancy score.

3 0
3 years ago
Other questions:
  • Steps needed to deposit cash on cash deposit machine
    6·1 answer
  • Which process improves the life of a computer?
    11·1 answer
  • Identify the parts used in an electric circuit- Tiles
    9·2 answers
  • How many frames per second can a half duplex traditional gigabit ethernet handle?
    12·1 answer
  • Routing protocols that enable routers to communicate beyond neighboring routers, allowing each router to independently map the n
    7·1 answer
  • WILL MARK BRAINIEST
    12·1 answer
  • Which of the following BEST describes Computer Science (CS)?
    8·1 answer
  • What is one similarity between low-and high-level programming languages? (5 points)
    14·1 answer
  • Differentiate the term, "bundling," as applied to a Mac/Apple computer and a PC.
    5·1 answer
  • You must configure a certificate authority on your network to use EFS. True or False?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!