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