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
The function of the file server is to : 1. store data and software programs that can be used by client computers on the network.
mylen [45]

Answer:

1. store data and software programs that can be used by client computers on the network.

Explanation:

8 0
3 years ago
Why are coders using encoder software? What are their advantages and disadvantages? And when might a coder need to use the codin
IrinaVladis [17]
<span>Coders using encoder software:
To enhance their productivity</span><span>
Speed and Efficiency
</span><span>Accuracy and consistency
</span>
Encoding is done to reduce the number of bit to be transmitted and save bandwidth .can be fairly complex and contain some delicate parts. This makes them less tolerant of mechanical abuse and restricts their allowable temperature.

<span>including increased productivity, accuracy, and efficiency in coding and the consistent application of coding rules. However, there is a cost to the software, but savings can be seen in other areas.</span>

7 0
2 years ago
Sam works in a real estate office. An offer comes in on a house but the homeowners are on vacation. He needs the homeowners to s
Digiron [165]

Answer:

Sam should send a digital document and request a signature. The could be fax or even an image printed out, if neither is possible get in contact with eh owner and his lawyer for more help.

Explanation:

3 0
2 years ago
Python 3.8.2 detais
olga2289 [7]

Answer:

me dont know

Explanation:

5 0
2 years ago
Overloaded methods must be differentiated by: Select one: a. method name b. data type of arguments c. method signature d. None o
Zarrin [17]

Answer:

b. data type of arguments

Explanation:

One of the ways to overload a method is using different type of arguments. Let's say we have a method that finds and returns two integer values

public int sumValues(int num1, int num2){

       return num1 + num2;

}

We can overload this method by passing double values as arguments

public double sumValues(double num1, double num2){

       return num1 + num2;

}

5 0
2 years ago
Other questions:
  • Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Part I so that it throws an InvalidTes
    10·1 answer
  • Which of the following characteristics relates to authentication header (AH)? It is a document that defines or describes compute
    11·1 answer
  • An app builder has created a report for sales people to view records from the custom object, some users have complained that the
    8·1 answer
  • Instructions
    12·1 answer
  • Can someone please type a code that makes a house in python and turtle graphics i need help
    9·1 answer
  • Select the correct answer from the drop-down menu.
    15·2 answers
  • Write a function that accepts a positive random number as a parameter and returns the sum of the random number's digits. Write a
    10·1 answer
  • In two to three sentences, define "home row" and explain why it is important.
    6·2 answers
  • What is wrong with the following code? publicstatic char grade(int score) { if (score &gt;= 9) { return 'A'; } else if (score &g
    6·1 answer
  • It would be at least two decades before some of
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!