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]
3 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]3 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
Is data science the sexiest job in 21st century according to Harvard business review?
Papessa [141]

Answer:

yuppp

Explanation:

i looked it up lol

3 0
3 years ago
What is the binary for O?​
nevsk [136]

Answer:

01001111

Explanation:

8 0
3 years ago
Read 2 more answers
Someone posing as an IT tech requests information about your computer configuration. What kind of attack is this
ra1l [238]

Answer:

Phishing

Explanation:

Phishing is one of the most common social engineering attacks in which the attacker disguises to be a trustworthy personnel in order to lure the victim or target into disclosing sensitive information such as passwords, credit card details and so on.

Phishing could be carried out via emails, telephones, or even text messages.

In this case, the attacker pretends to be an IT tech in order to get your computer configuration details which he can then use to carry out some other fraudulent acts.

Another example is in the case of someone receiving an email from their bank requesting that they need to update their records and need the person's password or credit card PIN.

8 0
4 years ago
Which of the following help departments organize unique access controls for access to folders and data files within a department
never [62]

Answer:

B. Role-based access controls

Explanation:

Roll-based access control is a way to control access to the network and to data which depends on the position of employees in a department. This way the permission to access information are granted according to roles that are assigned to employees. This type of access control allows access privileges to employees only to the folders and data files which they need in order to perform their tasks and it does not allow them to access the data or information which is not relevant to them. This helps the department to secure the sensitive information and data files and reduces the risk of data breaches.

6 0
3 years ago
Explain why decomposition will be used in creating the algorithm for the game including two dice.
RoseWind [281]
I saw this question while reviewing the notes in class
7 0
3 years ago
Other questions:
  • Assign a pointer to any instance of searchChar in personName to searchResult.#include #include using namespace std;int main() {c
    5·1 answer
  • The two variables causing the point of difference between the time codes are the frequency and the count
    6·1 answer
  • If you had a chance to see what your life would be like in 20 years
    8·1 answer
  • Intuitively, which of the following would happen to e⃗ net if d became very large?
    6·1 answer
  • What are the disadvantages of plasma display?
    9·1 answer
  • Which of the following is the most effective password?
    15·2 answers
  • Assume you need to declare a type parameter that can represent any type that implements the interface Comparable. Which type par
    15·1 answer
  • WILL MARK BRAINLIEST!!!!!!!!!!!
    5·2 answers
  • Points! taga pilipinas ba kayo?​
    10·1 answer
  • Use the drop-down menus to complete each statement. Two main versions of Outlook are the desktop app and the app. The has limite
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!