Different Ways to Add Parentheses in C++

C++Server Side ProgrammingProgramming

Suppose we have a string of numbers and operators, we have to find all possible results from computing all the different possible ways to group the numbers and operators. Here the valid operators are +, - and *. So if the input is like “2*3-4*5”, then the output will be [-34, -14, -10, -10, 10]. This is because −

  • (2*(3-(4*5))) = -34

  • ((2*3)-(4*5)) = -14

  • ((2*(3-4))*5) = -10

  • (2*((3-4)*5)) = -10

  • (((2*3)-4)*5) = 10

To solve this, we will follow these steps −

  • Define a map called a memo.

  • Define a method called solve(). This will take the input string as input.

  • create an array called ret

  • if the memo has input, then return memo[input]

  • for i in range 0 to the size of input string −

    • if input[i] is any supported operator, then

      • an array part1 := solve(substring of input from 0 to i - 1)

      • an array part2 := solve(substring of input from i to end of string)

      • for j in range 0 to size of part1

        • for k in range 0 to size of part2

          • if input[i] is addition, then

            • perform part[j] + part[k] and add into ret

          • if input[i] is multiplication, then

            • perform part[j] * part[k] and add into ret

          • if input[i] is subtraction, then

            • perform part[j] - part[k] and add into ret

  • if ret is empty, then return input string as an integer

  • memo[input] := ret, and return ret

Example (C++)

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
   public:
   map <string, vector<int>> memo;
   vector<int> diffWaysToCompute(string input) {
      vector <int> ret;
      if(memo.count(input)) return memo[input];
      for(int i = 0; i < input.size(); i++){
         if(input[i] == '+' || input[i] == '*' || input[i] == '-'){
            vector <int> part1 = diffWaysToCompute(input.substr(0, i));
            vector <int> part2 = diffWaysToCompute(input.substr(i + 1));
            for(int j = 0; j < part1.size(); j++ ){
               for(int k = 0; k < part2.size(); k++){
                  if(input[i] == '+'){
                     ret.push_back(part1[j] + part2[k]);
                  }
                  else if(input[i] == '*'){
                     ret.push_back(part1[j] * part2[k]);
                  } else {
                     ret.push_back(part1[j] - part2[k]);
                  }
               }
            }
         }
      }
      if(ret.empty()){
         ret.push_back(stoi(input));
      }
      return memo[input] = ret;
   }
};
main(){
   Solution ob;
   print_vector(ob.diffWaysToCompute("2*3-4*5"));
}

Input

"2*3-4*5"

Output

[-34, -10, -14, -10, 10, ]
raja
Published on 02-May-2020 10:49:55
Advertisements