Simple Bracket Checker
EasyPractice library · +10 XPstackbracket matchingbeginner
Given a string consisting only of opening and closing brackets '(' and ')', determine if the brackets are properly matched. A string of brackets is considered properly matched if:
- For every opening bracket '(', there is a corresponding closing bracket ')'.
- The brackets are closed in the correct order. For example, '(()())' is valid, but ')(' and '())(' are not.
Print "YES" if the string is properly matched, and "NO" otherwise.
Input
A single line containing a string of '(' and ')'.
Output
Print "YES" or "NO".
Constraints
The input string contains between 1 and 1000 characters, inclusive.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
(()())
Output
YES
All brackets are properly matched.
Example 2
Input
())
Output
NO
The second closing bracket has no matching opening bracket.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
(()())
Example 2 input
())