diff --git a/Check for Balanced Brackets in an expression (well-formedness) using Stack b/Check for Balanced Brackets in an expression (well-formedness) using Stack new file mode 100644 index 0000000..87a222b --- /dev/null +++ b/Check for Balanced Brackets in an expression (well-formedness) using Stack @@ -0,0 +1,49 @@ +// C++ program to check for balanced brackets. + +#include +using namespace std; + +// Function to check if brackets are balanced +bool areBracketsBalanced(string expr) +{ + // Declare a stack to hold the previous brackets. + stack temp; + for (int i = 0; i < expr.length(); i++) { + if (temp.empty()) { + + // If the stack is empty + // just push the current bracket + temp.push(expr[i]); + } + else if ((temp.top() == '(' && expr[i] == ')') + || (temp.top() == '{' && expr[i] == '}') + || (temp.top() == '[' && expr[i] == ']')) { + + // If we found any complete pair of bracket + // then pop + temp.pop(); + } + else { + temp.push(expr[i]); + } + } + if (temp.empty()) { + + // If stack is empty return true + return true; + } + return false; +} + +// Driver code +int main() +{ + string expr = "{()}[]"; + + // Function call + if (areBracketsBalanced(expr)) + cout << "Balanced"; + else + cout << "Not Balanced"; + return 0; +}