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
In multi-grade oil what is W means?
Simora [160]

Answer:

winter viscosity grades

Explanation:

6 0
3 years ago
Where can you find the Language and Advanced features in Outlook?
igor_vitrenko [27]

Answer:

File tab, Options link.

Explanation:

Microsoft Outlook is an e-mail and a personal information manager (PIM) application software that was designed and developed by Microsoft Inc., to avail end users the ability to send and receive electronic messages on their computers.

An e-mail is an acronym for electronic mail and it is a software application or program designed to let users send texts and multimedia messages over the internet.

You can find the Language and Advanced features in Outlook by navigating to the File tab and selecting or clicking on the Options link.

4 0
3 years ago
Which two computer peripherals are connected to the computer through a port?
chubhunter [2.5K]
Most computer devices are connected to the computer through port
Keyboard through usb port
Printer through usb port
Hand point device through usb port
Also computer equipped with LPT port for printers and COM port for additional devices like external modems e.t.c
7 0
4 years ago
I CANT DO SKIN MODS ON BRAWLHALLA RIGHT!!!! IM SO MADDDDDDDDDDD
gogolik [260]

I'm better then u in brawhlla

5 0
3 years ago
Based on the pedigree chart what should you assume about the perental generation
koban [17]

Answer:

The mother was a carrier for a sex-linked disease.

Explanation:

6 0
3 years ago
Other questions:
  • What is the purpose of requirements gathering
    9·2 answers
  • How can cultural hearths be described
    5·1 answer
  • In the Dread Pirate Roberts case, U.S. Ulbricht, 31 F. Supp. 3D 54D (S.D.N.Y. 2014), one of the issues that the court addressed
    10·1 answer
  • Amazon Web Services and Microsoft Azure are some of the most widely used _______.
    7·1 answer
  • They convert energy from the Sun into usable chemical energy by the process of photosynthesis. They are
    11·1 answer
  • How do you change the number of rows and columns displayed for an embedded Excel object on a PowerPoint slide?
    8·1 answer
  • My brainly isnt working it isnt showing and answers wats going on? wat do i do
    14·2 answers
  • The type code for an int array is 'b'. What line of code creates an array of int data values?
    8·1 answer
  • Am nevoie de un referat pe tema ,,Asezarea in Pagina '' la informatica
    6·1 answer
  • Organizational behavioural aspects
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!