static boolean validString(String a, char open, char close) {
    boolean digit = false;
    int count = 0;
    for (int i = 0; i < a.length(); i++) {
      if (a.charAt(i) == ' ') {
        continue;
      }
      if (Character.isDigit(a.charAt(i))) {
        digit = true;
      } else if (a.charAt(i) == open) {
        if (digit) {
          return false;
        }
        count++;
      } else if (a.charAt(i) == close) {
        if (!digit) {
          return false;
        }
        count--;
        if (count < 0) {
          return false;
        }
      } else {
        digit = false;
      }
    }
    return count == 0 && digit;
  }