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
What is the top anime shows? &lt;3
LenKa [72]

Answer:

This is personally based on my opinion.

My top 10 favorites

Toradora

Darling in the franxx

Lucky Star

My Melody

Death note

Attack on titans

One piece

The Promise neverland

Kaguya-sama: love is war

Black cover

4 0
2 years ago
Read 2 more answers
1.
aliya0001 [1]
#1 is D #2 is B #3 is A #4 is D #5 is B
3 0
2 years ago
Read 2 more answers
Typically , how do people earn income
natima [27]
People earn income by getting jobs and working. When they work, they get paid. That's an income
3 0
3 years ago
Read 2 more answers
What kind of table is a pasted Word table that can be edited with Word from within PowerPoint without changing the data in your
igomit [66]

D. Integrated

Or

A. Embedded

7 0
3 years ago
Read 2 more answers
How many suggestions available in correcting misspelled words<br>​
Basile [38]

Spelling Checker, Grammarly, and there are many assets.

5 0
3 years ago
Other questions:
  • Why is the protocol down, even though you issued the no shutdown command for interface vlan 99?
    5·2 answers
  • "Server Manager will allow you to manage all roles and features installed on any server, and view the status of all your servers
    7·1 answer
  • Write statements that output variable numComputers as follows. End with a newline
    14·1 answer
  • wHAT ARE THE 5 LAYERS in the internet protocol stack andwhat are the principal responsibilities of eack of theselayers.?
    12·1 answer
  • Which language is the most popular language for writing apple os x?
    9·1 answer
  • Directions for starting the computer and opening a word processor document
    5·1 answer
  • Suppose that we have a set of activities to schedule among a large number of lecture halls, where any activity can take place in
    8·1 answer
  • How many bytes are there in 256 Kbytes?
    6·1 answer
  • What is the biggest challenge for most business when going online?
    12·1 answer
  • Who is the father of computer​
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!