s = "(()(" def solution(s) : from collections import deque stack = deque() for c in s : if c == "(" : stack.append(c) elif c == ")" : if not stack : return False else : stack.pop() if stack : return False else : return True print(solution(s))