package com.likou;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
class ValidBrackets {
class Case1 {
public boolean isValid(String s) {
ArrayList<String> list = new ArrayList<>(120);
boolean a = true;
for (int i = 0; i < s.length(); i++) {
String s1 = s.substring(i, i + 1);
if (getStr(s1) != null) {
list.add(getStr(s1));
} else if (list.size() > 0 && list.get(list.size() - 1).equals(s1)) {
list.remove(list.size() - 1);
} else {
a = false;
break;
}
}
return a && list.size() == 0;
}
public String getStr(String s) {
String a;
switch (s) {
case "(":
a = ")";
break;
case "{":
a = "}";
break;
case "[":
a = "]";
break;
default:
a = null;
}
return a;
}
}
class Case2 {
public boolean isValid(String s) {
Map<String, String> map = new HashMap<>();
map.put("(", ")");
map.put("[", "]");
map.put("{", "}");
ArrayList<String> list = new ArrayList<>(120);
boolean a = true;
for (int i = 0; i < s.length(); i++) {
String s1 = s.substring(i, i + 1);
if (map.get(s1) != null) {
list.add(map.get(s1));
} else if (list.size() > 0 && list.get(list.size() - 1).equals(s1)) {
list.remove(list.size() - 1);
} else {
a = false;
break;
}
}
return a && list.size() == 0;
}
}
}
package com.example.demo;
import java.util.Stack;
class MaxValidBrackets {
public static void main(String[] args) {
String ss = "))))())(())()))(()()(())(())()))(((()()))()()))(())(())()())()(()())((()(((())()())(()())(())((()))))())()))()(())))())()))(()))((()())((()(())))(()))))))))((())(()()((())()()(()))))(((()(())))())))()())))())()()())()(())()(((())()))()()())))()())))()((((((())((())))((())())(((()())())()((((((()())((()()(())(()))(()())()))()(()(()())(()))((())((())))))()()))))()())()))))((((())(())))((()))(()()()()()((())((((())())()())()())(()(()()))())(((()())(()))()))(())()((()(())))))()())())()()(())))((())()()()))(())((()())))))((()((((()(((())()))))(()))()()))(()(())(()((()()()))))()))()()(((()()(())())()(())(()()()))()(()())))()((()((()))))())()(())()(()()((()()())(((())((())))(()())))()))()()())()))((()))(((()()()((()))))()()()))()))())())))))())()))(()))))(()()()))()((())))((())))()))(()()()()()()(()))())())(((())))(())(()(()())((()()()()))()()(()()))(()())(()()()((()()(((()(()((()((((()((())((()()))))))()())())(()(())()((((()()()()()))))()())()((())))))))()(((()())))()(()()(()()()()))()((((()((())(())))())))(()()()())()))))))((()))())((())(()(()(((()()()((((()()))())()())()())()))))())()(((()))))()()())))(())())))(())())((()))(())))(((()()))((((()))(()()))())((()())(()))(()(())(()(()))((((((()()(()()(()))()(()(())((((((((()(()())((())))())()())(()(()()))))(()(()()()))(()((()(((())((())(())()(()()())(()))())((()((((((())())(())()(()()(()())(())())()()))())(()))(())))()())))()()((()))())()()(())(()())()())())())))()()))((((()((()(())(((())()((())(())())))))()(()))())()))())(((())))))((((()(()()))))(((())(((())((())))))()()))()(()(((((((()))))((()))())()(())()))())())((()))))((())(())))(((())((((()(())(())()(((((())))()))()(()())(()()(())()(((()))())())))()))()()())((";
long l = System.currentTimeMillis();
int valid = isValid(ss);
System.out.println(valid);
System.out.println(System.currentTimeMillis() - l);
}
public static int isValid(String s) {
if (s.length() < 2 || s.indexOf("(") < 0 || s.lastIndexOf(")") < 0) {
return 0;
}
s = s.substring(s.indexOf("("), s.lastIndexOf(")") + 1);
int a = 0;
int aa = 0;
Stack<String> stack = new Stack<>();
Stack<String> stack1 = new Stack<>();
int length = s.length();
for (int i = 0; i < length; i++) {
String s1 = s.substring(i, i + 1);
String s2 = s.substring(length - i - 1, length - i);
String s3 = get1(stack, s1);
String s4 = get2(stack1, s2);
if ("0".equals(s3) || "0".equals(s4)) {
aa = a > aa ? a : aa;
a = 0;
} else {
a++;
}
}
aa = a > aa ? a : aa;
return aa;
}
private static String get1(Stack<String> stack, String s1) {
if (getStr(s1) != null) {
stack.push(s1);
return ".";
} else if (stack.size() > 0) {
stack.pop();
return "1";
} else {
return "0";
}
}
private static String get2(Stack<String> stack, String s1) {
if (getStr1(s1) != null) {
stack.push(s1);
return ".";
} else if (stack.size() > 0) {
stack.pop();
return "1";
} else {
return "0";
}
}
public static String getStr(String s) {
String a;
switch (s) {
case "(":
a = ")";
break;
default:
a = null;
}
return a;
}
public static String getStr1(String s) {
String a;
switch (s) {
case ")":
a = "(";
break;
default:
a = null;
}
return a;
}
}