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
Does learning swift is hard? more than android?
ladessa [460]
No it is not. it's quite simple when you get the hang of it
4 0
3 years ago
What makes for a good algorithm compatibility complexity humor correctness efficiency?
Svetlanka [38]
Correctness is key. The other ones are optimizations at most. Although I would always include humor... ;-)
5 0
3 years ago
our client, Rhonda, has come to you for advice. Rhonda has recently graduated from college and has a good job with a large compa
Radda [10]

Answer:

See explaination for how to manage her personal risk

Explanation:

Personal risks can be described as anything that exposes you to lose of money. It is often connection to financial investments and insurance.

The basic things She can do to manage her personal risks are:

1. Saving:

Savings in much ways drastically reduces the percentage of risks and help you build confidence. Savings can help Rhonda manage her personal risks as savings helps one become financially secure and provide safety in case of emergency.

2. Investing:

After savings comes the major process, which is investment. It is rightly said, savings without invested proper is vain. Investment not only gives you returns or generates more profits but also ensures present and future long term financial security.

3. Reduce expenses:

A common man's expenses can never finish except it is controlled. Reduction in daily expenses can give a hike in savings and increase return on investment. Prompt planning can help cut in expenses.

5 0
3 years ago
What did Peter and Rosemary do for a living?<br>​
marta [7]

Answer:

Peter and Rosemary Grant are distinguished for their remarkable long-term studies demonstrating evolution in action in Galápagos finches. They have demonstrated how very rapid changes in body and beak size in response to changes in the food supply are driven by natural selection.

Explanation:

please

6 0
3 years ago
The Gramm-Leach-Bliley Act contains a rule that ensures security and confidentiality of customer information, protects against a
ValentinkaMS [17]

Answer:

b. The Safeguards Rule

Explanation:

According to a different source, these are the options that come with this question:

a. The Information Assurance Rule

b. The Safeguards Rule

c. The Safety Rule

d. The Guardian Rule

This rule is called the <em>Safeguards Rule</em>, and it comes from the Gramm–Leach–Bliley Act (GLBA), also known as the Financial Services Modernization Act of 1999. This is an act of Congress signed by President Bill Clinton that removed barriers among banking companies, securities companies and insurance companies. This meant that organizations such as commercial banks, investment banks, securities firms, and insurance companies were able to consolidate.

7 0
3 years ago
Read 2 more answers
Other questions:
  • Recall that book ciphers do not necessarily require a full book to decode, but instead any written text, such as the Declaration
    6·1 answer
  • Why are video texts an example of multimedia? A. They use audio and visual elements together. B. They use one type of medium to
    13·2 answers
  • Name the different type of vegetation found in India ?​
    8·2 answers
  • Who PLAYS Apex Legend?
    10·2 answers
  • Consider the classes below: public class TestA { public static void main(String[] args) { int x = 2; int y = 20 int counter = 0;
    8·1 answer
  • What percentage of business are using social media today
    14·1 answer
  • Xercise 1<br>1.<br>What is system? Explain the components of system in brief.​
    12·1 answer
  • Hello,
    13·1 answer
  • Fill in the Blank
    10·2 answers
  • The Power of If Worksheet
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!