Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Super_Balanced_String2
- {
- public static void main(String[] args)
- {
- Scanner sc=new Scanner(System.in);
- int t=sc.nextInt();
- sc.nextLine();
- while(t-->0)
- {
- String s=sc.nextLine();
- int l=s.length();
- int longest_op_brac=0;
- int longest_clo_brac=0;
- for(int x=0;x<l;x++)
- {
- if(s.charAt(x)=='(')
- {
- int temp=0;
- for(int y=x;y<l;y++)
- {
- if(s.charAt(y)=='(')
- {
- temp+=1;
- if(temp>longest_op_brac){
- longest_op_brac=temp;//System.out.println(temp);
- }
- }
- else
- {
- x=y;
- break;
- }
- }
- }
- if(s.charAt(x)==')')
- {
- int temp=0;
- for(int y=x;y<l;y++)
- {
- if(s.charAt(y)==')')
- {
- temp+=1;
- if(temp>longest_clo_brac){
- longest_clo_brac=temp;//System.out.println(temp);
- }
- }
- else
- {
- x=y;
- break;
- }
- }
- }
- }
- if(longest_op_brac>longest_clo_brac)
- System.out.println(longest_clo_brac*2);
- else
- System.out.println(longest_op_brac*2);
- }
- }
- }
Add Comment
Please, Sign In to add comment