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
How can a robot differentiate between positive or negative product reviews?
Levart [38]

Answer:

Some of the sites have the rating system for example 3 stars, or the bot has a feature that can capture keywords that include a list of possible indicator that it is a negative review. For example if a user review includes a word "bad" or "horrible" the bot will flag that as a negative review.

Please give brainliest.

3 0
2 years ago
In what year did alienware introduce alienfx lighting zones into their pc's?
ser-zykov [4K]
The answer to the question asked  above is 2006. 
<span>Alienware introduce alienfx lighting zones into their pc's on 2006.</span>

Hope my answer would be a great help for you.    If you have more questions feel free to ask here at Brainly.
8 0
3 years ago
What are chemical manufacturers required to provide to anyone who purchases or uses their chemicals for a work purpose? Select a
Julli [10]

Answer:

Labels and Safety Data Sheets.

Explanation:

Label is a short description of what is inside the container. And Safety Data Sheets has everything that is required. You have precautionary measures listed as well as the training details in the safety data sheets. The training PPE is hence not required definitely. However, the only other thing required is the label, as without that you will not come to know what is inside the container from outside. Hence, the above answer.

8 0
3 years ago
Read 2 more answers
You have important data on your hard drive that is not backed up and your Windows installation is so corrupted you know that you
love history [14]

Answer:

Make every attempt to recover the data

Explanation:

If your decide to format the drive, use system restore or reinstall the windows OS you may end up loosing all your data so the best option is to try as much as you can attempting to recover the data because the data might or definitely will be lost while using other options you think are available.

8 0
3 years ago
Developers have proposed to build a subdivision near the shore of a lake, but this would require altering a popular wetland area
ad-work [718]
<span>They would argue that a a healthy, sustainable environment is necessary for humans. </span>Anthropocentrism is a belief that <span>interprets or regards the world in terms of human values and experiences. They see things as how they affect humans. They want to preserve the wetland so it could help humans.</span>
3 0
3 years ago
Other questions:
  • When you save a Notepad file for the first time, you must also A. print a copy for your records. B. give it a name. C. check the
    11·1 answer
  • What was the first carbonated drink to be introduced in the US?
    6·1 answer
  • Which type of market are you in if your firm, along with three other firms, controls 95% of the total music industry?
    13·1 answer
  • Which of the following is not a true statement? You can import a table or a query directly from an Access database into an Excel
    15·1 answer
  • The computer program that Josh is working on presents him with a sentence in which a word has been underlined. Josh has to indic
    9·1 answer
  • ___________ is a technique that uses IT to develop an ongoing relationship with the purchasers or buyers of an organization's pr
    13·1 answer
  • Attackers need a certain amount of information before launching their attack. One common place to find information is to go thro
    11·1 answer
  • What is the school wifi password trying to do my class work in school on different devices.
    9·1 answer
  • What would be a social networking page background for Sigmund Freud?
    15·1 answer
  • Help please match them if you just put a link or say “I don’t know but thanks for the points” I’ll report your answer and you wo
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!