127 括号字符串的有效性
Table of Contents

ac

给定一个字符串str,判断是不是整体有效的括号字符串(整体有效:即存在一种括号匹配方案,使每个括号字符均能找到对应的反向括号,且字符串中不包含非括号字符)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;


public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws Exception {
        String str = reader.readLine();
        System.out.println(isOK(str) ? "YES" : "NO");
    }

    static boolean isOK(String str){
        int len = str.length();
        char[] chars = str.toCharArray();
        for(int i=0;i<len;i++){
            if(! (chars[i] == '(' || chars[i] == ')') ){
                return false;
            }
        }
        int leftKuo = 0;
        for(int i=0;i<len;i++){
            if(chars[i] == '('){
                leftKuo++;
            }
            if(chars[i] == ')'){
                if(leftKuo > 0){
                    leftKuo --;
                }else {
                    return false;
                }
            }
        }
        if(leftKuo != 0){
            return false;
        }
        return true;
    }

}
/*
A1BC22DE1F
1221

*/