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
To drive defensively, you should _______________.
Keith_Richards [23]
The answer is B, look ahead and keep your eyes moving
7 0
3 years ago
Draw a flowchart diagram for a program that displays numbers 1 to 20
madreJ [45]
Here you go plz mark brainlist
6 0
2 years ago
Which options are available when a user modifies a recurring appointment? Check all that apply. Open this occurrence Open the fi
Rus_ich [418]

Open this occurence and open the series is the two options to choose from, from the dialog box that shows when attempting to modify the appointments

4 0
3 years ago
Read 2 more answers
What process combines data from a list with the content of a document to provide personalized documents?
Andrej [43]
The appropriate answer is d. mail merge. Mail merge uses a database of addresses that are used to create pre-addressed mailing labels that are generally used when sending letters to a very large group. This type of application is used by utility companies or any other organizations that requires mass mailings. Mail merge is found in the Microsoft Word application. Excell spreadsheets can also be used to complete tasks similar to that of mail merge.
8 0
3 years ago
Read 2 more answers
An _____ is a narrow strip of land connecting two larger land areas.
inessss [21]

Answer: isthmus

Explanation:

5 0
3 years ago
Other questions:
  • A food web is shown below. In this food web, energy is transferred directly from the to the
    11·1 answer
  • Weber believed that there is an inevitable destructive quality to which one of the four types of action?
    8·1 answer
  • . Hackers generally disguise their IP address. True False
    9·1 answer
  • In your opinion, is it more beneficial to have many folders or is it better to “nest” subfolders? Explain your response.
    10·2 answers
  • What is the name of the port that you plug an ethernet network cable into?
    6·1 answer
  • Friedman (1999) argues that "we are wrong to base health promotion in all societies on a Western framework for human development
    11·1 answer
  • In photoshop what should you consider when determining a projects purpose and objectives?
    15·1 answer
  • What is the suffix of hollow?
    7·1 answer
  • What would give Lucy, an entry-level candidate, an edge over others while she seeks a programmer’s position? Lucy, an entry-leve
    11·1 answer
  • I need help so bad it’s the entire test for EdHesive python coding Test 2
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!