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 decimal representation of the following signed binary numbers?
zheka24 [161]

Answer:

(10000000)₂ = -128 in decimal

(11001100)₂ = -52 in decimal

(10110111)₂ = -73 in decimal

Explanation:

<u>NOTE:</u> If Most Significant Bit is 0, it is positive number and if MSB is 1, it is negative number. If number is positive we can simply convert it to decimal. But if number is negative, follow following formula:

  • (complement of the number) +1

<u>EXAMPLE 1</u>

  (10000000)₂

  (01 1 1 1 1 1 1)₂   ↔  complement of the number

<u>+                    1</u>

  (10000000)₂ = 1 ×2^{7}= 128 (magnitude in decimal)

But we remember MSB was 1 so answer is -128.

<u>EXAMPLE 2</u>

  (1 1 0 0 1 1 00)₂

  (0 0 1 1 0 0 1 1)₂

<u>+                       1  </u>

  (0 0 1 1 0 1 0 0)₂ = 52 (magnitude in decimal)

But we remember MSB was 1 so answer is -52.

<u>EXAMPLE 3</u>

  (1 0 1 1 0 1 1 1)₂

  (0 1 00 1 000)₂

<u>+                      1  </u>

  (01 00 1 00 1‬)₂ = 73 (magnitude in decimal)

But we remember MSB was 1 so answer is -73.

5 0
3 years ago
It’s been six months since the disk crash at CSM Tech Publishing, and the owner is breathing a little easier because you install
Semmy [17]

Answer:

Storage Spaces

Explanation:

The feature that would best accommodate his needs would be the Storage Spaces feature that has been implemented in the Windows and Windows Server systems. This feature basically takes various hard drives and combines them together, this ultimately acts as copies of each other which protect the data of each one in the unfortunate case that one of the drives tends to fail. This feature also allows you to add more drives to the existing ones with very minimal effort without restricting configuration. Which would solve the needs that are needed in this scenario.

5 0
3 years ago
Microsoft word is an example of utility software? <br><br>A.true <br>B.false​
monitta

Answer:

false

Explanation:

Ms word is only an application software

7 0
2 years ago
Read 2 more answers
What if you have been asked to opt or design expert system or virtual reality using artificial intelligence for your daily/routi
algol13

Answer:

I would design the word asa better place

Explanation:

6 0
3 years ago
)Which of following can be thrown using the throwstatement?
Amanda [17]

Answer:

All of Given

Explanation:

The throw keywords can be used to throw any Throwable object. The syntax is :

throw <Throwable instance>

Note that Error and Exception are subclasses of Throwable while RuntimeException is a subclass of Exception. So the hierarchy is as follows:

Throwable

-- Error

-- Exception

   -- RuntimeException

And all of these are valid throwable entities. As a result "All of Given" is the most appropriate option for this question.

6 0
3 years ago
Read 2 more answers
Other questions:
  • What technology process would help you access files at all times?
    11·1 answer
  • . A program that can run more than one thread at once is called:
    9·1 answer
  • A power supply unit for a computer converts:
    7·1 answer
  • What is the sum of each pair of binary integers? (assuming 8-bit register is used)
    5·1 answer
  • What is the cell reference for the cell located in the second column and fifth row of a worksheet?
    9·1 answer
  • Which of the following statements is true about a file stream?
    5·1 answer
  • Give a recursive algorithm that takes as input a string s, removes the blank characters and reverses the string. For example, on
    7·1 answer
  • Use Spreadsheet Functions and Formulas
    6·1 answer
  • Are there any Potential Dangers in Artificial Intelligence?
    11·2 answers
  • [2]<br> (c) Describe how the microprocessor can determine when to sound the clock alarm.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!