Remove brackets from an algebraic string containing + and – operators

2.4

Simplify a given algebraic string of characters, ‘+’, ‘-‘ operators and parentheses. Output the simplified string without parentheses.

Examples:

Input : "a-(b+c)"
Output : "a-b-c"

Input : "a-(b-c-(d+e))-f"
Output : "a-b+c+d+e-f" 

The idea is to check operators just before starting of bracket, i.e., before character ‘(‘. If operator is -, we need to toggle all operators inside the bracket. A stack is used which stores only two integers 0 and 1 to indicate whether to toggle or not.
We iterate for every character of input string. Initially push 0 to stack. Whenever the character is an operator (‘+’ or ‘-‘), check top of stack. If top of stack is 0, append the same operator in the resultant string. If top of stack is 1, append the other operator (if ‘+’ append ‘-‘) in the resultant string.

// C++ program to simplify algebraic string
#include <bits/stdc++.h>
using namespace std;

// Function to simplify the string
char* simplify(string str)
{
    int len = str.length();

    // resultant string of max length equal
    // to length of input string
    char* res = new char(len);
    int index = 0, i = 0;

    // create empty stack
    stack<int> s;
    s.push(0);

    while (i < len) {
        if (str[i] == '+') {

            // If top is 1, flip the operator
            if (s.top() == 1)
                res[index++] = '-';

            // If top is 0, append the same operator
            if (s.top() == 0)
                res[index++] = '+';

        } else if (str[i] == '-') {
            if (s.top() == 1)
                res[index++] = '+';
            else if (s.top() == 0)
                res[index++] = '-';
        } else if (str[i] == '(' && i > 0) {
            if (str[i - 1] == '-') {

                // x is opposite to the top of stack
                int x = (s.top() == 1) ? 0 : 1;
                s.push(x);
            }

            // push value equal to top of the stack
            else if (str[i - 1] == '+')
                s.push(s.top());
        }

        // If closing parentheses pop the stack once
        else if (str[i] == ')')
            s.pop();

        // copy the character to the result
        else
            res[index++] = str[i];
        i++;
    }
    return res;
}

// Driver program
int main()
{
    string s1 = "a-(b+c)";
    string s2 = "a-(b-c-(d+e))-f";
    cout << simplify(s1) << endl;
    cout << simplify(s2) << endl;
    return 0;
}

Output:

a-b-c
a-b+c+d+e-f

This article is contributed by Chhavi . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.4 Average Difficulty : 2.4/5.0
Based on 7 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.