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
Describe the functions of a system software​
wel

Answer:

System Software is a set of programs that control and manage the operations of computer hardware. It also helps application programs to execute correctly. System Software are designed to control the operation and extend the processing functionalities of a computer system

8 0
3 years ago
Read 2 more answers
Good Morning! Please Help!
vova2212 [387]

Answer:

cyber bullying

rights

technology

speech

education

study

issues

Explanation:

4 0
3 years ago
A(n) _____ is money paid for work.<br><br> A. raise <br> B. allowance<br> C. wage<br> D. grant
Andreas93 [3]

Answer:

wage

Explanation:

3 0
3 years ago
Read 2 more answers
_____ provide a description of the data characteristics and the set of relationships that link the data found within the databas
Karolina [17]

Answer:

The correct answer to the following question will be "Metadata".

Explanation:

Metadata is "information providing information on other data" This is "data about data", in other words.

There are many subareas of metadata, including:

  • Structural metadata
  • Iscriptive metadata
  • Statistical metadata
  • Reference metadata
  • Administrative metadata

Metadata gives a summary of the features of the data and the collection of relationships that connect the data found in the database.

It defines other details, is a prefix in most uses of computing means "an underlying meaning or description. Very simple document metadata are, for example, creator, date generated and date changed and file size.

Therefore, Metadata is the right answer.

6 0
3 years ago
A friend complains that she is always running out of money even though she “never buys anything expensive.” What advice would yo
Paraphin [41]
To not buy anything unless it is 100% necessary.
4 0
3 years ago
Read 2 more answers
Other questions:
  • Describe a situation where it would be advantageous to use the copper tape or aluminum foil instead of the A and B push buttons
    7·2 answers
  • The technology (software) that automatically downloads website information to your computer is called ________.'
    15·1 answer
  • What would you use to compare two date ranges in a report?
    9·1 answer
  • The phrase has become an Internet buzz word. It refers to a top-level domain name for communications organizations.TrueFalse
    12·1 answer
  • Then standard toolbar appears whenever you select text true or fals
    15·1 answer
  • Green Field county stadium is planning to conduct a cricket match between two teams A and B. A large crowd is expected in the st
    6·1 answer
  • Which term describes the distance from one point on a wave to the same point on the next wave?
    13·2 answers
  • Bao bì chủ động active packaging và bao bì thông minh intelligent packaging khác biệt như thế nào
    15·1 answer
  • A ____________ is a collection of commands given a name.
    11·1 answer
  • Procedural programs specify instructions that a processor must execute to perform computations. These programs sometimes consist
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!