From 3c7339e59c915ed92dc414bc580b10cc07b7d445 Mon Sep 17 00:00:00 2001 From: Jyoti Singh <98025162+dev24il@users.noreply.github.com> Date: Sat, 8 Oct 2022 14:49:57 +0530 Subject: [PATCH] chore(CPlusPlus): add redundant parenthesis (#946) * Redundant parenthesis in cpp completed * Update README.md --- algorithms/CPlusPlus/README.md | 1 + .../Stacks/redundant-parenthesis.cpp | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 algorithms/CPlusPlus/Stacks/redundant-parenthesis.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 66505d55..1de7183a 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -99,6 +99,7 @@ - [Infix to postfix expression conversion](Stacks/infix-to-postfix.cpp) - [Stock Span Problem using Stacks](Stacks/stock-span-problem.cpp) - [Prefix to Postfix expression conversion](Stacks/prefix_to_postfix.cpp) +- [Redundant Parenthesis](Stacks/redundant-parenthesis.cpp) ## Sorting diff --git a/algorithms/CPlusPlus/Stacks/redundant-parenthesis.cpp b/algorithms/CPlusPlus/Stacks/redundant-parenthesis.cpp new file mode 100644 index 00000000..293ca19b --- /dev/null +++ b/algorithms/CPlusPlus/Stacks/redundant-parenthesis.cpp @@ -0,0 +1,74 @@ +// RedundantParenthesis +// Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parenthesis are redundant if the same sub-expression is surrounded by unnecessary or multiple brackets. Print True if redundant, else False. + +// Algorithm +// 1. We iterate through the given expression and for each character in the expression, if the character is an open parenthesis ‘(‘ or any operators, we push it to the stack. +// 2. If the character is close parenthesis ‘)’, then pop characters from the stack till matching open parenthesis ‘(‘ is found. +// For any sub-expression of expression, if we are able to pick any sub-expression of expression surrounded by (), then we again left with () as part of string, we have redundant braces. +// We iterate through the given expression and for each character in the expression, if the character is an open parenthesis ‘(‘ or any of the operators or operands, we push it to the stack. If the character is close parenthesis ‘)’, then pop characters from the stack till matching open parenthesis ‘(‘ is found. +// Now for redundancy two condition will arise while popping- +// 1. If immediate pop hits an open parenthesis ‘(‘, then we have found a duplicate parenthesis. For example, (((a+b))+c) has duplicate brackets around a+b. When we reach the second “)” after a+b, we have “((” in the stack. Since the top of stack is an opening bracket, we conclude that there are duplicate brackets. +// 2. If immediate pop doesn’t hit any operand(‘*’, ‘+’, ‘/’, ‘-‘) then it indicates the presence of unwanted brackets surrounded by expression. For instance, (a)+b contain unwanted () around a thus it is redundant. + + + +// solution +#include +using namespace std; + +int main() +{ + cout << "Enter the string:" << endl; + string s; + cin >> s; + // create a stack of characters + stack st; + bool ans = false; + + // Iterate through the given expression + for (int i = 0; i < s.size(); i++) + { + if (s[i] == '+' or s[i] == '-' or s[i] == '*' or s[i] == '/') + { + st.push(s[i]); + } + else if (s[i] == '(') + { + // if current character is close parenthesis '(' + st.push(s[i]); + } + else if (s[i] == ')') + { + // if current character is close parenthesis ')' + if (st.top() == '(') + { + ans = true; + } + while (st.top() == '+' or st.top() == '-' or st.top() == '*' or st.top() == '/') + { + st.pop(); + } + st.pop(); + } + } + + if (ans) + { + cout << "True"; + } + else + { + cout << "False"; + } +} + + +// Input : +// For example: +// 1. ((a+b)) +// 2. (a+b*(c-d)) + +// Output: +// 1. True, ((a+b)) can reduced to (a+b), this is Redundant +// 2. False, (a+b*(c-d)) doesn't have any redundant or multiple +// brackets