import java.util.*;


public class Solution {
    /**
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    static boolean f(char a)
	{
		return a == '[' || a == '{' || a == '(';
	}

	static boolean is(char a, char b)
	{
		return (a == '[' && b == ']') || (a == '{' && b == '}') || (a == '(' && b == ')');
	}

	public boolean isValid(String str)
	{
		char s[] = str.toCharArray();
		int len = str.length();

		LinkedList<Character> li = new LinkedList<Character>();

		for (int i = 0; i < len; i++)
		{
			if (f(s[i]))
				li.push(s[i]);
			else
			{
                if (li.size() == 0)
					return false;
				if (is(li.peek(), s[i]))
					li.poll();
				else
					return false;
			}
		}

		return li.size() == 0;
	}
}