import java.io.*;
import java.util.*;

public class BalancedString {
  public boolean isBalanced(String word) {
    boolean result = true;

    Stack<Character> brackets = new Stack<Character>();

    for (int i = 0; i < word.length(); i++) {
      char c = word.charAt(i);

      if (c == '(') {
        brackets.push(c);
      } else {
        if (c == ')') {
          if (brackets.isEmpty()) {
            result = false;
          } else {
            char top = brackets.pop();

            if (top != '(') {
              return false;
            }
          }
        }
      }
    }

    if (!brackets.isEmpty()) {
      result = false;
    }

    return result;
  }

  public static void main(String [] args) {
    BalancedString b = new BalancedString();
    System.out.println(b.isBalanced("(a((s)d))"));
  }
}