gistfile1.txt import java.util.Stack;public class Main{ public static void main(String[] args){ String str1 = "((())())"; print(isValid(str1),maxLength1(str1),maxLength2(str1)); String str2 = "(())(()(())"; print(isValid(str2),maxLength1(st
import java.util.Stack; public class Main{ public static void main(String[] args){ String str1 = "((())())"; print(isValid(str1),maxLength1(str1),maxLength2(str1)); String str2 = "(())(()(())"; print(isValid(str2),maxLength1(str2),maxLength2(str2)); String str3 = "()(()()("; print(isValid(str3),maxLength1(str3),maxLength2(str3)); } public static void print(boolean b,int maxLength1,int maxLength2){ System.out.println(b+"\t"+maxLength1+"\t"+maxLength2); } public static boolean isValid(String str){ if(str==null||str.equals("")) return false; char[] chars=str.toCharArray(); int status=0; for(int i=0;istack=new Stack (); int start=-1,maxLength=0; for(int i=0;i =0&&chars[pre]=='(') dp[i]=dp[i-1]+2+(pre>0 ? dp[pre-1]:0); } maxLength=Math.max(maxLength,dp[i]); } return maxLength; } }