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
Extension: Cash Register Program
Evgesh-ka [11]

Answer:

hee hee 4 you are correct

Explanation:

6 0
3 years ago
What lets you do many things, like write book reports and stories?
Marina CMI [18]
A -
Application programs
8 0
2 years ago
Read 2 more answers
Which is the best web development company in Pakistan?
andriy [413]

Answer:

Arbisoft is listed as the top cpmpany.

Explanation:

The top 5 are:

Arbisoft

Cygnis Media

Tintash

Square63

OneByte

4 0
3 years ago
Write a function that receives a StaticArray where the elements are already in sorted order, and returns a new StaticArray with
Allisa [31]

The code that remove duplicate is as follows:

def remove_duplicate(mylist):

    mylist = list(dict.fromkeys(mylist))

    return mylist

print(remove_duplicate([1, 1, 2, 3, 3, 5, 6, 7]))

<h3>Code explanation</h3>

The code is written in python.

  • we defined a function named "remove_duplicate" and it accept the parameter "mylist".
  • The variable "mylist" is used to store the new value after the duplicate vallues has been removed.
  • Then, wed returned mylist.
  • Finally, we call the function with the print statement . The function takes the required parameter.

learn more on python here: brainly.com/question/21126936

7 0
2 years ago
Ssl/tls provides security ________. a. between the sender and his or her e-mail server b. all the way between the sender and the
Brrunno [24]
SSL <span>stands for Secure Sockets Layer and TLS stands for </span>its<span> successor Transport Layer </span>Security. They are protocols responsible for encryption and authentication and are  application-layer protocols. SSL/TLS provides security all the way between the sender and the receiver, which means also between the sender and his or her e-mail server . Correct answer: c. both a and b.
7 0
3 years ago
Other questions:
  • Which statement describes the word "iterative"?
    7·2 answers
  • (tco 8) when a file is opened in the append mode, the file pointer is positioned
    8·1 answer
  • How can investors receive compounding returns? A: by selecting a savings account that has a higher interest rate B: by investing
    9·2 answers
  • Which hardware component is most suspect if a user can barely make out
    10·1 answer
  • Idea citizen activation
    9·2 answers
  • What is the Slide Sorter View used for?
    12·2 answers
  • Indicate whether the statement is true or false. ____ 1. Autoglobal array elements are referred to with an index number. ____ 2.
    9·1 answer
  • Exercise : Randomizer In this exercise, we are going to create a static class Randomizer that will allow users to get random int
    14·1 answer
  • The cameras picture review function allows you to look at the _ you've taken
    8·1 answer
  • You looked at the methods used to determine database requirements. Now, research and find a database requirements template. Choo
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!