Remove brackets from an algebraic string containing + and - operators
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    11 May, 2025
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                Given an algebraic expression as a string s containing operands (letters), + and  - operators, and parentheses, simplify the expression by removing all parentheses and correctly applying the operators. Return the simplified expression without parentheses.
Examples: 
Input:"(a - (b + c) + d)"
Output: "a- b - c + d"
Explanation: a - (b + c) + d simplifies to a - b - c + d
Input:"a - (b - c - (d + e )) - f"
Output: "a - b + c + d + e - f" 
Explanation: c - (d + e), to get (c - d - e) The outer - flips the signs, resulting in a - b + c + d + e - f
The approach uses a stack to track the sign context while processing the expression. Initially, the stack is set to 1 (positive context). When encountering a - before a parenthesis, the sign inside the parentheses is toggled by pushing -1 onto the stack. For each operator (+ or -), the stack’s top determines if the operator should be flipped. When encountering operands, they are directly appended to the result. After processing the entire string, the simplified expression is returned without parentheses and with correct signs.
            C++
    #include <iostream>
#include <stack>
#include <string>
using namespace std;
string simplify(string s)
{
    int len = s.length();
    string res = "";
    stack<int> st; 
    
    // Initially, we assume the sign is positive
    st.push(1);   
    for (int i = 0; i < len; i++)
    {
        if (s[i] == '(')
        {
            
            // If the previous character is '-', 
            // we flip the sign inside parentheses
            if (i > 0 && s[i - 1] == '-')
            {
                st.push(-st.top());
            }
            else
            {
                st.push(st.top());
            }
        }
        else if (s[i] == ')')
        {
            
            // Pop when we encounter a
            // closing parenthesis
            st.pop(); 
        }
        else if (s[i] == '+')
        {
            
            // Add '+' or '-' based on the current sign
            if (st.top() == 1)
                res += '+';
            else
                res += '-';
        }
        else if (s[i] == '-')
        {
            
            // Add '+' or '-' based on the current sign
            if (st.top() == 1)
                res += '-';
            else
                res += '+';
        }
        else
        {
            
            // If it's an operand
            // (variable), add it directly
            res += s[i];
        }
    }
    return res;
}
int main()
{
    string s = "(a-(d-(b-c))+d)";
    cout << simplify(s) << endl;
    return 0;
}
import java.util.Stack;
public class GfG {
    public static String simplify(String s) {
        int len = s.length();
        StringBuilder res = new StringBuilder();
        Stack<Integer> st = new Stack<>();
        // Initially, we assume the sign is positive
        st.push(1);
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == '(') {
                
                // If the previous character is '-', we 
                // flip the sign inside parentheses
                if (i > 0 && s.charAt(i - 1) == '-') {
                    st.push(-st.peek());
                } else {
                    st.push(st.peek());
                }
            } else if (s.charAt(i) == ')') {
                
                // Pop when we encounter a closing parenthesis
                st.pop();
            } else if (s.charAt(i) == '+') {
                
                // Add '+' or '-' based on the current sign
                if (st.peek() == 1)
                    res.append('+');
                else
                    res.append('-');
            } else if (s.charAt(i) == '-') {
                
                // Add '+' or '-' based on the current sign
                if (st.peek() == 1)
                    res.append('-');
                else
                    res.append('+');
            } else {
                
                // If it's an operand (variable), add it directly
                res.append(s.charAt(i));
            }
        }
        return res.toString();
    }
    public static void main(String[] args) {
        String s = "(a-(d-(b-c))+d)";
        System.out.println(simplify(s));
    }
}
def simplify(s):
    length = len(s)
    res = ""
    st = []
    # Initially, we assume the sign is positive
    st.append(1)
    for i in range(length):
        if s[i] == '(': 
            
            # If the previous character is '-', 
            # we flip the sign inside parentheses
            if i > 0 and s[i - 1] == '-':
                st.append(-st[-1])
            else:
                st.append(st[-1])
        elif s[i] == ')':
            
            # Pop when we encounter a closing parenthesis
            st.pop()
        elif s[i] == '+':
            
            # Add '+' or '-' based on the current sign
            if st[-1] == 1:
                res += '+'
            else:
                res += '-'
        elif s[i] == '-':
            
            # Add '+' or '-' based on the current sign
            if st[-1] == 1:
                res += '-'
            else:
                res += '+'
        else:
            
            # If it's an operand (variable), add it directly
            res += s[i]
    return res
s = '(a-(d-(b-c))+d)'
print(simplify(s))
using System;
using System.Collections.Generic;
class GfG {
    public static string Simplify(string s) {
        int len = s.Length;
        string res = "";
        Stack<int> st = new Stack<int>();
        // Initially, we assume the sign is positive
        st.Push(1);
        for (int i = 0; i < len; i++) {
            if (s[i] == '(') {
                
                // If the previous character is '-', we flip the sign inside parentheses
                if (i > 0 && s[i - 1] == '-') {
                    st.Push(-st.Peek());
                } else {
                    st.Push(st.Peek());
                }
            } else if (s[i] == ')') {
                
                // Pop when we encounter a closing parenthesis
                st.Pop();
            } else if (s[i] == '+') {
                
                // Add '+' or '-' based on the current sign
                if (st.Peek() == 1)
                    res += '+';
                else
                    res += '-';
            } else if (s[i] == '-') {
                
                // Add '+' or '-' based on the current sign
                if (st.Peek() == 1)
                    res += '-';
                else
                    res += '+';
            } else {
                
                // If it's an operand (variable), add it directly
                res += s[i];
            }
        }
        return res;
    }
    static void Main() {
        string s = "(a-(d-(b-c))+d)";
        Console.WriteLine(Simplify(s));
    }
}
function simplify(s) {
    let len = s.length;
    let res = '';
    let st = [];
    // Initially, we assume the sign is positive
    st.push(1);
    for (let i = 0; i < len; i++) {
        if (s[i] === '(') {
            
            // If the previous character is '-', we flip the sign inside parentheses
            if (i > 0 && s[i - 1] === '-') {
                st.push(-st[st.length - 1]);
            } else {
                st.push(st[st.length - 1]);
            }
        } else if (s[i] === ')') {
            
            // Pop when we encounter a closing parenthesis
            st.pop();
        } else if (s[i] === '+') {
            
            // Add '+' or '-' based on the current sign
            if (st[st.length - 1] === 1)
                res += '+';
            else
                res += '-';
        } else if (s[i] === '-') {
            
            // Add '+' or '-' based on the current sign
            if (st[st.length - 1] === 1)
                res += '-';
            else
                res += '+';
        } else {
            
            // If it's an operand (variable), add it directly
            res += s[i];
        }
    }
    return res;
}
let s = '(a-(d-(b-c))+d)';
console.log(simplify(s));
Time Complexity: O(n), Where n is the length of the given string.
Auxiliary Space: O(n)
                                
                                
                            
                                                                                
                                                            
                                                    
                                                
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem