Jacob_Thomas

Super_Balanced_String

Jul 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.*;
  2. class Super_Balanced_String
  3. {
  4.    
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner sc=new Scanner(System.in);
  8.         int t=sc.nextInt();
  9.         Super_Balanced_String su= new Super_Balanced_String();
  10.        
  11.        
  12.         sc.nextLine();
  13.         while(t-->0)
  14.         {
  15.             String s=sc.nextLine();
  16.             int l=s.length();
  17.             int largest=0;
  18.             for(int x=0;x<l-1;x++)
  19.             {
  20.                if(s.charAt(x)=='(' && s.charAt(x+1)==')')
  21.                {
  22.                    int m=su.check(x,s);
  23.                    if(largest<m)
  24.                    largest=m;
  25.                }
  26.             }
  27.             System.out.println(largest*2);
  28.         }
  29.     }
  30.     int check(int x, String s)
  31.     {
  32.         int pos1=x;
  33.        
  34.         int max=1;
  35.        
  36.         int inc=0;
  37.         while(((pos1-inc)>0) && (pos1+1+inc)<(s.length()-1))
  38.         {
  39.             //System.out.println(pos1-inc);
  40.             inc+=1;
  41.             if(s.charAt(pos1-inc)=='(' && s.charAt(pos1+1+inc)==')')
  42.             {
  43.                 max+=1;
  44.                 //System.out.println(pos1-inc);
  45.                
  46.             }
  47.             else
  48.             break;
  49.         }
  50.        
  51.         return max;
  52.     }
  53.    
  54. }
Add Comment
Please, Sign In to add comment