Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

# Stacks

Problem Description

In computer science, a stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added.(Wikipedia)

Syntax to create a Stack:

```// Create Integer type stack
Stack < Integer > myStack = new Stack<>();
```

The above code create a stack named myStack which can store integers.

Some more functions which we can use with stack:

• myStack.push(x) : Use push() to push an element into the stack.
• myStack.pop() : To remove an element from the top of the stack, we use the pop() method.
• myStack.peek() : The peek() method returns an object from the top of the stack.
• myStack.empty() : To check whether a stack is empty or not, we use the empty() method.

Now, try to solve the below problem using stack :

Given a string A consisting only of ’(‘ and ’)’.

You need to find whether parantheses in A is balanced or not ,if it is balanced then print 1 else print 0.

Examples of some correctly balanced strings are: “()()”, “((()))”, “((()))”

Examples of some unbalanced strings are: “()(“, “(()))”, “((“, “)(“ etc.

Problem Constraints

1 <= Number of Testcases <= 100

1 <= |A| <= 105

Input Format

First line of input consist of single integer T denoting the number of testcases.

Each testcase has a single line of input containing an string A.

Output Format

For each testcase output a single line i.e print 1 if parantheses in string are balanced else print 0.

Example Input

Input 1:

2
”(()())”
”(()”

`Example Output`

Output 1:

1
0

`Example Explanation`

Explanation 1:

Testcase 1: Given string (()()) is balanced so we print 1

Testcase 2: Given string (() is not balanced so we print 0

` `
Hints
• Hint 1
• Solution Approach
• Complete Solution

## Discussion

Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College/University Name
Job Title
Job Title
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer