← Home

241. Different Ways to Add Parentheses

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
241

Let's make this one less mysterious. For 241. Different Ways to Add Parentheses, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • Recursion, divide and conquer
  • https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/66328/A-recursive-Java-solution-(284-ms)

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are diffWaysToCompute.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Recursion, divide and conquer
02//https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/66328/A-recursive-Java-solution-(284-ms)
03//Runtime: 4 ms, faster than 84.42% of C++ online submissions for Different Ways to Add Parentheses.
04//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Different Ways to Add Parentheses.
05class Solution {
06public:
07    map<string, vector<int>> calculated;
08    vector<int> diffWaysToCompute(string input) {
09        int N = input.size();
10        vector<int> result;
11        for(int i = 0; i < N; i++){
12            char c = input[i];
13            if(c == '+' || c == '-' || c == '*'){
14                string sub1 = input.substr(0, i);
15                string sub2 = input.substr(i+1);
16                vector<int> v1 = calculated.find(sub1) == calculated.end() ? diffWaysToCompute(sub1) : calculated[sub1];
17                vector<int> v2 = calculated.find(sub2) == calculated.end() ? diffWaysToCompute(sub2) : calculated[sub2];
18                for(int i1 : v1){
19                    for(int i2 : v2){
20                        int r;
21                        switch(c){
22                            case '+':
23                                r = i1+i2;
24                                break;
25                            case '-':
26                                r = i1-i2;
27                                break;
28                            case '*':
29                                r = i1*i2;
30                                break;
31                        }
32                        result.push_back(r);
33                    }
34                }
35            }
36        }
37        //no operator inside input
38        if(result.size() == 0){
39            result.push_back(stoi(input));
40        }
41        calculated[input] = result;
42        return result;
43    }
44};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.