/**
* 括号匹配
*
* @author fangtengfei
* @date 2010-5-15
*/
public class ParenthesesMatching {
public static void main(String[] args) {
// String c="((((()()))()()())";
String c = "((((()))))()";
// String c="(((()()))()()())";
// String c="()()(())";
boolean hasMatch = ParenthesesHasMatching(c.toCharArray());
System.out.println(hasMatch);
}
private static boolean ParenthesesHasMatching(char[] charArray) {
// 括号匹配值,等于0则代表匹配,小于0直接退出,遇到(加1 遇到)-1
int matchValue = 0;
for (char c : charArray) {
if (c == '(') {
matchValue += 1;
}
if (c == ')') {
matchValue -= 1;
}
if (matchValue < 0) {
return false;
}
}
return matchValue == 0;
}
}