
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Different Ways to Add Parentheses in C++
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 −
#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, ]
- Related Articles
- Minimum Add to Make Parentheses Valid in Python
- Three Different ways to calculate factorial in C#
- Different ways to start a Task in C#
- What are the different ways in MySQL to add ‘half year interval’ in date?
- Different ways for Integer to String Conversions in C#
- Different ways to declare variable as constant in C and C++
- Valid Parentheses in C++
- Cost to Balance the parentheses in C++
- Different ways of Reading a file in C#
- Print system time in C++ (3 different ways)
- Score of Parentheses in C++
- Remove Invalid Parentheses in C++
- Number of Ways to Wear Different Hats to Each Other in C++
- Minimum Remove to Make Valid Parentheses in C++
- Different ways to concatenate Strings in Java
