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
If someone said to you, “Pseudocode is a waste of time! It’s just more work because you end up writing the same program twice,”
myrzilka [38]

Answer:

It allows the designer to focus on main logic without being distracted by programming languages syntax.

* Since it is language independent, it can be translated to any computer language code.

* It allows designer to express logic in plain natural language.

* It is easier to write actual code using pseudocode.

* Unlike algorithms, pseudocodes are concise so pseudocodes are more readable and easier to modify.

Explanation:

8 0
3 years ago
The movement of the economy through highs and lows is referred to as:
skad [1K]

Answer:

the business cycle

Explanation:

5 0
3 years ago
Complete the sentence.
mamaluj [8]

Answer:

We need context, it could be several answers

Explanation:

4 0
3 years ago
Who plays Roblox and wants to be friends on it
qaws [65]
I have roblox and I would like to be friends on it
4 0
2 years ago
Read 2 more answers
When you save a drawing using paint, it's automatically stored as a?
serious [3.7K]

Answer:

Bitmap File

Explanation:

6 0
3 years ago
Other questions:
  • Write a program that calls fork(). Before calling fork(), have the main process access a variable (e.g., x) and set its value to
    5·1 answer
  • As you are researching RAM for a computer you are building that will be used as a home office server for his small business. The
    5·2 answers
  • The most effective way to perform data entry is to keep your hands on the keyboard and press to move to the text cell in the row
    10·1 answer
  • Write a function max arguments. write a program that reads three floating-point numbers, uses the max function, and displays the
    5·1 answer
  • ______________ is only one of the marketing mix tools that acompany uses to achieve its marketing objectives.
    7·1 answer
  • What is the output of the following program when the method is called with 4?
    11·1 answer
  • Ebba received a message from one of her tech support employees. In violation of company policy, a user had downloaded a free pro
    14·1 answer
  • Write a program called DeliveryCharges for the package delivery service in Exercise 4. The program should again use an array tha
    9·1 answer
  • Convert this hexadecimal number to binary :. A07F​
    14·1 answer
  • 1. Create an optimized function "print s(n,s)" that prints the given argument s (representing a string) k times. k represents th
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!