The Wayback Machine - https://web.archive.org/web/20240713132902/https://www.geeksforgeeks.org/all-ways-to-add-parenthesis-for-evaluation/
Open In App

All ways to add parenthesis for evaluation

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string that represents an expression constituting numbers and binary operator +, – and * only. We need to parenthesize the expression in all possible way and return all evaluated values.

Input : expr = “3-2-1”
Output : {0, 2}
((3-2)-1) = 0
(3-(2-1)) = 2
Input : expr = "5*4-3*2"
Output : {-10, 10, 14, 10, 34}
(5*(4-(3*2))) = -10
(5*((4-3)*2)) = 10
((5*4)-(3*2)) = 14
((5*(4-3))*2) = 10
(((5*4)-3)*2) = 34

We can solve this problem by parenthesizing all possible valid substring of the expression and then evaluating them, but as we can see that it will involve solving lots of repeating subproblem, to save ourselves we can follow a dynamic programming approach. We store the result for each substring in a map and if string in recursion is already solved, we return result from map instead of solving that again. Below code runs a loop in the string and if at any instant, character is operator then we divide the input from there, recursively solve each part and then combine the result in all possible ways. See the use of c_str() function, this function converts the C++ string into C char array, this function is used in below code because atoi() function expects a character array as an argument not the string. It converts character array to number. 

A

C++




//  C++ program to output all possible values of
// an expression by parenthesizing it.
#include <bits/stdc++.h>
using namespace std;
 
//  method checks, character is operator or not
bool isOperator(char op)
{
    return (op == '+' || op == '-' || op == '*');
}
 
//  Utility recursive method to get all possible
// result of input string
vector<int> possibleResultUtil(string input,
            map< string, vector<int> > memo)
{
    //  If already calculated, then return from memo
    if (memo.find(input) != memo.end())
        return memo[input];
 
    vector<int> res;
    for (int i = 0; i < input.size(); i++)
    {
        if (isOperator(input[i]))
        {
            // If character is operator then split and
            // calculate recursively
            vector<int> resPre =
              possibleResultUtil(input.substr(0, i), memo);
            vector<int> resSuf =
              possibleResultUtil(input.substr(i + 1), memo);
 
            //  Combine all possible combination
            for (int j = 0; j < resPre.size(); j++)
            {
                for (int k = 0; k < resSuf.size(); k++)
                {
                    if (input[i] == '+')
                        res.push_back(resPre[j] + resSuf[k]);
                    else if (input[i] == '-')
                        res.push_back(resPre[j] - resSuf[k]);
                    else if (input[i] == '*')
                        res.push_back(resPre[j] * resSuf[k]);
                }
            }
        }
    }
 
    // if input contains only number then save that
    // into res vector
    if (res.size() == 0)
        res.push_back(atoi(input.c_str()));
 
    // Store in memo so that input string is not
    // processed repeatedly
    memo[input] = res;
    return res;
}
 
//  method to return all possible output
// from input expression
vector<int> possibleResult(string input)
{
    map< string, vector<int> > memo;
    return possibleResultUtil(input, memo);
}
 
//  Driver code to test above methods
int main()
{
    string input = "5*4-3*2";
    vector<int> res = possibleResult(input);
 
    for (int i = 0; i < res.size(); i++)
        cout << res[i] << " ";
    return 0;
}


Java




import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
public class PossibleExpressionValues {
 
    // Method to check if a character is an operator
    private static boolean isOperator(char op) {
        return (op == '+' || op == '-' || op == '*');
    }
 
    // Utility recursive method to get all possible results of an input string
    private static ArrayList<Integer> possibleResultUtil(String input, Map<String, ArrayList<Integer>> memo) {
        // If already calculated, then return from memo
        if (memo.containsKey(input)) {
            return memo.get(input);
        }
 
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < input.length(); i++) {
            if (isOperator(input.charAt(i))) {
                // If the character is an operator, then split and calculate recursively
                ArrayList<Integer> resPre = possibleResultUtil(input.substring(0, i), memo);
                ArrayList<Integer> resSuf = possibleResultUtil(input.substring(i + 1), memo);
 
                // Combine all possible combinations
                for (int j = 0; j < resPre.size(); j++) {
                    for (int k = 0; k < resSuf.size(); k++) {
                        if (input.charAt(i) == '+')
                            res.add(resPre.get(j) + resSuf.get(k));
                        else if (input.charAt(i) == '-')
                            res.add(resPre.get(j) - resSuf.get(k));
                        else if (input.charAt(i) == '*')
                            res.add(resPre.get(j) * resSuf.get(k));
                    }
                }
            }
        }
 
        // If the input contains only a number, then save it into the result vector
        if (res.isEmpty())
            res.add(Integer.parseInt(input));
 
        // Store in memo so that the input string is not processed repeatedly
        memo.put(input, res);
        return res;
    }
 
    // Method to return all possible outputs from an input expression
    private static ArrayList<Integer> possibleResult(String input) {
        Map<String, ArrayList<Integer>> memo = new HashMap<>();
        return possibleResultUtil(input, memo);
    }
 
    // Driver code to test the above methods
    public static void main(String[] args) {
        String input = "5*4-3*2";
        ArrayList<Integer> res = possibleResult(input);
 
        for (int i = 0; i < res.size(); i++) {
            System.out.print(res.get(i) + " ");
        }
    }
}


Python3




# Python program to output all possible values of
# an expression by parenthesizing it.
 
# method checks, character is operator or not
def isOperator(op):
    return (op == '+' or op == '-' or op == '*')
 
# Utility recursive method to get all possible
# result of input string
def possibleResultUtil(input, memo):
    # If already calculated, then return from memo
    if input in memo:
        return memo[input]
 
    res = []
    for i in range(len(input)):
        if isOperator(input[i]):
            # If character is operator then split and
            # calculate recursively
            resPre = possibleResultUtil(input[:i], memo)
            resSuf = possibleResultUtil(input[i+1:], memo)
 
            # Combine all possible combination
            for j in range(len(resPre)):
                for k in range(len(resSuf)):
                    if input[i] == '+':
                        res.append(resPre[j] + resSuf[k])
                    elif input[i] == '-':
                        res.append(resPre[j] - resSuf[k])
                    elif input[i] == '*':
                        res.append(resPre[j] * resSuf[k])
 
    # if input contains only number then save that
    # into res list
    if len(res) == 0:
        res.append(int(input))
 
    # Store in memo so that input string is not
    # processed repeatedly
    memo[input] = res
    return res
 
# method to return all possible output
# from input expression
def possibleResult(input):
    memo = {}
    return possibleResultUtil(input, memo)
 
# Driver code to test above methods
if __name__ == '__main__':
    input = "5*4-3*2"
    res = possibleResult(input)
 
    for i in range(len(res)):
        print(res[i], end=' ')


C#




using System;
using System.Collections.Generic;
 
class Program
{
    //  method checks, character is operator or not
    static bool isOperator(char op)
    {
        return (op == '+' || op == '-' || op == '*');
    }
 
    //  Utility recursive method to get all possible
    // result of input string
    static List<int> possibleResultUtil(string input,
            Dictionary<string, List<int>> memo)
    {
        //  If already calculated, then return from memo
        if (memo.ContainsKey(input))
            return memo[input];
 
        List<int> res = new List<int>();
        for (int i = 0; i < input.Length; i++)
        {
            if (isOperator(input[i]))
            {
                // If character is operator then split and
                // calculate recursively
                List<int> resPre =
                  possibleResultUtil(input.Substring(0, i), memo);
                List<int> resSuf =
                  possibleResultUtil(input.Substring(i + 1), memo);
 
                //  Combine all possible combination
                for (int j = 0; j < resPre.Count; j++)
                {
                    for (int k = 0; k < resSuf.Count; k++)
                    {
                        if (input[i] == '+')
                            res.Add(resPre[j] + resSuf[k]);
                        else if (input[i] == '-')
                            res.Add(resPre[j] - resSuf[k]);
                        else if (input[i] == '*')
                            res.Add(resPre[j] * resSuf[k]);
                    }
                }
            }
        }
 
        // if input contains only number then save that
        // into res vector
        if (res.Count == 0)
            res.Add(int.Parse(input));
 
        // Store in memo so that input string is not
        // processed repeatedly
        memo[input] = res;
        return res;
    }
 
    //  method to return all possible output
    // from input expression
    static List<int> possibleResult(string input)
    {
        Dictionary<string, List<int>> memo = new Dictionary<string, List<int>>();
        return possibleResultUtil(input, memo);
    }
 
    //  Driver code to test above methods
    static void Main(string[] args)
    {
        string input = "5*4-3*2";
        List<int> res = possibleResult(input);
 
        for (int i = 0; i < res.Count; i++)
            Console.Write(res[i] + " ");
 
        Console.ReadLine();
    }
}
// ksam24000


Javascript




// Function to check if a character is an operator
function isOperator(op) {
    return (op === '+' || op === '-' || op === '*');
}
 
// Utility recursive function to get all possible results of the input string
function possibleResultUtil(input, memo) {
    // If already calculated, then return from memo
    if (memo[input] !== undefined) {
        return memo[input];
    }
 
    let res = [];
 
    for (let i = 0; i < input.length; i++) {
        if (isOperator(input[i])) {
            // If character is an operator, then split and calculate recursively
            const resPre = possibleResultUtil(input.substring(0, i), memo);
            const resSuf = possibleResultUtil(input.substring(i + 1), memo);
 
            // Combine all possible combinations
            for (let j = 0; j < resPre.length; j++) {
                for (let k = 0; k < resSuf.length; k++) {
                    if (input[i] === '+') {
                        res.push(resPre[j] + resSuf[k]);
                    } else if (input[i] === '-') {
                        res.push(resPre[j] - resSuf[k]);
                    } else if (input[i] === '*') {
                        res.push(resPre[j] * resSuf[k]);
                    }
                }
            }
        }
    }
 
    // If input contains only a number, then save it into the result vector
    if (res.length === 0) {
        res.push(parseInt(input));
    }
 
    // Store in memo so that the input string is not processed repeatedly
    memo[input] = res;
    return res;
}
 
// Function to return all possible outputs from the input expression
function possibleResult(input) {
    const memo = {};
    return possibleResultUtil(input, memo);
}
 
// Driver code to test the above functions
const input = "5*4-3*2";
const res = possibleResult(input);
 
// Print the results
for (let i = 0; i < res.length; i++) {
    console.log(res[i]);
}


Output:

-10 10 14 10 34 



Similar Reads

Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
Given a string S of valid parentheses sequence of length N and an even integer K, the task is to find the valid parentheses sequence of length K which is also a subsequence of the given string. Note: There can be more than one valid sequence, print any of them. Examples: Input: S = "()()()", K = 4Output: ()()Explanation:The string "()()" is a subse
7 min read
Remove all redundant parenthesis
Given a valid expression Exp containing only binary operators '+', '-', '*', '/', and operands, remove all the redundant parenthesis. A set of parenthesis is said to be redundant if, removing them, does not change the value of the expression. Note: The operators '+' and '-' have the same priority. '*' and '/' also have the same priority. '*' and '/
13 min read
Sum of all the numbers in the Nth parenthesis
Given an integer N and a sequence (1), (3, 5), (7, 9, 11), (13, 15, 17, 19), ..... the task is to find the sum of all the numbers in Nth parenthesis.Examples: Input: N = 2 Output: 8 3 + 5 = 8Input: N = 3 Output: 27 7 + 9 + 11 = 27 Approach: It can be observed that for the values of N = 1, 2, 3, ... a series will be formed as 1, 8, 27, 64, 125, 216,
2 min read
Count all indices of cyclic regular parenthesis
Given a string S of length N, consisting of only opening '(' and closing ')' parenthesis. The task is to find all indices 'K' such that S[K...N-1] + S[0...K-1] is a regular parenthesis. A regular parentheses string is either empty (""), "(" + str1 + ")", or str1 + str2, where str1 and str2 are regular parentheses strings.For example: "", "()", "(()
7 min read
Count removal of pairs required to be empty all Balanced Parenthesis subsequences
Given a string str, the task is to find the maximum count of pairs (str[i], str[j]) of balanced parenthesis required to be removed such that the string does not contain any subsequence of balanced parenthesis: Examples: Input: str = "{(})" Output: 2 Explanation: Removing the pair of valid parenthesis(0, 2) modified str to "()" Removing the pair of
8 min read
Find maximum depth of nested parenthesis in a string
We are given a string having parenthesis like below “( ((X)) (((Y))) )” We need to find the maximum depth of balanced parenthesis, like 4 in the above example. Since ‘Y’ is surrounded by 4 balanced parentheses. If parenthesis is unbalanced then return -1. Examples : Input : S = "( a(b) (c) (d(e(f)g)h) I (j(k)l)m)"; Output : 4 Input : S = "( p((q))
9 min read
Identify and mark unmatched parenthesis in an expression
Given an expression, find and mark matched and unmatched parenthesis in it. We need to replace all balanced opening parenthesis with 0, balanced closing parenthesis with 1, and all unbalanced with -1.Examples: Input : ((a) Output : -10a1 Input : (a)) Output : 0a1-1 Input : (((abc))((d))))) Output : 000abc1100d111-1-1 The idea is based on a stack. W
6 min read
InfyTQ 2019 : Find the position from where the parenthesis is not balanced
Given a string str consisting of parenthesis from [ "(" , ")" , "{" , "}" , "[" , "]" ]. If the String is perfectly balanced return 0 else return the index(starting from 1)at which the nesting is found to be wrong.Examples: Input : str = "{[()]}[]" Output : 0Input : str = "[{]()}" Output : 3Input : str = "}[{}]" Output : 1Input : str = "{([]){" Out
9 min read
Parenthesis Theorem
Parenthesis Theorem is used in DFS of graph. It states that the descendants in a depth-first-search tree have an interesting property. If v is a descendant of u, then the discovery time of v is later than the discovery time of u. In any DFS traversal of a graph g = (V, E), for any two vertices u and v exactly one of the following holds: The interva
15+ min read
Calculate weight of parenthesis based on the given conditions
Given a valid parenthesis string S, the task is to find the weight of the parenthesis based on the following conditions: Weight of "( )" is 1Weight of "AB" = weight of "A" + weight of "B" (where, A and B are both independent valid parenthesis). e.g. weight of "()()" = weight of "()" + weight of "()"Weight of "(A)" = 2 times the weight of "A" (where
9 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg