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
mrs_skeptik [129]
2 years ago
14

30 points) Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to

determine whether 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 follow- ing strings have balanced parentheses: () (()()) ((())()) 1 The following strings do not have balanced parentheses: )( (() ())(()))()) We consider the empty string to have balanced parentheses, as there is no imbalance. The input to hasBalancedParantheses will always be a string only containing "(" or ")". Your task is to complete the has Balanced Parentheses() method. Note: this can be done both iteratively or recursively, but I be- lieve most people will find the iterative version much easier. Hint: There’s a pattern for how to determine if parentheses are balanced. Try to see if you can find that pattern first before coding this method up.

Computers and Technology
1 answer:
navik [9.2K]2 years ago
5 0

Answer:

The screenshot is attached below

Explanation:

The below program check balence of paranthesis using stack.

if the input charecter is ( then push to stack

if the input charecter is ) then pop top element and compair both elements for match.

Program:

public class Main

{

static class stack // create class for stack

{

int top=-1; // initialy stack is empty so top = -1

char items[] = new char[50]; // Create an array for stack to store stack elements

 

void push(char a) // Function push element to stack

{

if (top == 49) // chack stack is full ?

{

System.out.println("Stack full");

}

else

{

items[++top] = a; // increment the array index and store the element

}

}

 

char pop() // function to return the elements from stack

{

if (top == -1) // check the stack is empty

{

System.out.println("Empty stack");

return '\0';

}

else

{

char element = items[top]; // return the top element

top--; // Decrement the array index by one

return element;

}

}

 

boolean isEmpty() // Check the stack is empty or not

{

return (top == -1) ? true : false;

}

}

static boolean isMatch(char character1, char character2) // Check the input charecters are matching pairs or not

{

if (character1 == '(' && character2 == ')') // If they match return true

return true;

else

return false;

}

 

static boolean isBalanced(String parantheses) // check the input string is balenced or not

{

char[] exp = new char[parantheses.length()]; // Create a charecter array

for (int i = 0; i < parantheses.length(); i++) { // Convert the string parantheses to charecter array

exp[i] = parantheses.charAt(i);

}

stack st=new stack(); // Declare an empty character stack    

for(int i=0;i<exp.length;i++)

{  

if (exp[i] == '('))   // if input charecter is '(' push to stack

st.push(exp[i]);

if (exp[i] == ')') // if input charecter is ')' pop top element from stack

{

if (st.isEmpty())

{

return false;

}

 

else if ( !isMatch(st.pop(), exp[i]) ) // Call isMatch function

{

return false;

}

}

 

}

 

if (st.isEmpty())

return true; //balanced

else

{

return false; //not balanced

}

}

 

 

public static void main(String[] args)

{

 

System.out.println(isBalanced("()()()")); // Output true if string is balenced

 

}

 

}

Screenshot:

You might be interested in
What is Patch tool ???<br><br>​
AfilCa [17]

Answer:

<u><em>DEAR HERE IS YOUR ANSWER:</em></u>

<u><em>T</em></u>he Patch Tool is part of the healing brush set of tools. These are the go-to tools for retouching and repairing your images. The Patch Tool is primarily used to repair larger areas of an image, or get rid of any distractions or blemishes.

  • The patch tool was introduced into Photoshop at the same time as the Healing Brush
  • You don’t have to use the Patch tool to define a selection. You can use any selection tool and then select the Patch tool.  <em><u> </u></em>

<em><u>HOW TO USE PATCH TOOL IN PHOTOSHOP:</u></em>

  1. Click and hold the Healing Brush tool to select the Patch tool; on the Options bar, select the Destination radio button
  2. With the Patch tool still selected, drag to create a marquee around the source you want to use as the patch
  3. After you create the marquee, drag the selected source area to the destination to be repaired.

<em><u>I HOPE IT WILL WORK DEAR THANKS FOR ASKING  QUESTION</u></em>

Explanation:

4 0
2 years ago
Read 2 more answers
Does anyone know how to write this right? This is for a coding class and I’m super confused on it.
vovangra [49]

Answer:

This is using c++ syntax, you might need to make slight adjustment for other languages.

First activity:

string firstSnack = "chips";

string secondSnack = "pizza";

string thirdSnack = "apples";

string bestSnack =  firstSnack;

bestSnack = secondSnack;

Second activity:

double apple = 0.5;

double banana = 0.75;

double orange = 1.43;

double total = apple + banana + orange;

Explanation:

When first declaring a variable, you want to specify the type (such as int, double, string, bool, etc.) and then the name. You can set the variable value in the declaration, or you can set it to a value later in the program by not having the equals sign and whatever comes next.

4 0
2 years ago
The latest form of personal communication that most resembles a public journal is a _____.
Airida [17]
The answer is b becuase a daily blog is a daily journal
8 0
2 years ago
Read 2 more answers
What is the python print of (“5”+”7”)?
Firdavs [7]

Answer:

57

Explanation:

Since the 5 and the 7 don't represent integers instead they represent strings so it would come out as 57 hope I helped!

4 0
3 years ago
Which statements best describe slaves in ancient times? Select all that apply.
Romashka-Z-Leto [24]

Answer:

Slaves are the people thought as conquered People

Explanation:

In ancient times, slaves works without any remunerations. These people are captured or purchased by rich peoples. Their whole life spends in slavery. The people who are also prisoners of wars worked as slaves for their rest of the life.

5 0
2 years ago
Other questions:
  • Ned and mary ann are saving their files to a cd
    15·1 answer
  • Cyberlaw consists of: a. only state statutes. b. only federal statutes. c. traditional legal principles that have changed becaus
    6·1 answer
  • How many cells does the organism have?\
    5·2 answers
  • analyze the ethical issues in the use of information technology in Multinational company (MNC) and support you answer with one e
    15·1 answer
  • In your own words, describe how a network administrator can use the OSI model to isolate a network problem.
    13·1 answer
  • What does the statement that follows do? double gallons[6] = { 12.75, 14.87 }; a. It assigns the two values in the initializatio
    13·1 answer
  • Difference between automated testing and manual testing
    13·1 answer
  • Choose all stages of the information processing cycle.
    12·2 answers
  • How to determine the critical crack size for a<br> plate
    15·1 answer
  • the ghost adventures team uses a variety of tools and technology in investigations. which one is described as an adjustable freq
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!