adplus-dvertising
frame-decoration

Question

Write a piece of code which returns true if the string contains balanced parenthesis, false otherwise.
a)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
        {
  char ch = exp.charAt(i);
                if (ch == '(')
                stk.push(i);
                else if (ch == ')')
                {
   if(stk.peek() == null)
                        {
    return false;
   }
   stk.pop();
  }
 }
 return true;
}

b)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
            {
  char ch = exp.charAt(i);
                if (ch == '(')
                stk.push(i);
                else if (ch == ')')
                {
   if(stk.peek() != null)
                        {
    return true;
   }
   stk.pop();
  }
 }
 return false;
  }

c)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
        {
        char ch = exp.charAt(i);
               if (ch == ')')
               stk.push(i);
               else if (ch == '(')
               {
   if(stk.peek() == null)
                        {
    return false;
   }
   stk.pop();
  }
 }
 return true;
}

d)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
        {
  char ch = exp.charAt(i);
                if (ch == '(')
                stk.push(i);
                else if (ch == ')')
                {
   if(stk.peek() != null)
                        {
    return false;
   }
   stk.pop();
  }
 }
 return true;
  }

a.

a

b.

b

c.

c

d.

d

Answer: (a).a

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Write a piece of code which returns true if the string contains balanced parenthesis, false otherwise.

Similar Questions

Discover Related MCQs

Q. What is the time complexity for converting decimal to binary numbers?

Q. Express -15 as a 6-bit signed binary number.

Q. To implement a stack using queue(with only enqueue and dequeue operations), how many queues will you need?

Q. After performing these set of operations, what does the final list look contain?

InsertFront(10);

InsertFront(20);

InsertRear(30);

DeleteFront();

InsertRear(40);

InsertRear(10);

DeleteRear();

InsertRear(15);

display();

Q. What is the time complexity of deleting from the rear end of the dequeue implemented with a singly linked list?

Q. What are the applications of dequeue?

Q. What is a dequeue?

Q. What is the time complexity to insert a node based on position in a priority queue?

Q. What are the advantages of priority queues?

Q. What is not a disadvantage of priority scheduling in operating systems?

Q. What is the time complexity to insert a node based on key in a priority queue?

Q. Which of the following is not an application of priority queue?

Q. With what data structure can a priority queue be implemented?

Q. The essential condition which is checked before deletion in a linked queue is?

Q. The essential condition which is checked before insertion in a linked queue is?

Q. In linked list implementation of a queue, the important condition for a queue to be empty is?

Q. In linked list implementation of a queue, from where is the item deleted?

Q. In case of insertion into a linked queue, a node borrowed from the __________ list is inserted in the queue.

Q. In linked list implementation of a queue, front and rear pointers are tracked. Which of these pointers will change during an insertion into EMPTY queue?

Q. In linked list implementation of a queue, front and rear pointers are tracked. Which of these pointers will change during an insertion into a NONEMPTY queue?