-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBracketBalance.java
More file actions
30 lines (30 loc) · 852 Bytes
/
BracketBalance.java
File metadata and controls
30 lines (30 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Stack <Integer> st = new Stack<>();
int n=sc.nextInt();
while(n-->0)
{
int pos=0;
String str=sc.next();
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='(')
{
System.out.print(++pos+" ");
st.push(pos);
}
else if(str.charAt(i)==')')
{
System.out.print(st.peek()+" ");
st.pop();
}
}
System.out.println();
}
}
}