Problem
Write a program to find bracket matches correctly or not.
Examples
[][][] => "Matched" (())()) => "Not Matched" }}{{ => "Not Matched"
Instructions
- Try to solve it in a most simple and efficient way.
- Share your codes in the comments (in any language), I'll find some bugs ๐.
- Feel free to mention if you find any bugs in my code ๐ง.
- Share some challenges in the comments, I'll solve them in my next blog post.
- Try to solve it on your own before referring below code.
Code
def is_valid_match(input_exp):
flag = 0
open_brackets = ['[', '(', '{']
close_brackets = [']', ')', '}']
for char in input_exp:
if char in open_brackets:
flag += 1
elif char in close_brackets:
flag -= 1
if flag < 0:
return False
return flag == 0
input_exp = '{}{}{}}'
print('Matched' if is_valid_match(input_exp) else 'Not Matched')
I've used Python for solving this challenge, but if you want the same solution in some other languages let me know in the comments ๐.