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
Kelly is working on creating a rocket-focused game in Scratch and is ready to add some sound. What should Kelly do if she wants
Alja [10]

Answer:i think she should add a new sound from the soundtab and use the start sound block to attach her sound to the rocket.

Explanation:

8 0
3 years ago
Read 2 more answers
When creating an ad, how does Greg know what it will look like to his potential customers?
gladu [14]

Answer:

Greg can see previews of his ad in the Preview tab of his Google My Business account.

Google My Business is a free tool for businesses to manage their online existence across Google including Search and Maps. It also provides them to edit their business information which helps customers to find their business. They can also find how many customers searched for their business.

So once Greg's ad has been officially accepted, he can enter the specific keywords he’s targeting to have a generic view  of the ad in the browser. Google gives examples of desktop ads using the keywords selected in the campaign to make a general preview.  As Greg types his URL, headline, and description, a generic preview of mobile and desktop versions of his ad will show up.

3 0
3 years ago
What are the four steps of the research process?
pishuonlain [190]

Answer:

Step 1: Identify the Problem

Step 2: Review the Literature. ...

Step 3: Clarify the Problem Step 4: Clearly Define Terms and Concepts.

7 0
3 years ago
Which of the following best describes the primary function of software applications?
e-lub [12.9K]
The answer is that it is to perform specific operations for various applications. These functions include writing reports, creating spreadsheets, manipulating images, keeping records and developing websites and calculating expenses.
7 0
3 years ago
Read 2 more answers
Hello,
tekilochka [14]
I think you should get used input
5 0
3 years ago
Other questions:
  • Kumar was working on his term paper and had not saved his work before the battery died on his laptop. He panics because the pape
    12·1 answer
  • Question 9. which describes an str?
    10·1 answer
  • Declare k, d, and s so that they can store an integer, a real number, and a small word (under 10 characters). Use these variable
    13·1 answer
  • Please help again if anyone doesn't mind
    15·1 answer
  • In a Windows environment, __________ is a powerful tool that enables security administrators to share user and group definitions
    10·1 answer
  • Flash drives, CDs, external disks are all examples of storage (memory) devices.'True or false?
    9·1 answer
  • ap csp The local, remote, and upstream _______ can each have multiple ___ _____. When a participant in a collaborative group on
    5·1 answer
  • Help please
    13·2 answers
  • Whenever you press a key, click the mouse or start an application, you're sending instructions tow
    9·1 answer
  • The identification of the technology management framework contain?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!