Jacob_Thomas

Super_Balanced_String

Jul 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.util.*;
  2. class Super_Balanced_String2
  3. {
  4.    
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner sc=new Scanner(System.in);
  8.         int t=sc.nextInt();
  9.        sc.nextLine();
  10.         while(t-->0)
  11.         {
  12.             String s=sc.nextLine();
  13.             int l=s.length();
  14.             int longest_op_brac=0;
  15.             int longest_clo_brac=0;
  16.             for(int x=0;x<l;x++)
  17.             {
  18.                 if(s.charAt(x)=='(')
  19.                 {
  20.                     int temp=0;
  21.                     for(int y=x;y<l;y++)
  22.                     {
  23.                         if(s.charAt(y)=='(')
  24.                         {
  25.                            temp+=1;  
  26.                            if(temp>longest_op_brac){
  27.                            longest_op_brac=temp;//System.out.println(temp);
  28.                         }
  29.                         }
  30.                         else
  31.                         {
  32.                            x=y;
  33.                            
  34.                        
  35.                             break;
  36.                         }
  37.                        
  38.                     }
  39.                 }
  40.                 if(s.charAt(x)==')')
  41.                 {
  42.                     int temp=0;
  43.                     for(int y=x;y<l;y++)
  44.                     {
  45.                         if(s.charAt(y)==')')
  46.                         {
  47.                            temp+=1;  
  48.                            if(temp>longest_clo_brac){
  49.                            longest_clo_brac=temp;//System.out.println(temp);
  50.                         }
  51.                            
  52.                         }
  53.                         else
  54.                         {
  55.                            x=y;
  56.                            
  57.                        
  58.                             break;
  59.                         }
  60.                     }
  61.                 }
  62.                
  63.                
  64.             }
  65.             if(longest_op_brac>longest_clo_brac)
  66.             System.out.println(longest_clo_brac*2);
  67.             else
  68.             System.out.println(longest_op_brac*2);
  69.            
  70.         }
  71.     }
  72. }
Add Comment
Please, Sign In to add comment