Question

In: Computer Science

Algorithm design:Suppose that three types of parentheses are allowed in an expression: parentheses, square brackets, and...

Algorithm design:Suppose that three types of parentheses are allowed in an expression: parentheses, square brackets, and curly braces. Please design an efficient algorithm to determine whether the parentheses in the expression are correctly paired. Please write down the algorithm idea and steps.

Please explain what kind of data structure is used to solve the problem, and then give the specific algorithm steps

Solutions

Expert Solution

1) A stack will be used to solve the problem.

Algorithm steps:

Declare a stack S.
Now traverse the input string one character at a time.
If the character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to stack S.
If the character is a closing bracket (‘)’ or ‘}’ or ‘]’) then pop from stack S.
If the popped character is the matching starting bracket then continue to next character, else parenthesis are not balanced - stop loop and return False.
After full traversal of the input string, if there is some starting bracket left in stack then return False as the string is not balanced.
Pseudocode:

function balancedbracket(inputString):
Stack S <- empty stack
for character in inputString:
if character == '(' or character == '{' or character == '[':
S.push(character)
else:
temp = S.pop()
if character == ')' and temp != '(':
return False
if character == ']' and temp != '[':
return False
if character == '}' and temp != '{':
return False
if S.empty():
return True
return False


THANK YOU!! PLEASE VOTE


Related Solutions

Algorithm design:Suppose that three types of parentheses are allowed in an expression: parentheses, square brackets, and...
Algorithm design:Suppose that three types of parentheses are allowed in an expression: parentheses, square brackets, and curly braces. Please design an efficient algorithm to determine whether the parentheses in the expression are correctly paired. Please write down the algorithm idea and steps. Please explain what kind of data structure is used to solve the problem, and then give the specific algorithm steps
Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed,...
Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed, i.e., all left brackets are matched with their right counterparts and there are no unmatched brackets between any pair of matched brackets. Note that there are generally more than one types of brackets and only those of the same type can match against each other. For simplicity, you can assume that all operands are represented as ‘a’ and addition, +, is the only available...
Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For...
Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
Devise a Turing machine to check the correct nesting of brackets (parentheses). So, your machine should...
Devise a Turing machine to check the correct nesting of brackets (parentheses). So, your machine should start with a tape with something like (()()(())) on it and halt with T printed on the tape, and it should start with something like (()()(() on the tape and halt with F. That is, correctly nested strings of brackets should give T, and wrongly nested strings of brackets should give the answer F. This example is important since it shows that Turing machines...
Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined...
Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if “(exp1)op(exp2)” is a normal, fully parenthesized expression whose operation is op, the postfix version of this is “pexp1 pexp2 op”, where pexp1 is the postfix version of exp1 and pexp2 is the postfix version of exp2. The postfix version of a sin- gle number or variable is just that number or variable. For example, the postfix version of "((5+2) * (8-3))/4"...
Answer ALL of the questions below! Circle the right expression in the parentheses. Q1. Import tariff...
Answer ALL of the questions below! Circle the right expression in the parentheses. Q1. Import tariff (a. raises, b. lowers, c, keeps constant) the domestic price and (a. always, b. sometimes, c. never) (a. raises, b lowers) the world price. 2. (Multiple choices.) A tariff in a large country tends to benefit (a. domestic producers/ b. foreign producers/ c. domestic consumers/ d. foreign consumers/ e. domestic government revenue) and hurt (a. domestic producers/ b. foreign producers/ c. domestic consumers/ d....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
Please answer ALL the questions below! Circle the right expression in the parentheses. Q1. Free trade...
Please answer ALL the questions below! Circle the right expression in the parentheses. Q1. Free trade as against trade policy can be justified with several reasons. Various forms of trade policy (a. always / b. sometimes) generate welfare loss to the economy, so that free trade is (a. always/ b. usually/ c. sometimes) more efficient than under trade policy. Optimal tariff argument goes (a. along with/ b. against) free trade where the terms of trade (a. can/ b. cannot) dominate...
These questions refer to the expression for the magnetic field at the center of the square...
These questions refer to the expression for the magnetic field at the center of the square solenoid. Assume: for all coils, each side of the square a = 4.87 cm and the length of the solenoid L = 47.8 cm. NOTE: Everything must be in MKS units! NOTE: μo = 1.26x10-6 in MKS units (determined in question 1). a) In procedure 1, you will plot Bmax vs. N, the number of turns of the coil. Suppose you set the current...
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:      1. Make a guess at the answer (you can pick n/2 as your initial guess).      2. Computer = n / guess.      3. Set guess = (guess +r) / 2.      4. Go back to step 2 until the last two guess values are within 1% of each other.Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT