From 25b25884452b20567bb6dfd7c1b58246d4c37663 Mon Sep 17 00:00:00 2001 From: Srikant Sahoo <46858011+coder-hellosrikant@users.noreply.github.com> Date: Wed, 2 Oct 2019 00:05:31 +0530 Subject: [PATCH] Updated parenthesis_checker.cpp Added and replaced some code which I found redundant in it making it more faster and efficient. --- parentheses_checker.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/parentheses_checker.cpp b/parentheses_checker.cpp index 71ad23b..cf604d2 100644 --- a/parentheses_checker.cpp +++ b/parentheses_checker.cpp @@ -121,15 +121,16 @@ int main() { } else { temp = s.pop(); - if((exp[i] == ')') && (temp == '{' || temp == '[')) { + + //making flag=0 for every possible invalid popped elemnt + if((exp[i] == ')') && (temp != '(')) flag = 0; - } - if((exp[i] == '}') && (temp == '(' || temp == ']')) { + + if((exp[i] == '}') && (temp != '{')) flag = 0; - } - if((exp[i] == ']') && (temp == '(' || temp == '{')) { + + if((exp[i] == ']') && (temp != '[')) flag = 0; - } } } }