diff --git a/3-2.cpp b/3-2.cpp index 1594f09..18cdcd7 100644 --- a/3-2.cpp +++ b/3-2.cpp @@ -154,6 +154,7 @@ int main() { string s = "()("; stack st; + bool isBalanced = true; for(char c: s) { @@ -161,18 +162,23 @@ int main() { st.push(c); } - else + else if(c==')') { if(st.empty()) { + isBalanced=false; break; } + else + { + st.pop(); + } + - st.pop(); } } - if(st.empty()) + if(st.empty() && isBalanced) { cout << "Balanced"; }