id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\n\n int SolveByMemo(string &s , int &k , int index , int prev , vector<vector<int>> &dp)\n {\n if(index == s.length())\n return 0;\n\n if(dp[index][prev] != -1)\n return dp[index][prev];\n\n int op1 = 0 + SolveByMemo(s, k , index+1 , prev , dp);\n\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index] - 'a' , dp);\n else if(abs(s[index] - 'a' - prev) <= k)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index] - 'a' , dp);\n\n return dp[index][prev] = max(op1 , op2);\n }\n\npublic:\n int longestIdealString(string s, int k) {\n \n // 1. Recursion + Memoization || Top Down Approach\n vector<vector<int>> dp(s.length() , vector<int> (27, -1));\n return SolveByMemo(s, k , 0 , 26, dp);\n\n // 2. Tabulation Method || Bottom Up Approach\n // return\n }\n};\n",
"memory": "176590"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\n\n int SolveByMemo(string &s , int &k , int index , int prev , vector<vector<int>> &dp)\n {\n if(index == s.length())\n return 0;\n\n if(dp[index][prev] != -1)\n return dp[index][prev];\n\n int op1 = 0 + SolveByMemo(s, k , index+1 , prev , dp);\n\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);\n else if(abs(s[index]-'a' - prev) <= k)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);\n\n return dp[index][prev] = max(op1 , op2);\n }\n\npublic:\n int longestIdealString(string s, int k) {\n \n // 1. Recursion + Memoization || Top Down Approach\n vector<vector<int>> dp(s.length() , vector<int> (27, -1));\n return SolveByMemo(s, k , 0 , 26, dp);\n\n // 2. Tabulation Method || Bottom Up Approach\n // return\n }\n};\n",
"memory": "181014"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\n\n int SolveByMemo(string &s , int &k , int index , int prev , vector<vector<int>> &dp)\n {\n if(index == s.length())\n return 0;\n\n if(dp[index][prev] != -1)\n return dp[index][prev];\n\n int op1 = 0 + SolveByMemo(s, k , index+1 , prev , dp);\n\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);\n else if(abs(s[index]-'a' - prev) <= k)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);\n\n return dp[index][prev] = max(op1 , op2);\n }\n\n int SolveByTab(string &s , int &k )\n {\n vector<vector<int>> dp(s.length()+1 , vector<int> (27, 0));\n \n for(int index = s.length()-1 ; index>=0 ; index--)\n {\n for(int prev = 0 ; prev<= 26 ; prev++)\n {\n int op1 = 0 + dp[index+1][prev];\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + dp[index+1][s[index]-'a'];\n else if(abs(s[index]-'a' - prev) <= k)\n op2 = 1 + dp[index+1][s[index]-'a'];\n\n dp[index][prev] = max(op1 , op2);\n }\n }\n\n return max(dp[0][s[0]-'a'] , dp[0][26]);\n }\n\n int SolveByTabSpaceOptimised(string &s , int &k )\n {\n vector<int> next(27, 0) ;\n vector<int> curr(27, 0);\n \n for(int index = s.length()-1 ; index>=0 ; index--)\n {\n for(int prev = 0 ; prev<= 26 ; prev++)\n {\n int op1 = 0 + next[prev];\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + next[s[index]-'a'];\n else if(abs(s[index]-'a' - prev) <= k)\n op2 = 1 + next[s[index]-'a'];\n\n curr[prev] = max(op1 , op2);\n }\n next = curr;\n }\n\n return max(curr[s[0]-'a'] , curr[26]);\n }\n\npublic:\n int longestIdealString(string s, int k) {\n \n // 1. Recursion + Memoization || Top Down Approach\n vector<vector<int>> dp(s.length() , vector<int> (27, -1));\n return SolveByMemo(s, k , 0 , 26, dp);\n\n // 2. Tabulation Method || Bottom Up Approach\n // return SolveByTab(s, k);\n\n // 3. Space Optimization using Tabulation\n // return SolveByTabSpaceOptimised(s, k);\n }\n};\n",
"memory": "185438"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\n\n int SolveByMemo(string &s , int &k , int index , int prev , vector<vector<int>> &dp)\n {\n if(index == s.length())\n return 0;\n\n if(dp[index][prev] != -1)\n return dp[index][prev];\n\n int op1 = 0 + SolveByMemo(s, k , index+1 , prev , dp);\n\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);\n else if(abs(s[index]-'a' - prev) <= k)\n op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);\n\n return dp[index][prev] = max(op1 , op2);\n }\n\n int SolveByTab(string &s , int &k )\n {\n vector<vector<int>> dp(s.length()+1 , vector<int> (27, 0));\n \n for(int index = s.length()-1 ; index>=0 ; index--)\n {\n for(int prev = 0 ; prev<= 26 ; prev++)\n {\n int op1 = 0 + dp[index+1][prev];\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + dp[index+1][s[index]-'a'];\n else if(abs(s[index]-'a' - prev) <= k)\n op2 = 1 + dp[index+1][s[index]-'a'];\n\n dp[index][prev] = max(op1 , op2);\n }\n }\n\n return max(dp[0][s[0]-'a'] , dp[0][26]);\n }\n\n int SolveByTabSpaceOptimised(string &s , int &k )\n {\n vector<int> next(27, 0) ;\n vector<int> curr(27, 0);\n \n for(int index = s.length()-1 ; index>=0 ; index--)\n {\n for(int prev = 0 ; prev<= 26 ; prev++)\n {\n int op1 = 0 + next[prev];\n int op2 = 0 ;\n\n if(prev == 26)\n op2 = 1 + next[s[index]-'a'];\n else if(abs(s[index]-'a' - prev) <= k)\n op2 = 1 + next[s[index]-'a'];\n\n curr[prev] = max(op1 , op2);\n }\n next = curr;\n }\n\n return max(curr[s[0]-'a'] , curr[26]);\n }\n\npublic:\n int longestIdealString(string s, int k) {\n \n // 1. Recursion + Memoization || Top Down Approach\n vector<vector<int>> dp(s.length() , vector<int> (27, -1));\n return SolveByMemo(s, k , 0 , 26, dp);\n\n // 2. Tabulation Method || Bottom Up Approach\n // return SolveByTab(s, k);\n\n // 3. Space Optimization using Tabulation\n // return SolveByTabSpaceOptimised(s, k);\n }\n};\n",
"memory": "185438"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>> dp;\nint solve(string &s , int &k , int i , int prev)\n{\n if(i == s.size()) return 0;\n if(dp[i][prev+1]!=-1) return dp[i][prev+1];\n int notpick = solve(s , k , i+1 , prev);\n int pick = 0;\n if(abs(s[i]-prev-'a') <=k || prev == -1 ){ pick = 1+solve(s , k , i+1 , s[i]-'a');}\n return dp[i][prev+1] = max(pick , notpick);\n \n}\n int longestIdealString(string s, int k) {\n dp = vector<vector<int>> (s.size()+1 , vector<int>(27 , -1));\n return solve(s , k , 0 , -1);\n }\n};",
"memory": "189861"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n private:\n int solve(int i, int c ,string &s,int k ,vector<vector<int>>&dp){\n if(i == s.size())return 0;\n if(dp[i][c] != -1)return dp[i][c];\n \n int take = 0;\n if(c == 27 || abs((s[i]-'a') - c ) <= k){\n take = 1 + solve(i+1,s[i]-'a',s,k,dp);\n }\n\n int nottake = solve(i+1,c,s,k,dp);\n\n return dp[i][c] = max(take,nottake);\n }\npublic:\n int longestIdealString(string s, int k) {\n int n = s.size();\n int ans = 0;\n vector<vector<int>>dp(n+1,vector<int>(28,-1));\n return solve(0,27,s,k,dp);\n }\n};",
"memory": "194285"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(int i,int p,string& s,int k,vector<vector<int>>& dp){\n if(i>=s.size()) return 0;\n if(dp[i][p+1]!=-1) return dp[i][p+1];\n int d=s[i]-'a';\n int ans=0,ans1=0;\n if(p==-1){\n ans=1+solve(i+1,s[i]-'a',s,k,dp);\n }else if(abs(d-p)<=k){\n ans=1+solve(i+1,s[i]-'a',s,k,dp);\n }\n ans1=solve(i+1,p,s,k,dp);\n return dp[i][p+1]=max(ans,ans1);\n }\n int longestIdealString(string s, int k) {\n vector<vector<int>> dp(s.size(),vector<int>(28,-1));\n return solve(0,-1,s,k,dp);\n }\n};",
"memory": "194285"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int rec(int i, int ch, string&s,int k,vector<vector<int>>&dp){\n int n=s.length();\n if(i==n){\n return 0;\n }\n\n if(dp[i][ch]!=-1){\n return dp[i][ch];\n }\n\n int ans=0;\n\n if(ch==30){ //picking first character\n ans=max(ans,1+rec(i+1,s[i]-'a',s,k,dp));\n ans=max(ans,rec(i+1,ch,s,k,dp));\n }else if(abs(ch-(s[i]-'a'))<=k){\n ans=max(ans,1+rec(i+1,s[i]-'a',s,k,dp));\n ans=max(ans,rec(i+1,ch,s,k,dp));\n }else{\n ans=max(ans,rec(i+1,ch,s,k,dp));\n }\n \n return dp[i][ch]=ans;\n }\n int longestIdealString(string s, int k) {\n int n=s.length();\n vector<vector<int>> dp(n+1,vector<int>(31,-1));\n return rec(0,30,s,k,dp);\n }\n};",
"memory": "198709"
} |
2,444 | <p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "acfgbd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= k <= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n private:\n int f(int t, char c, int k, string &s,vector<vector<int>>&dp){ \n int n = s.size();\n if(t<0){\n return 0;\n }\n if(dp[t][c-'a']!=-1){\n return dp[t][c-'a'];\n }\n int nt = f(t-1,c,k,s,dp);\n int tk = 0;\n if(c == 'z' + 1 || abs(c-s[t])<=k){\n tk = f(t-1,s[t],k,s,dp) + 1;\n }\n\n return dp[t][c-'a']=max(nt,tk);\n }\npublic:\n int longestIdealString(string s, int k) {\n int n = s.size();\n vector<vector<int>>dp(n+1,vector<int>(70,-1));\n return f(n-1,'z' + 1,k,s,dp);\n }\n};",
"memory": "203133"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n\n int dim1 = grid.size()-2;\n int dim2 = grid[0].size()-2;\n vector<vector<int>> ans(dim1,vector<int>(dim2,0));\n\n for(int x = 0; x < dim1; x++){\n for(int y = 0; y < dim2; y++){\n\n int currMax = 0;\n for(int i = x; i < x + 3; i++){\n for(int j = y; j < y + 3; j++){\n currMax = max(currMax, grid[i][j]);\n }\n }\n ans[x][y] = currMax;\n }\n }\n return ans;\n }\n};",
"memory": "13400"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n #define MAX(a, b) ((a) > (b) ? (a) : (b))\n #define MAX_3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))\n int m(grid.size()), n(grid[0].size());\n for (int i = 0; i < m; i ++) {\n vector<int> tmp(grid[i].begin(), grid[i].begin()+3);\n for (int j = 1; j < n-1; j ++) {\n tmp[(j+1) % 3] = grid[i][j+1];\n int m(MAX(tmp[(j-1) % 3], tmp[j % 3]));\n grid[i][j] = MAX(tmp[(j+1) % 3], m);\n }\n }\n vector<vector<int>> ans(m-2, vector<int>(n-2, 0));\n for (int j = 1; j < n-1; j ++) {\n for (int i = 1; i < m-1; i ++) {\n ans[i-1][j-1] = MAX_3(grid[i-1][j], grid[i][j], grid[i+1][j]);\n }\n }\n return ans;\n }\n};",
"memory": "13500"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int largestValue(vector<vector<int>>& grid, int r, int c)\n {\n int maxValue = 0;\n for (int i=r;i<r+3;i++)\n {\n for(int j=c; j<c+3; j++)\n {\n maxValue=max(maxValue,grid[i][j]);\n }\n }\n\n return maxValue;\n }\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> ans(n - 2, vector<int>(n - 2));\n\n for (int i = 0; i < n - 2; i++) {\n for (int j = 0; j < n - 2; j++)\n {\n ans[i][j] = largestValue(grid, i, j);\n }\n }\n\n return ans;\n }\n};",
"memory": "13600"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int highest_val(vector<vector<int>>&grid, int k, int l)\n {\n int high_val = 0;\n for(int i= k; i< k+3; i++)\n {\n for(int j=l; j< l+3; j++)\n {\n high_val = max(high_val, grid[i][j]);\n }\n }\n return high_val;\n }\n\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n\n int orig_size = grid.size();\n vector<vector<int>> maxLocal(orig_size - 2, vector<int>(orig_size - 2, 0));\n\n for(int i=0; i< orig_size-2; i++)\n {\n for(int j=0; j< orig_size-2; j++)\n {\n maxLocal[i][j] = highest_val(grid, i, j); \n }\n }\n return maxLocal;\n }\n};",
"memory": "13700"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findLocalMax(vector<vector<int>>& grid,int row,int col)\n {\n int maxVal=INT_MIN;\n for(int x=row;x<=row+2;x++)\n {\n for(int y=col;y<=col+2;y++)\n {\n maxVal=max(maxVal,grid[x][y]);\n }\n }\n return maxVal;\n }\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n=grid.size();\n vector<vector<int>> maxLocal(n-2,vector<int>(n-2));\n for(int row=0;row<n-2;row++)\n {\n for(int col=0;col<n-2;col++)\n {\n maxLocal[row][col]=findLocalMax(grid,row,col);\n }\n }\n return maxLocal;\n }\n};",
"memory": "13700"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int f(int i,int j,vector<vector<int>>& grid){\n int ans = INT_MIN;\n for(int x = i;x<i+3;x++){\n for(int y = j;y<j+3;y++){\n ans = max(ans,grid[x][y]);\n }\n }\n return ans;\n\n }\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> arr(n-2,vector<int>(n-2));\n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++){\n int max = f(i,j,grid);\n arr[i][j] = max;\n }\n }\n return arr;\n }\n};",
"memory": "13800"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\r\npublic:\r\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\r\n int n = grid.size();\r\n vector<vector<int>> res(n-2, vector<int>(n-2));\r\n for (int i=1; i < n-1; i++)\r\n {\r\n for (int j=1; j < n-1; j++)\r\n {\r\n res[i-1][j-1] = getMax(grid,i,j);\r\n }\r\n }\r\n return res;\r\n }\r\nprivate:\r\n int getMax(vector<vector<int>>& grid, int i, int j)\r\n {\r\n int max = INT_MIN;\r\n for (int k=-1; k<2;k++)\r\n {\r\n for (int n=-1; n<2; n++)\r\n {\r\n max = std::max(max,grid[i+k][j+n]);\r\n }\r\n }\r\n return max;\r\n }\r\n};",
"memory": "13800"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int f(int i,int j,vector<vector<int>>& grid){\n int ans = INT_MIN;\n for(int x = i;x<i+3;x++){\n for(int y = j;y<j+3;y++){\n ans = max(ans,grid[x][y]);\n }\n }\n return ans;\n\n }\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> arr(n-2,vector<int>(n-2));\n for(int i=0;i<n-2;i++){\n for(int j=0;j<n-2;j++){\n int max = f(i,j,grid);\n arr[i][j] = max;\n }\n }\n return arr;\n }\n};",
"memory": "13900"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int y = grid.size();\n std::vector<std::vector<int>> inner(y- 2);\n for (int i = 0; i < y - 2; i++){\n std::vector<int> row(y - 2);\n for (int j = 0; j < y- 2; j++){\n int mx = grid[i][j];\n for (int k = i; k <= i + 2; k++){\n for (int l = j; l <= j + 2; l++){\n mx = max(mx, grid[k][l]);\n }\n }\n row[j] = mx;\n }\n inner[i] = row;\n }\n return inner;\n }\n};",
"memory": "14000"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n #define MAX_3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))\n int m(grid.size()), n(grid[0].size());\n for (int i = 0; i < m; i ++) {\n vector<int> tmp(n);\n for (int j = 1; j < n-1; j ++) {\n tmp[j] = MAX_3(grid[i][j-1], grid[i][j], grid[i][j+1]);\n }\n swap(grid[i], tmp);\n }\n vector<vector<int>> ans(m-2, vector<int>(n-2, 0));\n for (int i = 1; i < m-1; i ++) {\n for (int j = 1; j < n-1; j ++) {\n ans[i-1][j-1] = MAX_3(grid[i-1][j], grid[i][j], grid[i+1][j]);\n }\n }\n return ans;\n }\n};",
"memory": "14100"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n #define MAX(a, b) ((a) > (b) ? (a) : (b))\n #define MAX_3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))\n int m(grid.size()), n(grid[0].size());\n for (int i = 0; i < m; i ++) {\n vector<int> tmp(n);\n for (int j = 1; j < n-1; j ++) {\n tmp[j] = MAX_3(grid[i][j-1], grid[i][j], grid[i][j+1]);\n }\n swap(grid[i], tmp);\n }\n vector<vector<int>> ans(m-2, vector<int>(n-2, 0));\n for (int j = 1; j < n-1; j ++) {\n for (int i = 1; i < m-1; i ++) {\n ans[i-1][j-1] = MAX_3(grid[i-1][j], grid[i][j], grid[i+1][j]);\n }\n }\n return ans;\n }\n};",
"memory": "14100"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> rowMax(n, vector<int>(n - 2));\n vector<vector<int>> maxLocal(n - 2, vector<int>(n - 2));\n\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n - 2; ++j) {\n rowMax[i][j] = max({grid[i][j], grid[i][j + 1], grid[i][j + 2]});\n }\n }\n\n for (int i = 0; i < n - 2; ++i) {\n for (int j = 0; j < n - 2; ++j) {\n maxLocal[i][j] = max({rowMax[i][j], rowMax[i + 1][j], rowMax[i + 2][j]});\n }\n }\n\n return maxLocal;\n }\n};",
"memory": "14200"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n\n //vector<vector<int>> answer(grid.size()-2, vector<int>(grid.size()-2));\n vector<vector<int>> answer;\n\n for(int i = 0; i < grid.size()-2; i++) {\n\n vector<int> arr;\n \n for(int j = 0; j < grid.size()-2; j++) {\n\n int greatest = 0;\n for(int k = i; k < i+3; k++) {\n for(int l = j; l < j+3; l++) { \n if(grid[k][l] > greatest) {\n greatest = grid[k][l];\n }\n }\n }\n arr.push_back(greatest);\n }\n answer.push_back(arr);\n \n }\n return answer;\n }\n};",
"memory": "14600"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n vector<vector<int>> res;\n\n int maxEle;\n for(int row = 0; row < grid.size() - 2; ++row){\n vector<int> tmp;\n for(int col = 0; col < grid[0].size() - 2; ++col){\n maxEle = 0;\n for(int i = row; i < row + 3; ++i)\n for(int j = col; j < col + 3; ++j)\n maxEle = max(maxEle, grid[i][j]);\n tmp.push_back(maxEle);\n }\n res.push_back(tmp);\n }\n\n return res;\n }\n};",
"memory": "14700"
} |
2,454 | <p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
<pre>
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
<strong>Output:</strong> [[9,9],[8,6]]
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 <= n <= 100</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n vector<vector<int> > ans;\n int n = grid.size();\n int m = grid[0].size();\n for(int i =0;i<=n-3;i++){\n vector<int>temp;\n for(int j =0;j<=m-3;j++){\n int maxi = grid[i][j];\n for(int k =i;k<=(i+2);k++){\n for( int p=j;p<=(j+2);p++){\n maxi = max(maxi,grid[k][p]);\n }\n }\n temp.push_back(maxi);\n\n\n }\n ans.push_back(temp);\n } \n return ans;\n }\n};",
"memory": "14700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> nums;\n vector<char> op;\n int size = expression.size();\n for(int i=0;i<size;) {\n int c = 0;\n while(i<size && !(expression[i]=='-' || expression[i]=='+' || expression[i]=='*')) {\n c = c*10 + expression[i]-'0';\n i++;\n }\n nums.push_back(c);\n if(i<size)\n op.push_back(expression[i++]);\n }\n int n = nums.size();\n vector<int> dp[n][n];\n for(int i=0;i<n;i++)\n dp[i][i].push_back(nums[i]);\n for(int cl=1;cl<n;cl++) {\n int i = 0;\n int j = cl;\n while(j<n) {\n for(int k=i;k<j;k++) {\n for(auto &x:dp[i][k]) {\n for(auto &y:dp[k+1][j]) {\n if(op[k]=='-')\n dp[i][j].push_back(x-y);\n else if(op[k]=='+')\n dp[i][j].push_back(x+y);\n else\n dp[i][j].push_back(x*y);\n }\n }\n }\n i++;\n j++;\n }\n }\n return dp[0][n-1];\n }\n};",
"memory": "7800"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> data;\n vector<char> ops;\n istringstream ss(expression+\"+\");// \"+\"便于while()转换,不会用到\n int num = 0;\n char op = ' ';\n while(ss >> num && ss >> op) {\n data.push_back(num);\n ops.push_back(op);\n }\n int n = data.size();\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>()));\n for(int i = 0; i < n; ++i) { \n for(int j = i; j >=0; --j) {//以i为结尾,j为开头的表达式\n if(i == j) dp[i][j].push_back(data[i]);\n else {\n for(int k = j; k < i; ++k) {// 操作符位置。\n for(auto left : dp[j][k]) {\n for(auto right : dp[k+1][i]) {\n int val = 0;\n switch(ops[k]) {\n case '+': val = left + right;break;\n case '-': val = left - right;break;\n case '*': val = left * right;break;\n }\n dp[j][i].push_back(val);\n }\n }\n }\n \n }\n }\n }\n return dp[0][n-1];\n }\n};",
"memory": "7900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int func(int f,int s, char op){\n if(op=='+'){\n return f+s;\n }\n if(op=='-'){\n return f-s;\n }\n if(op=='*'){\n return f*s;\n }\n return 0;\n }\n vector<int> diffWaysToCompute(string s) {\n vector<int>ans;\n int n=s.size();\n vector<int>num;\n vector<char>op;\n string st=\"\";\n for(int i=0;i<n;i++){\n if(s[i]=='+' || s[i]=='-' || s[i]=='*'){\n int x=stoi(st);\n num.push_back(x);\n st=\"\";\n op.push_back(s[i]);\n }else{\n st+=s[i];\n }\n }\n if(st!=\"\"){\n int x=stoi(st);\n num.push_back(x);\n }\n n=num.size();\n // vector<vector<int>>dp(n,vector<int>(n,0));\n vector<int> dp[n][n];\n for(int i=0;i<n;i++){\n dp[i][i].push_back(num[i]);\n }\n for(int len=2;len<=n;len++){\n for(int i=0;i<n;i++){\n int j=i+len-1;\n if(j>=n) continue;\n for(int k=i;k<j;k++){\n char opera=op[k];\n for(auto &f:dp[i][k]){\n for(auto &s:dp[k+1][j]){\n int result=func(f,s,opera);\n dp[i][j].push_back(result);\n }\n }\n // if(len==n){\n // ans.push_back(result);\n // cout<<len<<\" \"<<i<<\" \"<<k<<\" \"<<j<<\" \"<<result<<endl;\n // }\n }\n }\n }\n for(auto &it:dp[0][n-1]){\n ans.push_back(it);\n }\n return ans;\n }\n};",
"memory": "8000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.length();\n // Create a 2D array of vectors to store results of subproblems\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n));\n\n // Initialize the dp array with empty vectors\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n dp[i][j] = vector<int>();\n }\n }\n\n // Handle base cases: single digits and two-digit numbers\n for (int i = 0; i < n; i++) {\n if (isdigit(expression[i])) {\n // Check if it's a two-digit number\n int dig1 = expression[i] - '0';\n if (i + 1 < n && isdigit(expression[i + 1])) {\n int dig2 = expression[i + 1] - '0';\n int number = dig1 * 10 + dig2;\n dp[i][i + 1].push_back(number);\n }\n // Single digit case\n dp[i][i].push_back(expression[i] - '0');\n }\n }\n\n // Fill the dp table for all possible subexpressions\n for (int length = 3; length <= n; length++) {\n for (int start = 0; start + length - 1 < n; start++) {\n int end = start + length - 1;\n // Try all possible positions to split the expression\n for (int split = start; split < end; split++) {\n if (isdigit(expression[split])) continue;\n\n vector<int> leftResults = dp[start][split - 1];\n vector<int> rightResults = dp[split + 1][end];\n\n // Compute results based on the operator at position 'split'\n for (int leftValue : leftResults) {\n for (int rightValue : rightResults) {\n switch (expression[split]) {\n case '+':\n dp[start][end].push_back(leftValue +\n rightValue);\n break;\n case '-':\n dp[start][end].push_back(leftValue -\n rightValue);\n break;\n case '*':\n dp[start][end].push_back(leftValue *\n rightValue);\n break;\n }\n }\n }\n }\n }\n }\n\n // Return the results for the entire expression\n return dp[0][n - 1];\n }\n};",
"memory": "8000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // function calculates the result for each combination of left and right subarray\n void calVal(vector<int>& target, const vector<int>& ls, const vector<int>& rs, char op) {\n // passing in all the arguments as references to avoid return type\n for (const auto& l : ls) {\n for (const auto& r : rs) {\n switch (op) {\n case '+': target.push_back(l + r); break;\n case '-': target.push_back(l - r); break;\n case '*': target.push_back(l * r); break;\n }\n }\n }\n }\n\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> nums; \n vector<char> ops; \n\n int curr = 0;\n // separate the numbers and operators\n for (const auto& ch : expression) {\n if (isdigit(ch)) {\n curr = curr * 10 + (ch - '0'); \n } \n else {\n nums.push_back(curr); \n curr = 0; \n ops.push_back(ch); \n }\n }\n // add last number\n nums.push_back(curr); \n\n int n = nums.size();\n\n // 3d table to store the results\n vector<vector<vector<int>>> out(n, vector<vector<int>>(n));\n // put items into the table\n for (int len = 0; len < n; ++len) { \n for (int i = 0; i < n - len; ++i) {\n int j = i + len; \n if (i == j) {\n // single number\n out[i][j].push_back(nums[i]); \n } \n else {\n for (int k = i; k < j; ++k) {\n // merge results from left and right\n calVal(out[i][j], out[i][k], out[k + 1][j], ops[k]);\n }\n }\n }\n }\n\n return out[0][n - 1];\n }\n};",
"memory": "8100"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.size();\n vector<vector<vector<int>>> memoization(n+1, vector<vector<int>>(n+1, vector<int>()));\n diffWaysToCompute(expression, 0, n, memoization);\n return memoization[0][n];\n }\n void diffWaysToCompute(string& expression, int low, int high, vector<vector<vector<int>>>& memoization) {\n for(int i = low; i < high; ++i) {\n char c = expression[i];\n if(c == '+' || c == '-' || c == '*') {\n if(memoization[low][i].empty()) \n diffWaysToCompute(expression, low, i, memoization);\n if(memoization[i+1][high].empty())\n diffWaysToCompute(expression, i+1, high, memoization);\n int val = 0;\n for(auto l : memoization[low][i]) {\n for(auto r : memoization[i+1][high]) {\n switch(c) {\n case '+' : val = l + r; break;\n case '-' : val = l - r; break;\n case '*' : val = l * r; break;\n }\n memoization[low][high].push_back(val);\n }\n }\n }\n }\n if (memoization[low][high].empty()) \n memoization[low][high].push_back(stoi(expression.substr(low, high)));\n\n }\n};",
"memory": "8200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void compute(std::vector<std::vector<std::vector<int>>>& vals, std::vector<int>& nums,\n std::vector<char>& ops, int l, int r) {\n std::cout << l << \" \" << r << std::endl;\n if (!vals[l][r].empty()) {\n return;\n }\n if (l == r) {\n vals[l][r].push_back(nums[l]);\n return;\n }\n\n for (int o = l; o <= r - 1; ++o) {\n compute(vals, nums, ops, l, o);\n compute(vals, nums, ops, o+1, r);\n if (ops[o] == '+') {\n for (int lv : vals[l][o]) {\n for (int rv : vals[o+1][r]) {\n vals[l][r].push_back(lv + rv);\n } \n }\n } else if (ops[o] == '-') {\n for (int lv : vals[l][o]) {\n for (int rv : vals[o+1][r]) {\n vals[l][r].push_back(lv - rv);\n } \n }\n } else {\n for (int lv : vals[l][o]) {\n for (int rv : vals[o+1][r]) {\n vals[l][r].push_back(lv * rv);\n } \n }\n }\n }\n }\n\n vector<int> diffWaysToCompute(string expression) {\n istringstream buf{expression};\n std::vector<int> nums;\n std::vector<char> ops;\n\n int n; buf >> n;\n nums.push_back(n);\n while (buf.peek() != EOF) {\n int n; char c;\n buf >> c >> n;\n nums.push_back(n);\n ops.push_back(c);\n }\n\n std::cout << nums.size() << std::endl;\n std::cout << ops.size() << std::endl;\n\n std::vector<std::vector<std::vector<int>>> vals;\n for (int i = 0; i < nums.size(); i++) {\n vals.push_back(std::vector<std::vector<int>>(nums.size()));\n }\n\n compute(vals, nums, ops, 0, nums.size() - 1);\n return std::vector<int>(vals[0][nums.size() - 1].begin(), vals[0][nums.size() - 1].end());\n\n }\n};",
"memory": "8300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int compute(int a,int b,char c){\n if(c=='*')\n return a*b;\n if(c=='+')\n return a+b;\n return a-b;\n }\n bool isOperator(char ch){\n if(ch=='+'||ch=='-'||ch=='*')\n return true;\n return false;\n }\n vector<int> diffWaysToCompute(string expression) {\n int n=expression.length();\n vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1));\n for(int i=0;i<n;i++){\n if(!isOperator(expression[i]))\n dp[i][i]={expression[i]-'0'};\n }\n for(int i=0;i<n-1;i++){\n if((i==0||isOperator(expression[i-1])) && (i==n-2 || isOperator(expression[i+2])))\n dp[i][i+1]={stoi(expression.substr(i,2))};\n }\n for(int l=3;l<=n;l++){\n for(int i=0;i<=n-l;i++){\n for(int j=i;j<l+i;j++){\n if(isOperator(expression[j])){\n vector<int> left=dp[i][j-1];\n vector<int> right=dp[j+1][l+i-1];\n for(int t1:left){\n for(int t2:right){\n dp[i][l+i-1].push_back(compute(t1,t2,expression[j]));\n }\n }\n }\n }\n }\n }\n return dp[0][n-1];\n }\n};",
"memory": "8300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.length();\n // Create a 2D array of lists to store results of subproblems\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n));\n\n initializeBaseCases(expression, dp);\n\n // Fill the dp table for all possible subexpressions\n for (int length = 3; length <= n; length++) {\n for (int start = 0; start + length - 1 < n; start++) {\n int end = start + length - 1;\n processSubexpression(expression, dp, start, end);\n }\n }\n\n // Return the results for the entire expression\n return dp[0][n - 1];\n }\n\nprivate:\n void initializeBaseCases(string& expression,\n vector<vector<vector<int>>>& dp) {\n int n = expression.length();\n // Handle base cases: single digits and two-digit numbers\n for (int i = 0; i < n; i++) {\n if (isdigit(expression[i])) {\n // Check if it's a two-digit number\n int dig1 = expression[i] - '0';\n if (i + 1 < n && isdigit(expression[i + 1])) {\n int dig2 = expression[i + 1] - '0';\n int number = dig1 * 10 + dig2;\n dp[i][i + 1].push_back(number);\n }\n // Single digit case\n dp[i][i].push_back(dig1);\n }\n }\n }\n\n void processSubexpression(string& expression,\n vector<vector<vector<int>>>& dp, int start,\n int end) {\n // Try all possible positions to split the expression\n for (int split = start; split <= end; split++) {\n if (isdigit(expression[split])) continue;\n\n vector<int> leftResults = dp[start][split - 1];\n vector<int> rightResults = dp[split + 1][end];\n\n computeResults(expression[split], leftResults, rightResults,\n dp[start][end]);\n }\n }\n\n void computeResults(char op, vector<int>& leftResults,\n vector<int>& rightResults, vector<int>& results) {\n // Compute results based on the operator at position 'split'\n for (int leftValue : leftResults) {\n for (int rightValue : rightResults) {\n switch (op) {\n case '+':\n results.push_back(leftValue + rightValue);\n break;\n case '-':\n results.push_back(leftValue - rightValue);\n break;\n case '*':\n results.push_back(leftValue * rightValue);\n break;\n }\n }\n }\n }\n};",
"memory": "8400"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string s) {\n \n vector< pair<char, int> > arr;\n\n int n = s.length();\n\n for( int i=0; i<n; i++) {\n if((s[i]=='+') || (s[i]=='-') || (s[i]=='*')) {\n arr.push_back({ s[i], 0});\n } else {\n int j = i;\n while((j+1<n) && isdigit(s[j+1])) {\n j += 1;\n }\n int v = stoi(s.substr( i, j-i+1));\n i=j;\n arr.push_back({ '$', v});\n }\n }\n\n int m = arr.size();\n\n vector< vector< vector<int> > > dp( m, vector< vector<int> >( m, vector<int>()));\n\n auto eval = []( char op, int lval, int rval)->int {\n if(op=='+') {\n return lval + rval;\n } else if(op=='-') {\n return lval - rval;\n } else {\n return lval*rval;\n }\n };\n\n function<void(int,int)> solve;\n\n solve = [&]( int sid, int eid)->void {\n if(dp[sid][eid].empty()) {\n if(sid==eid) {\n dp[sid][eid].push_back(arr[sid].second);\n } else {\n for( int i=sid+1; i<=eid; i+=2) {\n solve( sid, i-1);\n solve( i+1, eid);\n char op = arr[i].first;\n for( int lv: dp[sid][i-1]) {\n for( int rv: dp[i+1][eid]) {\n dp[sid][eid].push_back(eval(op, lv, rv));\n }\n }\n }\n }\n }\n };\n\n solve( 0, m-1);\n\n vector<int> res;\n for( int val: dp[0][m-1]) {\n res.push_back(val);\n }\n\n return res;\n }\n};",
"memory": "8400"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.length();\n // Create a 2D array of lists to store results of subproblems\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n));\n\n initializeBaseCases(expression, dp);\n\n // Fill the dp table for all possible subexpressions\n for (int length = 3; length <= n; length++) {\n for (int start = 0; start + length - 1 < n; start++) {\n int end = start + length - 1;\n processSubexpression(expression, dp, start, end);\n }\n }\n\n // Return the results for the entire expression\n return dp[0][n - 1];\n }\n\nprivate:\n void initializeBaseCases(string& expression,\n vector<vector<vector<int>>>& dp) {\n int n = expression.length();\n // Handle base cases: single digits and two-digit numbers\n for (int i = 0; i < n; i++) {\n if (isdigit(expression[i])) {\n // Check if it's a two-digit number\n int dig1 = expression[i] - '0';\n if (i + 1 < n && isdigit(expression[i + 1])) {\n int dig2 = expression[i + 1] - '0';\n int number = dig1 * 10 + dig2;\n dp[i][i + 1].push_back(number);\n }\n // Single digit case\n dp[i][i].push_back(dig1);\n }\n }\n }\n\n void processSubexpression(string& expression,\n vector<vector<vector<int>>>& dp, int start,\n int end) {\n // Try all possible positions to split the expression\n for (int split = start; split <= end; split++) {\n if (isdigit(expression[split])) continue;\n\n vector<int> leftResults = dp[start][split - 1];\n vector<int> rightResults = dp[split + 1][end];\n\n computeResults(expression[split], leftResults, rightResults,\n dp[start][end]);\n }\n }\n\n void computeResults(char op, vector<int>& leftResults,\n vector<int>& rightResults, vector<int>& results) {\n // Compute results based on the operator at position 'split'\n for (int leftValue : leftResults) {\n for (int rightValue : rightResults) {\n switch (op) {\n case '+':\n results.push_back(leftValue + rightValue);\n break;\n case '-':\n results.push_back(leftValue - rightValue);\n break;\n case '*':\n results.push_back(leftValue * rightValue);\n break;\n }\n }\n }\n }\n};",
"memory": "8500"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.length();\n // Create a 2D array of lists to store results of subproblems\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n));\n\n initializeBaseCases(expression, dp);\n\n // Fill the dp table for all possible subexpressions\n for (int length = 3; length <= n; length++) {\n for (int start = 0; start + length - 1 < n; start++) {\n int end = start + length - 1;\n processSubexpression(expression, dp, start, end);\n }\n }\n\n // Return the results for the entire expression\n return dp[0][n - 1];\n }\n\nprivate:\n void initializeBaseCases(string& expression,\n vector<vector<vector<int>>>& dp) {\n int n = expression.length();\n // Handle base cases: single digits and two-digit numbers\n for (int i = 0; i < n; i++) {\n if (isdigit(expression[i])) {\n // Check if it's a two-digit number\n int dig1 = expression[i] - '0';\n if (i + 1 < n && isdigit(expression[i + 1])) {\n int dig2 = expression[i + 1] - '0';\n int number = dig1 * 10 + dig2;\n dp[i][i + 1].push_back(number);\n }\n // Single digit case\n dp[i][i].push_back(dig1);\n }\n }\n }\n\n void processSubexpression(string& expression,\n vector<vector<vector<int>>>& dp, int start,\n int end) {\n // Try all possible positions to split the expression\n for (int split = start; split <= end; split++) {\n if (isdigit(expression[split])) continue;\n\n vector<int> leftResults = dp[start][split - 1];\n vector<int> rightResults = dp[split + 1][end];\n\n computeResults(expression[split], leftResults, rightResults,\n dp[start][end]);\n }\n }\n\n void computeResults(char op, vector<int>& leftResults,\n vector<int>& rightResults, vector<int>& results) {\n // Compute results based on the operator at position 'split'\n for (int leftValue : leftResults) {\n for (int rightValue : rightResults) {\n switch (op) {\n case '+':\n results.push_back(leftValue + rightValue);\n break;\n case '-':\n results.push_back(leftValue - rightValue);\n break;\n case '*':\n results.push_back(leftValue * rightValue);\n break;\n }\n }\n }\n }\n};",
"memory": "8500"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n return helper(expression, 0, expression.size()-1);\n }\n\n vector<int> helper(string &expression, int st, int ed) {\n vector<int> ans;\n\n bool hasSymbol = false;\n\n for(int k=st+1;k<ed;k++) {\n if (isSymbol(expression[k])) {\n if (dp[st][k-1].empty()) {\n dp[st][k-1] = helper(expression, st, k-1);\n }\n if (dp[k+1][ed].empty()) {\n dp[k+1][ed] = helper(expression, k+1, ed);\n }\n for(auto p: dp[st][k-1]) {\n for(auto q: dp[k+1][ed]) {\n ans.push_back(performOperation(expression[k], p, q));\n }\n }\n hasSymbol = true;\n }\n }\n\n if (!hasSymbol) {\n return {stoi(expression.substr(st, ed-st+1))};\n }\n\n return ans;\n }\n\n int performOperation(char c, int a, int b) {\n switch(c) {\n case '-': return a-b;\n case '+': return a+b;\n case '*': return a*b;\n default : return -1;\n }\n }\n\n bool isSymbol(char c) {\n return c=='-' || c == '+' || c =='*';\n }\n\n vector<int> dp[20][20];\n};\n\n/**\n\n\n\n*/",
"memory": "8600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.length();\n // Create a 2D array of lists to store results of subproblems\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n));\n\n initializeBaseCases(expression, dp);\n\n // Fill the dp table for all possible subexpressions\n for (int length = 3; length <= n; length++) {\n for (int start = 0; start + length - 1 < n; start++) {\n int end = start + length - 1;\n processSubexpression(expression, dp, start, end);\n }\n }\n\n // Return the results for the entire expression\n return dp[0][n - 1];\n }\n\nprivate:\n void initializeBaseCases(string& expression,\n vector<vector<vector<int>>>& dp) {\n int n = expression.length();\n // Handle base cases: single digits and two-digit numbers\n for (int i = 0; i < n; i++) {\n if (isdigit(expression[i])) {\n // Check if it's a two-digit number\n int dig1 = expression[i] - '0';\n if (i + 1 < n && isdigit(expression[i + 1])) {\n int dig2 = expression[i + 1] - '0';\n int number = dig1 * 10 + dig2;\n dp[i][i + 1].push_back(number);\n }\n // Single digit case\n dp[i][i].push_back(dig1);\n }\n }\n }\n\n void processSubexpression(string& expression,\n vector<vector<vector<int>>>& dp, int start,\n int end) {\n // Try all possible positions to split the expression\n for (int split = start; split <= end; split++) {\n if (isdigit(expression[split])) continue;\n\n vector<int> leftResults = dp[start][split - 1];\n vector<int> rightResults = dp[split + 1][end];\n\n computeResults(expression[split], leftResults, rightResults,\n dp[start][end]);\n }\n }\n\n void computeResults(char op, vector<int>& leftResults,\n vector<int>& rightResults, vector<int>& results) {\n // Compute results based on the operator at position 'split'\n for (int leftValue : leftResults) {\n for (int rightValue : rightResults) {\n switch (op) {\n case '+':\n results.push_back(leftValue + rightValue);\n break;\n case '-':\n results.push_back(leftValue - rightValue);\n break;\n case '*':\n results.push_back(leftValue * rightValue);\n break;\n }\n }\n }\n }\n};",
"memory": "8600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evaluate(const int left, const int right, const char op) {\n switch (op) {\n case '+': return left + right;\n case '-': return left - right;\n default: return left * right;\n }\n }\n\n vector<int> helper(const int n, const vector<int> &nums, const vector<char>& ops, const int left, const int right, vector<vector<int>> &cache) {\n if (left == right)\n return {nums[left]};\n if (right - left == 1)\n return {evaluate(nums[left], nums[right], ops[left])};\n const int idx = left * n + right;\n if (!cache[idx].empty())\n return cache[idx];\n vector<int> ret;\n for (int i = left; i < right; ++i) {\n auto prefix = helper(n, nums, ops, left, i, cache);\n auto suffix = helper(n, nums, ops, i + 1, right, cache);\n for (const auto p : prefix)\n for (const auto s : suffix)\n ret.push_back(evaluate(p, s, ops[i]));\n }\n return cache[idx] = ret;\n }\n\n vector<int> diffWaysToCompute(const string& expression) {\n vector<int> nums;\n vector<char> ops;\n int num = 0;\n for (char c : expression)\n if (isdigit(c))\n num = num * 10 + (c - '0');\n else {\n nums.push_back(num);\n ops.push_back(c);\n num = 0;\n }\n nums.push_back(num);\n const int n = (int)nums.size();\n vector<vector<int>> cache(n * n);\n return helper(n, nums, ops, 0, n - 1, cache);\n }\n};",
"memory": "8700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n // Initialize memoization vector to store results of subproblems\n vector<vector<vector<int>>> memo(\n expression.length(), vector<vector<int>>(expression.length()));\n // Solve for the entire expression\n return computeResults(expression, memo, 0, expression.length() - 1);\n }\n\nprivate:\n vector<int> computeResults(string& expression,\n vector<vector<vector<int>>>& memo, int start,\n int end) {\n // If result is already memoized, return it\n if (!memo[start][end].empty()) {\n return memo[start][end];\n }\n\n vector<int> results;\n\n // Base case: single digit\n if (start == end) {\n results.push_back(expression[start] - '0');\n return results;\n }\n\n // Base case: two-digit number\n if (end - start == 1 && isdigit(expression[start])) {\n int tens = expression[start] - '0';\n int ones = expression[end] - '0';\n results.push_back(10 * tens + ones);\n return results;\n }\n\n // Recursive case: split the expression at each operator\n for (int i = start; i <= end; i++) {\n char currentChar = expression[i];\n if (isdigit(currentChar)) {\n continue;\n }\n\n vector<int> leftResults =\n computeResults(expression, memo, start, i - 1);\n vector<int> rightResults =\n computeResults(expression, memo, i + 1, end);\n\n // Combine results from left and right subexpressions\n for (int leftValue : leftResults) {\n for (int rightValue : rightResults) {\n switch (currentChar) {\n case '+':\n results.push_back(leftValue + rightValue);\n break;\n case '-':\n results.push_back(leftValue - rightValue);\n break;\n case '*':\n results.push_back(leftValue * rightValue);\n break;\n }\n }\n }\n }\n\n // Memoize the result for this subproblem\n memo[start][end] = results;\n\n return results;\n }\n};",
"memory": "8800"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nvector<int> rec(int start,int end,string &e,vector<vector<vector<int>>>&dp){\n if(!dp[start][end].empty())return dp[start][end];\n vector<int>ans;\n if(start==end){\n ans.push_back(e[start]-'0');\n return ans;\n }\n if(end==start+1 && isdigit(e[start])){\n ans.push_back((e[start]-'0')*10+(e[end]-'0'));\n return ans;\n }\n for(int i=start;i<=end;i++){\n char cur=e[i];\n if(isdigit(cur)){\n continue;\n }\n \n vector<int>left=rec(start,i-1,e,dp);\n vector<int>right=rec(i+1,end,e,dp);\n for(auto j:left){\n for(int k:right){\n switch(cur){\n case '+':{ans.push_back(j+k);break;}\n case '-':{ans.push_back(j-k);break;}\n case '*':{ans.push_back(j*k);break;}\n }\n }\n }\n }\n dp[start][end]=ans;\n return ans;\n }\n\n\n vector<int> diffWaysToCompute(string e) {\n vector<vector<vector<int>>>dp(e.length(), vector<vector<int>>(e.length()));\n \n return rec(0,e.size()-1,e,dp);\n }\n};",
"memory": "8900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> dp[20][20];\n vector<int> convert(string& expression){\n vector<int> ans;\n int sz=expression.size();\n ans.reserve(sz);\n int x=0;\n for(int i=0; i<sz; i++){\n const char c=expression[i];\n switch(c){\n case '+': ans.push_back(101); break;// 101 for +\n case '-': ans.push_back(102); break;// 102 for -\n case '*': ans.push_back(103); break;// 103 for *\n default:{\n x=10*x+c-'0';\n if (i==sz-1 || !isdigit(expression[i+1])){\n ans.push_back(x);\n x=0;\n }\n }\n }\n }\n return ans;\n }\n\n vector<int> f(int l, int r, vector<int>& nums){\n if (l>r) return {};\n if (l==r){\n if (nums[l]>=101) return {};\n return {nums[l]};\n }\n if (dp[l][r].size()>0) return dp[l][r];\n\n vector<int> ans;\n for(int i=l; i<=r; i++){\n if (nums[i]<100) continue;\n vector<int> Left=f(l, i-1, nums);\n vector<int> Right=f(i+1, r, nums);\n for(int L: Left)\n for(int R: Right){\n int x=0;\n switch(nums[i]){\n case 101: ans.push_back(L+R); break;\n case 102: ans.push_back(L-R); break;\n case 103: ans.push_back(L*R); break;\n }\n }\n }\n return dp[l][r]=ans;\n }\n\n vector<int> diffWaysToCompute(string& expression) {\n vector<int> nums=convert(expression);\n int n=nums.size();\n return f(0, n-1, nums);\n }\n};",
"memory": "9000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> dp[20][20];\n vector<int> convert(string& expression){\n vector<int> ans;\n int sz=expression.size();\n ans.reserve(sz);\n int x=0;\n for(int i=0; i<sz; i++){\n const char c=expression[i];\n switch(c){\n case '+': ans.push_back(101); break;// 101 for +\n case '-': ans.push_back(102); break;// 102 for -\n case '*': ans.push_back(103); break;// 103 for *\n default:{\n x=10*x+c-'0';\n if (i==sz-1 || !isdigit(expression[i+1])){\n ans.push_back(x);\n x=0;\n }\n }\n }\n }\n return ans;\n }\n\n vector<int> f(int l, int r, vector<int>& nums){\n if (l>r) return {};\n if (l==r){\n if (nums[l]>=101) return {};\n return {nums[l]};\n }\n if (dp[l][r].size()>0) return dp[l][r];\n\n vector<int> ans;\n for(int i=l; i<=r; i++){\n if (nums[i]<100) continue;\n vector<int> Left=f(l, i-1, nums);\n vector<int> Right=f(i+1, r, nums);\n for(int L: Left)\n for(int R: Right){\n int x=0;\n switch(nums[i]){\n case 101: ans.push_back(L+R); break;\n case 102: ans.push_back(L-R); break;\n case 103: ans.push_back(L*R); break;\n }\n }\n }\n return dp[l][r]=ans;\n }\n\n vector<int> diffWaysToCompute(string& expression) {\n vector<int> nums=convert(expression);\n int n=nums.size();\n return f(0, n-1, nums);\n }\n};",
"memory": "9000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> dp[20][20];\n vector<int> convert(string& expression){\n vector<int> ans;\n int sz=expression.size();\n ans.reserve(sz);\n int x=0;\n for(int i=0; i<sz; i++){\n const char c=expression[i];\n switch(c){\n case '+': ans.push_back(101); break;// 101 for +\n case '-': ans.push_back(102); break;// 102 for -\n case '*': ans.push_back(103); break;// 103 for *\n default:{\n x=10*x+c-'0';\n if (i==sz-1 || !isdigit(expression[i+1])){\n ans.push_back(x);\n x=0;\n }\n }\n }\n }\n return ans;\n }\n\n vector<int> f(int l, int r, vector<int>& nums){\n if (l>r) return {};\n if (l==r){\n if (nums[l]>=101) return {};\n return {nums[l]};\n }\n if (dp[l][r].size()>0) return dp[l][r];\n\n vector<int> ans;\n for(int i=l; i<=r; i++){\n if (nums[i]<100) continue;\n vector<int> Left=f(l, i-1, nums);\n vector<int> Right=f(i+1, r, nums);\n for(int L: Left)\n for(int R: Right){\n int x=0;\n switch(nums[i]){\n case 101: ans.push_back(L+R); break;\n case 102: ans.push_back(L-R); break;\n case 103: ans.push_back(L*R); break;\n }\n }\n }\n return dp[l][r]=ans;\n }\n\n vector<int> diffWaysToCompute(string& expression) {\n vector<int> nums=convert(expression);\n int n=nums.size();\n return f(0, n-1, nums);\n }\n};\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "9100"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n map<pair<int,int>, vector<int>> dp;\n int calculate(int x, int y, char opr){\n int ans;\n switch(opr){\n case '+':\n ans = x + y;\n break;\n case '-':\n ans = x - y;\n break;\n case '*':\n ans = x * y;\n break;\n default:\n break;\n }\n return ans;\n }\n vector<int> rec(string &expression, int i, int j){\n if(dp.count({i,j})) return dp[{i,j}];\n\n bool isNumber = true;\n vector<int> ans;\n for(int k = i; k < j; k++){\n if(!isdigit(expression[k])){\n vector<int> left = rec(expression, i, k-1);\n vector<int> right = rec(expression, k+1, j);\n\n for(auto x : left){\n for(auto y : right){\n ans.push_back(calculate(x, y, expression[k]));\n }\n }\n\n isNumber = false;\n }\n }\n if(isNumber){\n ans.push_back(stoi(expression.substr(i,j-i+1)));\n }\n return dp[{i,j}] = ans;\n }\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.size();\n return rec(expression, 0, n-1);\n }\n};",
"memory": "9100"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> dp[20][20];\n vector<int> convert(string& expression){\n vector<int> ans;\n int sz=expression.size();\n ans.reserve(sz);\n int x=0;\n for(int i=0; i<sz; i++){\n const char c=expression[i];\n switch(c){\n case '+': ans.push_back(101); break;// 101 for +\n case '-': ans.push_back(102); break;// 102 for -\n case '*': ans.push_back(103); break;// 103 for *\n default:{\n x=10*x+c-'0';\n if (i==sz-1 || !isdigit(expression[i+1])){\n ans.push_back(x);\n x=0;\n }\n }\n }\n }\n return ans;\n }\n\n vector<int> f(int l, int r, vector<int>& nums){\n if (l>r) return {};\n if (l==r){\n if (nums[l]>=101) return {};\n return {nums[l]};\n }\n if (dp[l][r].size()>0) return dp[l][r];\n\n vector<int> ans;\n for(int i=l; i<=r; i++){\n if (nums[i]<100) continue;\n vector<int> Left=f(l, i-1, nums);\n vector<int> Right=f(i+1, r, nums);\n for(int L: Left)\n for(int R: Right){\n int x=0;\n switch(nums[i]){\n case 101: ans.push_back(L+R); break;\n case 102: ans.push_back(L-R); break;\n case 103: ans.push_back(L*R); break;\n }\n }\n }\n return dp[l][r]=ans;\n }\n\n vector<int> diffWaysToCompute(string& expression) {\n vector<int> nums=convert(expression);\n int n=nums.size();\n return f(0, n-1, nums);\n }\n};\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "9200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // Memoization map to store already computed results\n unordered_map<string, vector<int>> map;\n\n vector<int> diffWaysToCompute(string expression) {\n // Step 1: Check if the result for the current expression is already computed\n if (map.find(expression) != map.end()) {\n return map[expression]; // Return cached result\n }\n\n vector<int> result;\n\n // Step 2: Traverse the expression to find operators (+, -, *)\n for (int i = 0; i < expression.length(); i++) {\n char ch = expression[i];\n\n // Step 3: If the character is an operator, split the expression into left and right parts\n if (ch == '*' || ch == '+' || ch == '-') {\n // Recursive call to compute the result for the left sub-expression\n vector<int> left = diffWaysToCompute(expression.substr(0, i));\n \n // Recursive call to compute the result for the right sub-expression\n vector<int> right = diffWaysToCompute(expression.substr(i + 1));\n\n // Step 4: Combine the results from left and right sub-expressions\n for (int l : left) {\n for (int r : right) {\n if (ch == '+') {\n result.push_back(l + r); // Add the results\n } else if (ch == '-') {\n result.push_back(l - r); // Subtract the results\n } else if (ch == '*') {\n result.push_back(l * r); // Multiply the results\n }\n }\n }\n }\n }\n\n // Step 5: Base case - If there were no operators, it means the expression is a single number\n if (result.empty()) {\n result.push_back(stoi(expression)); // Convert string to integer\n }\n\n // Step 6: Store the result in the map to avoid redundant computation\n map[expression] = result;\n\n // Step 7: Return the final computed result\n return result;\n }\n};",
"memory": "9200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n return dfs(expression);\n }\n\n vector<int> dfs(string exp) {\n if (memo.count(exp)) return memo[exp];\n if (exp.size() < 3) return {stoi(exp)};\n vector<int> ans;\n int n = exp.size();\n for (int i = 0; i < n; ++i) {\n char c = exp[i];\n if (c == '-' || c == '+' || c == '*') {\n vector<int> left = dfs(exp.substr(0, i));\n vector<int> right = dfs(exp.substr(i + 1, n - i - 1));\n for (int& a : left) {\n for (int& b : right) {\n if (c == '-')\n ans.push_back(a - b);\n else if (c == '+')\n ans.push_back(a + b);\n else\n ans.push_back(a * b);\n }\n }\n }\n }\n memo[exp] = ans;\n return ans;\n }\n\nprivate:\n unordered_map<string, vector<int>> memo;\n};",
"memory": "9300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n map<pair<int,int>,vector<int>>dp;\n vector<int> solve(int i,int j,string &s){\n if(j-i+1<=2){\n string temp=s.substr(i,(j-i+1));\n cout<<temp<<endl;\n return {stoi(temp)};\n }\n\n if(dp.count({i,j})>0) return dp[{i,j}];\n\n vector<int>ans;\n\n for(int k=i;k<=j;k++){\n if(!isdigit(s[k])){\n vector<int> temp=solve(i,k-1,s);\n vector<int> tmp=solve(k+1,j,s);\n\n if(s[k]=='+'){\n for(int it=0;it<temp.size();it++){\n for(int itt=0;itt<tmp.size();itt++){\n ans.push_back(temp[it]+tmp[itt]);\n }\n }\n }\n else if(s[k]=='*'){\n for(int it=0;it<temp.size();it++){\n for(int itt=0;itt<tmp.size();itt++){\n ans.push_back(temp[it]*tmp[itt]);\n }\n }\n }\n else if(s[k]=='-'){\n for(int it=0;it<temp.size();it++){\n for(int itt=0;itt<tmp.size();itt++){\n ans.push_back(temp[it]-tmp[itt]);\n }\n }\n }\n }\n }\n return dp[{i,j}]=ans;\n }\n vector<int> diffWaysToCompute(string s) {\n dp.clear();\n return solve(0,s.size()-1,s);\n }\n};",
"memory": "9300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "struct ParseResult {\n std::vector<int> values;\n std::vector<char> operators;\n};\n\nclass Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n auto [values, operators] = parse(expression);\n // for (int x : values) std::cout << x << std::endl;\n // for (int x : operators) std::cout << x << std::endl;\n std::vector<std::pair<int, char>> stack;\n std::vector<int> results;\n rec(values, operators, 0, stack, results);\n return results;\n }\nprivate:\n ParseResult parse(const std::string &expr) {\n std::string buf = \"\";\n std::vector<int> values;\n std::vector<char> operators {'+'};\n for (char c : expr) {\n if (c == '+' || c == '-' || c == '*') {\n values.push_back(std::stoi(buf));\n buf = \"\";\n operators.push_back(c);\n } else {\n buf.push_back(c);\n }\n }\n values.push_back(std::stoi(buf));\n return {values, operators};\n }\n\n void rec(const std::vector<int> &values, const std::vector<char> &operators, int i, std::vector<std::pair<int, char>> &stack, std::vector<int> &results) {\n if (i == values.size() && stack.size() == 1) {\n results.push_back(stack.back().first);\n return;\n }\n if (stack.size() > 1) {\n auto [num2, op2] = stack.back();\n stack.pop_back();\n auto [num1, op1] = stack.back();\n stack.pop_back();\n stack.push_back({calc(num1, num2, op2), op1});\n rec(values, operators, i, stack, results);\n stack.pop_back();\n stack.push_back({num1, op1});\n stack.push_back({num2, op2});\n }\n if (i < values.size()) {\n stack.push_back({values[i], operators[i]});\n rec(values, operators, i + 1, stack, results);\n stack.pop_back();\n }\n }\n\n int calc(int num1, int num2, char op) {\n switch (op) {\n case '+':\n return num1 + num2;\n case '-':\n return num1 - num2;\n case '*':\n return num1 * num2;\n default:\n return -1;\n }\n }\n};",
"memory": "9400"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> dp[20][20];\n vector<int> convert(string& expression){\n vector<int> ans;\n int sz=expression.size();\n ans.reserve(sz);\n int x=0;\n for(int i=0; i<sz; i++){\n const char c=expression[i];\n switch(c){\n case '+': ans.push_back(101); break;// 101 for +\n case '-': ans.push_back(102); break;// 102 for -\n case '*': ans.push_back(103); break;// 103 for *\n default:{\n x=10*x+c-'0';\n if (i==sz-1 || !isdigit(expression[i+1])){\n ans.push_back(x);\n x=0;\n }\n }\n }\n }\n return ans;\n }\n\n vector<int> f(int l, int r, vector<int>& nums){\n if (l>r) return {};\n if (l==r){\n if (nums[l]>=101) return {};\n return {nums[l]};\n }\n if (dp[l][r].size()>0) return dp[l][r];\n\n vector<int> ans;\n for(int i=l; i<=r; i++){\n if (nums[i]<100) continue;\n vector<int> Left=f(l, i-1, nums);\n vector<int> Right=f(i+1, r, nums);\n for(int L: Left)\n for(int R: Right){\n int x=0;\n switch(nums[i]){\n case 101: ans.push_back(L+R); break;\n case 102: ans.push_back(L-R); break;\n case 103: ans.push_back(L*R); break;\n }\n }\n }\n return dp[l][r]=ans;\n }\n\n vector<int> diffWaysToCompute(string& expression) {\n vector<int> nums=convert(expression);\n const char n=nums.size();\n return f(0, n-1, nums);\n }\n};\n",
"memory": "9500"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n //dp[index1][index2]\n vector<int> solve(int index1, int index2, string expression, vector<vector<vector<int>>>& dp) {\n //base case\n if(index1 == index2) {\n return dp[index1][index2] = {expression[index1] - '0'};\n }\n if(index1 + 1 == index2) {\n return dp[index1][index2] = {(expression[index1] - '0')*10 + (expression[index2] - '0')};\n }\n if(dp[index1][index2].size() != 0) return dp[index1][index2];\n vector<int> v;\n for(int i = index1; i <= index2; i++) {\n if(expression[i] == '*' or expression[i] == '-' or expression[i] == '+') {\n vector<int> temp = solve(index1, i - 1, expression, dp);\n vector<int> temp1 = solve(i + 1, index2, expression, dp);\n for(int j = 0; j < temp.size(); j++) {\n for(int k = 0; k < temp1.size(); k++) {\n if(expression[i] == '*') {\n v.push_back(temp[j]*temp1[k]);\n }\n else if(expression[i] == '+') {\n v.push_back(temp[j] + temp1[k]);\n }\n else {\n v.push_back(temp[j] - temp1[k]);\n }\n }\n }\n } \n }\n return dp[index1][index2] = v;\n }\n vector<int> diffWaysToCompute(string expression) {\n vector<vector<vector<int>>> dp(expression.size(), vector<vector<int>>(expression.size(), vector<int>()));\n return solve(0, expression.size() - 1, expression, dp);\n }\n};",
"memory": "9600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n //dp[index1][index2]\n vector<int> solve(int index1, int index2, string expression, vector<vector<vector<int>>>& dp) {\n //base case\n if(index1 == index2) {\n return dp[index1][index2] = {expression[index1] - '0'};\n }\n if(index1 + 1 == index2) {\n return dp[index1][index2] = {(expression[index1] - '0')*10 + (expression[index2] - '0')};\n }\n if(dp[index1][index2].size() != 0) return dp[index1][index2];\n vector<int> v;\n for(int i = index1; i <= index2; i++) {\n if(expression[i] == '*' or expression[i] == '-' or expression[i] == '+') {\n vector<int> temp = solve(index1, i - 1, expression, dp);\n vector<int> temp1 = solve(i + 1, index2, expression, dp);\n for(int j = 0; j < temp.size(); j++) {\n for(int k = 0; k < temp1.size(); k++) {\n if(expression[i] == '*') {\n v.push_back(temp[j]*temp1[k]);\n }\n else if(expression[i] == '+') {\n v.push_back(temp[j] + temp1[k]);\n }\n else {\n v.push_back(temp[j] - temp1[k]);\n }\n }\n }\n } \n }\n return dp[index1][index2] = v;\n }\n vector<int> diffWaysToCompute(string expression) {\n vector<vector<vector<int>>> dp(expression.size(), vector<vector<int>>(expression.size(), vector<int>()));\n return solve(0, expression.size() - 1, expression, dp);\n }\n};",
"memory": "9600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n map<string,vector<int>> dp;\n vector<int> func(string q){\n if(dp.find(q)!=dp.end())\n return dp[q]; \n\n vector<int> ans;\n for(int i=0;i<q.size();i++){\n if(!(q[i]=='+' || q[i]=='-' || q[i]=='*'))\n continue;\n string q1=q.substr(0,i);\n string q2=q.substr(i+1);\n vector<int> v1=func(q1);\n vector<int> v2=func(q2);\n\n for(auto &j:v1){\n for(auto &k:v2){\n if(q[i]=='+')\n ans.push_back(j+k);\n else if(q[i]=='-')\n ans.push_back(j-k);\n else\n ans.push_back(j*k);\n }\n } \n }\n if(ans.empty()){ \n int no=stoi(q);\n return dp[q]= {no};\n }\n return dp[q]= ans;\n \n }\n vector<int> diffWaysToCompute(string q) {\n dp.clear();\n return func(q); \n }\n};",
"memory": "9700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n map<string,vector<int>> dp;\n vector<int> func(string q){\n if(dp.find(q)!=dp.end())\n return dp[q]; \n\n vector<int> ans;\n for(int i=0;i<q.size();i++){\n if(!(q[i]=='+' || q[i]=='-' || q[i]=='*'))\n continue;\n string q1=q.substr(0,i);\n string q2=q.substr(i+1);\n vector<int> v1=func(q1);\n vector<int> v2=func(q2);\n\n for(auto &j:v1){\n for(auto &k:v2){\n if(q[i]=='+')\n ans.push_back(j+k);\n else if(q[i]=='-')\n ans.push_back(j-k);\n else\n ans.push_back(j*k);\n }\n } \n }\n if(ans.empty()){ \n int no=stoi(q);\n return dp[q]= {no};\n }\n return dp[q]= ans;\n \n }\n vector<int> diffWaysToCompute(string q) {\n dp.clear();\n return func(q); \n }\n};",
"memory": "9700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n\n map<pair<int, int>, vector<int>> cache; // (start, end) -> possible values\n\npublic:\n\n vector<int> diffWaysToComputePart(string expression, int start, int end) {\n \n if (this->cache.find({start, end}) != this->cache.end())\n return this->cache[{start, end}]; // calculated once before\n if (end - start + 1 <= 2) { // in the range [0, 99]\n this->cache[{start, end}] = {stoi(expression.substr(start, end - start + 1))};\n return {stoi(expression.substr(start, end - start + 1))};\n }\n\n vector<int> ans; // start recursion\n for (int i = start + 1; i < end; ++i) {\n if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*') {\n vector<int> left = diffWaysToComputePart(expression, start, i - 1);\n vector<int> right = diffWaysToComputePart(expression, i + 1, end);\n for (int l: left)\n for (int r: right)\n if (expression[i] == '+')\n ans.push_back(l + r);\n else if (expression[i] == '-')\n ans.push_back(l - r);\n else // '*'\n ans.push_back(l * r);\n }\n }\n\n this->cache[{start, end}] = ans;\n return ans;\n }\n\n vector<int> diffWaysToCompute(string expression) {\n return diffWaysToComputePart(expression, 0, expression.size() - 1);\n }\n};",
"memory": "9800"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<int> diffWaysToCompute(string expression) {\n return ways(expression, {});\n }\n\n private:\n vector<int> ways(const string& s, unordered_map<string, vector<int>>&& mem) {\n if (const auto it = mem.find(s); it != mem.cend())\n return it->second;\n\n vector<int> ans;\n\n for (int i = 0; i < s.length(); ++i)\n if (ispunct(s[i]))\n for (const int a : ways(s.substr(0, i), std::move(mem)))\n for (const int b : ways(s.substr(i + 1), std::move(mem)))\n if (s[i] == '+')\n ans.push_back(a + b);\n else if (s[i] == '-')\n ans.push_back(a - b);\n else\n ans.push_back(a * b);\n\n return mem[s] = (ans.empty() ? vector<int>{stoi(s)} : ans);\n }\n};",
"memory": "9900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> dfs(const string& expression, int i, int j, unordered_map<string, vector<int>>& memo) {\n string key = to_string(i) + \"_\" + to_string(j);\n if (memo.count(key)) {\n return memo[key];\n }\n\n bool isNumber = true;\n for (int k = i; k <= j; ++k) {\n if (!isdigit(expression[k])) {\n isNumber = false;\n break;\n }\n }\n\n if (isNumber) {\n return {stoi(expression.substr(i, j - i + 1))}; \n }\n\n vector<int> res;\n for (int k = i + 1; k < j; ++k) {\n if (expression[k] == '+' || expression[k] == '-' || expression[k] == '*') {\n vector<int> left = dfs(expression, i, k - 1, memo);\n vector<int> right = dfs(expression, k + 1, j, memo);\n for (int l : left) {\n for (int r : right) {\n if (expression[k] == '+') {\n res.push_back(l + r);\n } else if (expression[k] == '-') {\n res.push_back(l - r);\n } else {\n res.push_back(l * r);\n }\n }\n }\n }\n }\n\n memo[key] = res;\n return res;\n }\n vector<int> diffWaysToCompute(string expression) {\n unordered_map<string, vector<int>>memo;\n return dfs(expression, 0, expression.size() - 1, memo);\n }\n};",
"memory": "10000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<int> diffWaysToCompute(string expression) {\n return ways(expression, {});\n }\n\n private:\n vector<int> ways(const string& s, unordered_map<string, vector<int>>&& mem) {\n if (const auto it = mem.find(s); it != mem.cend())\n return it->second;\n\n vector<int> ans;\n\n for (int i = 0; i < s.length(); ++i)\n if (ispunct(s[i]))\n for (const int a : ways(s.substr(0, i), std::move(mem)))\n for (const int b : ways(s.substr(i + 1), std::move(mem)))\n if (s[i] == '+')\n ans.push_back(a + b);\n else if (s[i] == '-')\n ans.push_back(a - b);\n else\n ans.push_back(a * b);\n\n return mem[s] = (ans.empty() ? vector<int>{stoi(s)} : ans);\n }\n};\n\n\n \n \n",
"memory": "10100"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unordered_map<string, vector<int>> dp;\n\n vector<int> helper(string &s, int i, int j) {\n string key = to_string(i) + \",\" + to_string(j);\n if (dp.find(key) != dp.end()) return dp[key]; // Return cached result if it exists\n \n if (i > j) return {};\n \n string num = s.substr(i, j - i + 1);\n if (num.find_first_not_of(\"0123456789\") == string::npos) {\n return {stoi(num)};\n }\n\n vector<int> ans;\n for (int k = i; k <= j; k++) {\n if (s[k] == '+' || s[k] == '-' || s[k] == '*') {\n vector<int> left = helper(s, i, k - 1);\n vector<int> right = helper(s, k + 1, j);\n \n for (auto x : left) {\n for (auto y : right) {\n if (s[k] == '+') ans.push_back(x + y);\n else if (s[k] == '-') ans.push_back(x - y);\n else if (s[k] == '*') ans.push_back(x * y);\n }\n }\n }\n }\n return dp[key] = ans; // Store the result in the map\n }\n\n vector<int> diffWaysToCompute(string expression) {\n return helper(expression, 0, expression.size() - 1);\n }\n};\n",
"memory": "10200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unordered_map<string, vector<int>> dp;\n\n vector<int> helper(string &s, int i, int j) {\n string key = to_string(i) + \",\" + to_string(j);\n if (dp.find(key) != dp.end()) return dp[key]; // Return cached result if it exists\n \n if (i > j) return {};\n \n string num = s.substr(i, j - i + 1);\n if (num.find_first_not_of(\"0123456789\") == string::npos) {\n return {stoi(num)};\n }\n\n vector<int> ans;\n for (int k = i; k < j; k++) {\n if (s[k] == '+' || s[k] == '-' || s[k] == '*') {\n vector<int> left = helper(s, i, k - 1);\n vector<int> right = helper(s, k + 1, j);\n \n for (auto x : left) {\n for (auto y : right) {\n if (s[k] == '+') ans.push_back(x + y);\n else if (s[k] == '-') ans.push_back(x - y);\n else if (s[k] == '*') ans.push_back(x * y);\n }\n }\n }\n }\n return dp[key] = ans; // Store the result in the map\n }\n\n vector<int> diffWaysToCompute(string expression) {\n return helper(expression, 0, expression.size() - 1);\n }\n};\n",
"memory": "10300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> helper(vector<int>& a, vector<int>& b, char op) {\n vector<int> ans;\n for (int x : a) {\n for (int y : b) {\n if (op == '+') ans.push_back(x + y);\n else if (op == '-') ans.push_back(x - y);\n else if (op == '*') ans.push_back(x * y);\n else if (op == '/' && y != 0) ans.push_back(x / y); // Avoid division by zero\n }\n }\n return ans;\n }\n\n vector<int> solve(int i, int j, string& expression, unordered_map<string, vector<int>>& memo) {\n string key = to_string(i) + \"-\" + to_string(j);\n if (memo.count(key)) return memo[key];\n\n vector<int> ans;\n for (int k = i; k <= j; ++k) {\n if (expression[k] == '+' || expression[k] == '-' || expression[k] == '*' || expression[k] == '/') {\n vector<int> left = solve(i, k - 1, expression, memo);\n vector<int> right = solve(k + 1, j, expression, memo);\n vector<int> result = helper(left, right, expression[k]);\n ans.insert(ans.end(), result.begin(), result.end());\n }\n }\n\n if (ans.empty()) {\n ans.push_back(stoi(expression.substr(i, j - i + 1))); // Handle multi-digit numbers\n }\n\n return memo[key] = ans;\n }\n\n vector<int> diffWaysToCompute(string expression) {\n unordered_map<string, vector<int>> memo;\n return solve(0, expression.size() - 1, expression, memo);\n }\n};\n",
"memory": "10500"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n std::unordered_map<std::string, std::vector<int>> memo;\n\n void parseExpression(const std::string& expression, std::vector<int>& numbers, std::vector<char>& operators) {\n int currentNumber = 0;\n for (char c : expression) {\n if (std::isdigit(c)) {\n currentNumber = currentNumber * 10 + (c - '0');\n } else {\n numbers.push_back(currentNumber);\n currentNumber = 0;\n operators.push_back(c);\n }\n }\n numbers.push_back(currentNumber);\n }\n\n int computeOperation(int left, int right, char op) {\n switch (op) {\n case '+': return left + right;\n case '-': return left - right;\n case '*': return left * right;\n default: throw std::invalid_argument(\"Invalid operator: \" + std::string(1, op));\n }\n }\n\n std::vector<int> computeResults(const std::vector<int>& numbers, const std::vector<char>& operators, int start, int end) {\n std::string key = std::to_string(start) + \",\" + std::to_string(end);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n\n std::vector<int> results;\n if (start == end) {\n results.push_back(numbers[start]);\n } else {\n for (int i = start; i < end; ++i) {\n std::vector<int> leftResults = computeResults(numbers, operators, start, i);\n std::vector<int> rightResults = computeResults(numbers, operators, i + 1, end);\n char op = operators[i];\n\n for (int left : leftResults) {\n for (int right : rightResults) {\n results.push_back(computeOperation(left, right, op));\n }\n }\n }\n }\n\n memo[key] = results;\n return results;\n }\n\npublic:\n std::vector<int> diffWaysToCompute(std::string expression) {\n std::vector<int> numbers;\n std::vector<char> operators;\n parseExpression(expression, numbers, operators);\n \n memo.clear(); \n return computeResults(numbers, operators, 0, numbers.size() - 1);\n }\n};\n\n//KDS",
"memory": "10600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n std::unordered_map<std::string, std::vector<int>> memo;\n\n void parseExpression(const std::string& expression, std::vector<int>& numbers, std::vector<char>& operators) {\n int currentNumber = 0;\n for (char c : expression) {\n if (std::isdigit(c)) {\n currentNumber = currentNumber * 10 + (c - '0');\n } else {\n numbers.push_back(currentNumber);\n currentNumber = 0;\n operators.push_back(c);\n }\n }\n numbers.push_back(currentNumber);\n }\n\n int computeOperation(int left, int right, char op) {\n switch (op) {\n case '+': return left + right;\n case '-': return left - right;\n case '*': return left * right;\n default: throw std::invalid_argument(\"Invalid operator: \" + std::string(1, op));\n }\n }\n\n std::vector<int> computeResults(const std::vector<int>& numbers, const std::vector<char>& operators, int start, int end) {\n std::string key = std::to_string(start) + \",\" + std::to_string(end);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n\n std::vector<int> results;\n if (start == end) {\n results.push_back(numbers[start]);\n } else {\n for (int i = start; i < end; ++i) {\n std::vector<int> leftResults = computeResults(numbers, operators, start, i);\n std::vector<int> rightResults = computeResults(numbers, operators, i + 1, end);\n char op = operators[i];\n\n for (int left : leftResults) {\n for (int right : rightResults) {\n results.push_back(computeOperation(left, right, op));\n }\n }\n }\n }\n\n memo[key] = results;\n return results;\n }\n\npublic:\n std::vector<int> diffWaysToCompute(std::string expression) {\n std::vector<int> numbers;\n std::vector<char> operators;\n parseExpression(expression, numbers, operators);\n \n memo.clear(); \n return computeResults(numbers, operators, 0, numbers.size() - 1);\n }\n};\n\n//KDS",
"memory": "10600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nstring exp;\nunordered_map<int,unordered_map<int,vector<int>>> dp;\nvector<int> solve(int st,int e){\n if(dp[st].find(e)!=dp[st].end())\n return dp[st][e];\n if(st==e){\n return dp[st][e]={exp[st]-'0'};\n\n }\n if(e-st==1){\n return dp[st][e]={stoi(exp.substr(st,2))};\n }\n\n vector<int> ans;\n for(int i=st;i<=e;i++){\n if(exp[i]=='+'){\n vector<int> l=solve(st,i-1),r=solve(i+1,e);\n for(auto i:l){\n for(auto j:r){\n ans.push_back(i+j);\n }\n }\n }\n else if(exp[i]=='-'){\n vector<int> l=solve(st,i-1),r=solve(i+1,e);\n for(auto i:l){\n for(auto j:r){\n ans.push_back(i-j);\n }\n }\n }\n\n else if(exp[i]=='*'){\n vector<int> l=solve(st,i-1),r=solve(i+1,e);\n\n \n\n for(auto i:l){\n for(auto j:r){\n ans.push_back(i*j);\n }\n }\n }\n }\n\n return dp[st][e]=ans;\n}\n vector<int> diffWaysToCompute(string expression) {\n int st=0,e=expression.size()-1;\n this->exp=expression;\n return solve(0,e);\n }\n};",
"memory": "11300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n return Partition(expression, 0, expression.size());\n }\n\n std::vector<int> Partition(std::string &exp, int left, int right) {\n int first_v = exp[left] - '0'; int left_front = left;\n if (std::isdigit(exp[left+1])) { first_v = first_v * 10 + exp[left+1] - '0'; left_front++; }\n if (left_front + 1 == right) return {first_v};\n\n int last_v = exp[right-1] - '0'; int right_tail = right;\n if (std::isdigit(exp[right-2])) { last_v = last_v + (exp[right-2] - '0') * 10; right_tail--; }\n if (left_front + 3 == right_tail) return {Calc(first_v, last_v, exp[left_front+1])};\n\n std::vector<int> retv;\n for (int i = left_front + 1; i < right;) {\n std::vector<int> left_val = Partition(exp, left, i);\n std::vector<int> right_val = Partition(exp, i+1, right);\n for (auto &l_v : left_val) {\n for (auto &r_v : right_val) {\n retv.emplace_back(Calc(l_v, r_v, exp[i]));\n }\n }\n if (std::isdigit(exp[i + 2])) {\n i = i + 3;\n } else {\n i = i + 2;\n }\n }\n return retv;\n }\n\n int Calc(int i, int j, char expr) {\n switch(expr) {\n case '+':\n return i + j;\n case '-':\n return i - j;\n case '*':\n return i * j;\n }\n return 0;\n }\n};",
"memory": "11700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\n int op(int num1, int num2, char sym) const noexcept {\n switch(sym) {\n case '+':\n return num1 + num2;\n case '-':\n return num1 - num2;\n case '*':\n return num1 * num2;\n default:\n cout << num1 << \" \" << sym << \" \" << num2 << \"\\n\";\n throw invalid_argument(\"not expected\");\n }\n }\n\n template<typename T>\n using Iterator = vector<T>::iterator;\n\n vector<int> getComputations(Iterator<int> numsBegin, Iterator<int> numsEnd, Iterator<char> symsBegin) const {\n if (next(numsBegin) == numsEnd) {\n return { *numsBegin };\n }\n\n if (next(numsBegin, 2) == numsEnd) {\n return { op(*numsBegin, *(++numsBegin), *symsBegin) };\n }\n\n vector<int> results;\n auto itrNums = numsBegin;\n ++itrNums;\n auto itrSyms = symsBegin;\n for (; itrNums != numsEnd ; ++itrNums, ++itrSyms) {\n auto firstHalf = getComputations(numsBegin, itrNums, symsBegin);\n auto secondHalf = getComputations(itrNums, numsEnd, next(itrSyms));\n\n for (auto num1: firstHalf) {\n for (auto num2: secondHalf) {\n results.push_back(op(num1, num2, *itrSyms));\n }\n }\n }\n return results;\n }\n\npublic:\n vector<int> diffWaysToCompute(string expression) {\n auto* first = expression.c_str();\n auto* last = first + expression.size();\n\n vector<int> nums;\n vector<char> syms;\n int value;\n auto ptr = from_chars(first, last, value).ptr;\n while (ptr != last) {\n nums.push_back(value);\n syms.push_back(*ptr);\n first = ptr + 1;\n ptr = from_chars(first, last, value).ptr;\n }\n nums.push_back(value);\n\n return getComputations(nums.begin(), nums.end(), syms.begin());\n }\n};",
"memory": "11900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\n int op(int num1, int num2, char sym) const noexcept {\n switch(sym) {\n case '+':\n return num1 + num2;\n case '-':\n return num1 - num2;\n case '*':\n return num1 * num2;\n default:\n cout << num1 << \" \" << sym << \" \" << num2 << \"\\n\";\n throw invalid_argument(\"not expected\");\n }\n }\n\n template<typename T>\n using Iterator = vector<T>::iterator;\n\n vector<int> getComputations(Iterator<int> numsBegin, Iterator<int> numsEnd, Iterator<char> symsBegin) const {\n if (next(numsBegin) == numsEnd) {\n return { *numsBegin };\n }\n\n if (next(numsBegin, 2) == numsEnd) {\n return { op(*numsBegin, *(++numsBegin), *symsBegin) };\n }\n\n vector<int> results;\n auto itrNums = numsBegin;\n ++itrNums;\n auto itrSyms = symsBegin;\n for (; itrNums != numsEnd ; ++itrNums, ++itrSyms) {\n auto firstHalf = getComputations(numsBegin, itrNums, symsBegin);\n auto secondHalf = getComputations(itrNums, numsEnd, next(itrSyms));\n\n for (auto num1: firstHalf) {\n for (auto num2: secondHalf) {\n results.push_back(op(num1, num2, *itrSyms));\n }\n }\n }\n return results;\n }\n\n pair<vector<int>, vector<char>> parse(const string& expression) const {\n auto* first = expression.c_str();\n auto* last = first + expression.size();\n\n vector<int> nums;\n vector<char> syms;\n int value;\n auto ptr = from_chars(first, last, value).ptr;\n while (ptr != last) {\n nums.push_back(value);\n syms.push_back(*ptr);\n first = ptr + 1;\n ptr = from_chars(first, last, value).ptr;\n }\n nums.push_back(value);\n\n return { nums, syms };\n } \n\npublic:\n vector<int> diffWaysToCompute(string expression) {\n auto [nums, syms] = parse(expression);\n return getComputations(nums.begin(), nums.end(), syms.begin());\n }\n};",
"memory": "11900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> fun(vector<int>&v,int s,int e){\n if(s==e)\n return {v[s]};\n if(s+2==e){\n if(v[(s+e)/2]==-1)\n return {v[s]+v[e]};\n else if(v[(s+e)/2]==-2)\n return {v[s]-v[e]};\n else if(v[(s+e)/2]==-3)\n return {v[s]*v[e]};\n }\n vector<int>ans;\n for(int j=s;j<=e-2;j+=2){\n vector<int>a=fun(v,s,j);\n vector<int>b=fun(v,j+2,e);\n for(int k=0;k<a.size();k++){\n for(int l=0;l<b.size();l++){\n if(v[j+1]==-1)\n ans.push_back(a[k]+b[l]);\n else if(v[j+1]==-2)\n ans.push_back(a[k]-b[l]);\n else if(v[j+1]==-3)\n ans.push_back(a[k]*b[l]);\n }\n }\n }\n return ans;\n }\n vector<int> diffWaysToCompute(string s) {\n vector<int>v;\n int i=0;\n while(i<s.length()){\n if(s[i]=='+'){\n v.push_back(-1);\n i++;\n continue;\n }\n if(s[i]=='-'){\n v.push_back(-2);\n i++;\n continue;\n }\n if(s[i]=='*'){\n v.push_back(-3);\n i++;\n continue;\n }\n int a=0;\n while(s[i]>='0' && s[i]<='9'){\n a=a*10+(s[i]-'0');\n i++;\n }\n v.push_back(a);\n }\n return fun(v,0,v.size()-1);\n }\n};",
"memory": "12300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n int perform(int x, int y, char op) {\n if(op == '+') return x + y;\n if(op == '-') return x - y;\n if(op == '*') return x * y;\n return 0;\n }\n vector<int> solve(int start, int end, string &s){\n if(end==start) return {s[end]-'0'};\n if(end-start==1) return {((s[start]-'0')*10 + (s[end]-'0'))};\n\n vector<int> results; \n\n for(int i=start;i<end;i++){\n if(s[i]=='+' || s[i]=='-' || s[i]=='*'){\n vector<int> left = solve(start,i-1,s);\n vector<int> right = solve(i+1,end,s);\n\n for(auto x : left){\n for(auto y : right){\n int val = perform(x, y, s[i]);\n results.push_back(val);\n }\n }\n }\n }\n return results;\n }\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.length();\n vector<int> ans;\n return solve(0,n-1,expression);\n }\n};",
"memory": "12400"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> operands;\n vector<char> operators;\n int idx = 0;\n while (idx < expression.length()) {\n if ('0' <= expression[idx] && expression[idx] <= '9') {\n operands.push_back(read_int(expression, idx));\n } else if (expression[idx] == '+') {\n operators.push_back('+');\n idx++;\n } else if (expression[idx] == '*') {\n operators.push_back('*');\n idx++;\n } else if (expression[idx] == '-') {\n operators.push_back('-');\n idx++;\n } else {\n idx++;\n }\n }\n return diffWaysToCompute(operands, operators, 0, operands.size() - 1);\n }\n\n int read_int(string expression, int &idx) {\n int ans = 0;\n while (idx < expression.length() && expression[idx] >= '0' && expression[idx] <= '9') {\n ans *= 10;\n ans += expression[idx] - '0';\n idx++;\n }\n return ans;\n }\n\n vector<int> diffWaysToCompute(const vector<int> &operands, const vector<char> &operators, int start, int end) {\n if (start > end) return vector<int>();\n if (start == end) {\n return vector<int>(1, operands[start]);\n }\n vector<int> res;\n for (int i = start; i < end; i++) {\n vector<int> first = diffWaysToCompute(operands, operators, start, i);\n vector<int> second = diffWaysToCompute(operands, operators, i + 1, end);\n for (int f: first) {\n for (int s: second) {\n res.push_back(evaluate(f, s, operators[i]));\n }\n }\n }\n return res;\n }\n\n int evaluate(int a, int b, char op) {\n switch (op) {\n case '+':\n return a + b;\n case '*':\n return a * b;\n case '-':\n return a - b;\n default:\n return 0;\n }\n }\n};",
"memory": "12500"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n string s;\n int op(char c, int a, int b){\n if(c == '+'){\n return a+b;\n }else if(c == '-'){\n return a-b;\n }\n return a*b;\n }\n\n vector<int> rec(int l, int r){\n // if(l == r){\n // int x = s[l]-'0';\n // return {x};\n // }\n bool isNum = true;\n int number = 0;\n for(int i = l; i <= r; i++){\n if(!isdigit(s[i])){\n isNum = false;\n break;\n }\n number = number*10+(s[i]-'0');\n }\n if(isNum) return {number};\n vector<int> ans;\n for(int i = l; i <= r; i++){\n if(!isdigit(s[i])){\n vector<int> left = rec(l, i-1);\n vector<int> right = rec(i+1, r);\n for(auto x : left){\n for(auto y : right){\n ans.push_back(op(s[i], x, y));\n }\n }\n }\n }\n return ans;\n } \n\n vector<int> diffWaysToCompute(string expression) {\n this->s = expression;\n return rec(0, s.length()-1);\n }\n};",
"memory": "12600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int op(int i,int j, char opcode)\n {\n if(opcode == '+')\n return i + j;\n if(opcode == '-')\n return i - j;\n if(opcode == '*')\n return i * j;\n return -1;\n }\n vector<int> solve(string& exp, int i, int j)\n { \n // if(i == j)\n // return {exp[i]-'0'};\n int currNum = 0, f = 0;\n vector<int> v;\n for(int k=i;k<=j;k++)\n {\n if(exp[k] >= '0' && exp[k] <= '9')\n currNum = currNum*10 + exp[k]-'0';\n else\n {\n vector<int> l = solve(exp, i, k-1);\n vector<int> r = solve(exp, k+1, j);\n for(auto x : l)\n {\n for(auto y : r)\n {\n int res = op(x,y,exp[k]);\n v.push_back(res);\n }\n }\n currNum = 0;\n f=1;\n }\n }\n if(!f)\n return {currNum};\n return v;\n }\n vector<int> diffWaysToCompute(string expression) {\n return solve(expression, 0, expression.size()-1); \n }\n};",
"memory": "12700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n int n = expression.size();\n vector<vector<vector<int>>> memo(n, vector<vector<int>>(n));\n // memo[i][j] means from i-th to j-th char, list all different kind of \n // value it can come up to.\n dfsMemo(expression, 0, n-1, memo);\n return memo[0][n-1];\n }\n \n // save the result to memo is enough, do not return vector<int>\n void dfsMemo(const string& exp, int i, int j, \n vector<vector<vector<int>>>& memo) {\n // cout << i << \" \" << j << endl;\n if(i>j) return; // nothing to split.\n // if(!memo[i][j].empty()) return;\n vector<int> res;\n // case1: split the expression with parenthesis.\n for(int k = i; k<=j; k++) {\n char c = exp.at(k);\n if(c=='+' || c=='-' || c=='*') {\n dfsMemo(exp, i, k-1, memo);\n dfsMemo(exp, k+1, j, memo);\n for(int x : memo[i][k-1]) {\n for(int y : memo[k+1][j]) {\n if(c=='+') res.push_back(x+y);\n else if(c=='-') res.push_back(x-y);\n else if(c=='*') res.push_back(x*y);\n }\n }\n }\n }\n if(res.empty()) {\n // which means there is no + - *\n int num = 0;\n for(int k = i; k<=j; k++) {\n num = num*10 + (exp.at(k)-'0');\n }\n res.push_back(num);\n }\n memo[i][j] = move(res);\n }\n};",
"memory": "12800"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "#include <vector>\n#include <string>\n#include <cctype>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> solve(int i, int j, const string& s) {\n vector<int> res;\n\n // Base case: if the substring is all digits\n if (i > j) return res;\n if (i == j) {\n res.push_back(s[i] - '0');\n return res;\n }\n\n \n bool allDigits = true;\n for (int k = i; k <= j; ++k) {\n if (!isdigit(s[k])) {\n allDigits = false;\n break;\n }\n }\n\n if (allDigits) {\n res.push_back(stoi(s.substr(i, j - i + 1)));\n return res;\n }\n\n \n for (int id = i; id <= j; ++id) {\n if (s[id] == '+' || s[id] == '-' || s[id] == '*') {\n vector<int> left = solve(i, id - 1, s);\n vector<int> right = solve(id + 1, j, s);\n\n for (int l : left) {\n for (int r : right) {\n if (s[id] == '+') {\n res.push_back(l + r);\n } else if (s[id] == '-') {\n res.push_back(l - r);\n } else if (s[id] == '*') {\n res.push_back(l * r);\n }\n }\n }\n }\n }\n return res;\n }\n\n vector<int> diffWaysToCompute(string expression) {\n return solve(0, expression.size() - 1, expression);\n }\n};\n",
"memory": "12900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string e) \n {\n vector<int>results;\n if(e.size()==0)return results;\n if(e.size()==1)\n {\n results.push_back(e[0]-'0');\n return results;\n }\n if(e.size()==2 && isdigit(e[0]))\n {\n results.push_back(stoi(e));\n return results;\n }\n\n for(int i=0;i<e.size();i++)\n {\n char curr = e[i];\n if(isdigit(curr))continue;\n\n vector<int>leftres = diffWaysToCompute(e.substr(0,i));\n vector<int>rightres = diffWaysToCompute(e.substr(i+1));\n\n for(int l:leftres)\n {\n for(int r:rightres)\n {\n int comp=0;\n switch(curr)\n {\n case '+' :\n comp = l+r;\n break;\n case '-' :\n comp = l-r;\n break;\n case '*' :\n comp = l*r;\n break;\n }\n results.push_back(comp);\n }\n }\n }\n return results;\n }\n};",
"memory": "13000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int>ans;\n if(expression.size()==1) {\n ans.push_back(expression[0]-'0');\n\n return ans;\n \n \n }\n bool flag = true;\n for(int i=0;i<expression.size();i++) {\n char op= expression[i];\n\n if(op=='+' || op=='-' || op=='*') flag = false;\n }\n if(flag) {\n int res = 0;\n for(int i=0;i<expression.size();i++) {\n res = res*10 + (expression[i]-'0');\n }\n ans.push_back(res);\n\n return ans;\n\n\n\n\n \n }\n // vector<int>ans;\n \n for(int i=0;i<expression.size();i++){\n char op= expression[i];\n\n if(op=='+' || op=='-' || op=='*'){\n vector<int>leftAns = diffWaysToCompute(expression.substr(0,i));\n vector<int>rightAns = diffWaysToCompute(expression.substr(i+1));\n // cout << op << \" \" << leftAns.size() << \" \" << rightAns.size() << '\\n';\n for(int x:leftAns){\n for(int y :rightAns){\n // cout << op << \" \" << x << \" \" << y << '\\n';\n if(op=='+') {\n ans.push_back(x+y);\n }\n else if(op=='-'){\n ans.push_back(x-y);\n }\n else ans.push_back(x*y);\n }\n }\n }\n else {\n\n }\n }\n return ans;\n\n }\n};",
"memory": "13100"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "\nclass Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> result;\n bool isNumber = true;\n int num = 0;\n for (char c : expression) {\n if (isdigit(c)) {\n num = num * 10 + (c - '0');\n } else {\n isNumber = false;\n break;\n }\n }\n if (isNumber) {\n result.push_back(num);\n return result;\n }\n for (int i = 0; i < expression.size(); ++i) {\n char c = expression[i];\n if (c == '+' || c == '-' || c == '*') {\n vector<int> leftResults = diffWaysToCompute(expression.substr(0, i));\n vector<int> rightResults = diffWaysToCompute(expression.substr(i + 1));\n for (int left : leftResults) {\n for (int right : rightResults) {\n if (c == '+') result.push_back(left + right);\n else if (c == '-') result.push_back(left - right);\n else if (c == '*') result.push_back(left * right);\n } }\n }\n }\n return result;\n }\n};",
"memory": "13200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "\nclass Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> result;\n bool isNumber = true;\n int num = 0;\n for (char c : expression) {\n if (isdigit(c)) {\n num = num * 10 + (c - '0');\n } else {\n isNumber = false;\n break;\n }\n }\n if (isNumber) {\n result.push_back(num);\n return result;\n }\n for (int i = 0; i < expression.size(); ++i) {\n char c = expression[i];\n if (c == '+' || c == '-' || c == '*') {\n vector<int> leftResults = diffWaysToCompute(expression.substr(0, i));\n vector<int> rightResults = diffWaysToCompute(expression.substr(i + 1));\n for (int left : leftResults) {\n for (int right : rightResults) {\n if (c == '+') result.push_back(left + right);\n else if (c == '-') result.push_back(left - right);\n else if (c == '*') result.push_back(left * right);\n } }\n }\n }\n return result;\n }\n};",
"memory": "13200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string input) {\n vector<int> result;\n int size = input.size();\n for (int i = 0; i < size; i++) {\n char cur = input[i];\n if (cur == '+' || cur == '-' || cur == '*') {\n vector<int> result1 = diffWaysToCompute(input.substr(0, i));\n vector<int> result2 = diffWaysToCompute(input.substr(i+1));\n for (auto n1 : result1) {\n for (auto n2 : result2) {\n if (cur == '+')\n result.push_back(n1 + n2);\n else if (cur == '-')\n result.push_back(n1 - n2);\n else\n result.push_back(n1 * n2); \n }\n }\n }\n }\n if (result.empty())\n result.push_back(atoi(input.c_str()));\n return result;\n }\n};",
"memory": "13300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "\nclass Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> result;\n bool isNumber = true;\n int num = 0;\n for (char c : expression) {\n if (isdigit(c)) {\n num = num * 10 + (c - '0');\n } else {\n isNumber = false;\n break;\n }\n }\n if (isNumber) {\n result.push_back(num);\n return result;\n }\n for (int i = 0; i < expression.size(); ++i) {\n char c = expression[i];\n if (c == '+' || c == '-' || c == '*') {\n vector<int> leftResults = diffWaysToCompute(expression.substr(0, i));\n vector<int> rightResults = diffWaysToCompute(expression.substr(i + 1));\n for (int left : leftResults) {\n for (int right : rightResults) {\n if (c == '+') result.push_back(left + right);\n else if (c == '-') result.push_back(left - right);\n else if (c == '*') result.push_back(left * right);\n } }\n }\n }\n return result;\n }\n};",
"memory": "13300"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "\nclass Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> result;\n bool isNumber = true;\n int num = 0;\n for (char c : expression) {\n if (isdigit(c)) {\n num = num * 10 + (c - '0');\n } else {\n isNumber = false;\n break;\n }\n }\n if (isNumber) {\n result.push_back(num);\n return result;\n }\n for (int i = 0; i < expression.size(); ++i) {\n char c = expression[i];\n if (c == '+' || c == '-' || c == '*') {\n vector<int> leftResults = diffWaysToCompute(expression.substr(0, i));\n vector<int> rightResults = diffWaysToCompute(expression.substr(i + 1));\n for (int left : leftResults) {\n for (int right : rightResults) {\n if (c == '+') result.push_back(left + right);\n else if (c == '-') result.push_back(left - right);\n else if (c == '*') result.push_back(left * right);\n } }\n }\n }\n return result;\n }\n};",
"memory": "13400"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int perform(int x,int y,char op){\n\n if(op== '+') return x+y;\n if(op=='-') return x-y;\n if(op == '*') return x*y;\n if(op == '/') return x/y;\n return 0;\n }\n vector<int> diffWaysToCompute(string exp) {\n \n\n vector<int> results;\n bool isNumber = 1;\n for(int i=0;i<exp.size();i++){\n if(!isdigit(exp[i])){\n isNumber =0;\n vector<int>left = diffWaysToCompute(exp.substr(0,i));\n vector<int>right = diffWaysToCompute(exp.substr(i+1));\n\n for(int x: left ){\n for(int y:right){\n int val = perform(x,y,exp[i]);\n results.push_back(val);\n }\n }\n }\n }\n\n\n\n\n if(isNumber == 1) results.push_back(stoi(exp));\n return results;\n\n\n }\n};",
"memory": "13400"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n // function to get the result of the operation\n int perform(int x, int y, char op) {\n if(op == '+') return x + y;\n if(op == '-') return x - y;\n if(op == '*') return x * y;\n return 0;\n }\n \n vector<int> diffWaysToCompute(string exp) {\n \n vector<int> results;\n bool isNumber = 1;\n \n for(int i = 0; i < exp.length(); i++) {\n // check if current character is an operator\n if(!isdigit(exp[i])) {\n \n // if current character is not a digit then\n // exp is not purely a number\n isNumber = 0;\n \n // list of first operands\n vector<int> left = diffWaysToCompute(exp.substr(0, i));\n \n // list of second operands\n vector<int> right = diffWaysToCompute(exp.substr(i + 1));\n \n // performing operations\n for(int x : left) {\n for(int y : right) {\n int val = perform(x, y, exp[i]);\n results.push_back(val);\n }\n }\n \n }\n }\n \n if(isNumber == 1) results.push_back(stoi(exp));\n return results;\n }\n};\n",
"memory": "13500"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\n\npublic:\n\n vector<int> diffWaysToCompute(string expression) {\n\n vector<int> results;\n\n // Base case: if the string is empty, return an empty list\n\n if (expression.length() == 0) return results;\n\n // Base case: if the string is a single character, treat it as a number\n\n // and return it\n\n if (expression.length() == 1) {\n\n results.push_back(stoi(expression));\n\n return results;\n\n }\n\n // If the string has only two characters and the first character is a\n\n // digit, parse it as a number\n\n if (expression.length() == 2 && isdigit(expression[0])) {\n\n results.push_back(stoi(expression));\n\n return results;\n\n }\n\n // Recursive case: iterate through each character\n\n for (int i = 0; i < expression.length(); i++) {\n\n char currentChar = expression[i];\n\n // Skip if the current character is a digit\n\n if (isdigit(currentChar)) continue;\n\n // Split the expression into left and right parts\n\n vector<int> leftResults =\n\n diffWaysToCompute(expression.substr(0, i));\n\n vector<int> rightResults =\n\n diffWaysToCompute(expression.substr(i + 1));\n\n // Combine results from left and right parts\n\n for (int leftValue : leftResults) {\n\n for (int rightValue : rightResults) {\n\n int computedResult = 0;\n\n // Perform the operation based on the current character\n\n switch (currentChar) {\n\n case '+':\n\n computedResult = leftValue + rightValue;\n\n break;\n\n case '-':\n\n computedResult = leftValue - rightValue;\n\n break;\n\n case '*':\n\n computedResult = leftValue * rightValue;\n\n break;\n\n }\n\n results.push_back(computedResult);\n\n }\n\n }\n\n }\n\n return results;\n\n }\n\n};\n\n \n \n \n",
"memory": "13500"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> results;\n\n // Base case: if the string is empty, return an empty list\n if (expression.length() == 0) return results;\n\n // Base case: if the string is a single character, treat it as a number\n // and return it\n if (expression.length() == 1) {\n results.push_back(stoi(expression));\n return results;\n }\n\n // If the string has only two characters and the first character is a\n // digit, parse it as a number\n if (expression.length() == 2 && isdigit(expression[0])) {\n results.push_back(stoi(expression));\n return results;\n }\n\n // Recursive case: iterate through each character\n for (int i = 0; i < expression.length(); i++) {\n char currentChar = expression[i];\n\n // Skip if the current character is a digit\n if (isdigit(currentChar)) continue;\n\n // Split the expression into left and right parts\n vector<int> leftResults =\n diffWaysToCompute(expression.substr(0, i));\n vector<int> rightResults =\n diffWaysToCompute(expression.substr(i + 1));\n\n // Combine results from left and right parts\n for (int leftValue : leftResults) {\n for (int rightValue : rightResults) {\n int computedResult = 0;\n\n // Perform the operation based on the current character\n switch (currentChar) {\n case '+':\n computedResult = leftValue + rightValue;\n break;\n case '-':\n computedResult = leftValue - rightValue;\n break;\n case '*':\n computedResult = leftValue * rightValue;\n break;\n }\n\n results.push_back(computedResult);\n }\n }\n }\n\n return results;\n }\n};",
"memory": "13600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int perform(int x, int y, char op) {\n if(op == '+') return x + y;\n if(op == '-') return x - y;\n if(op == '*') return x * y;\n return 0;\n }\n \n vector<int> diffWaysToCompute(string exp) {\n \n vector<int> results;\n bool isNumber = 1;\n \n for(int i = 0; i < exp.length(); i++) {\n if(!isdigit(exp[i])) {\n isNumber = 0;\n vector<int> left = diffWaysToCompute(exp.substr(0, i));\n vector<int> right = diffWaysToCompute(exp.substr(i + 1));\n for(int x : left) {\n for(int y : right) {\n int val = perform(x, y, exp[i]);\n results.push_back(val);\n }\n }\n \n }\n }\n \n if(isNumber == 1) results.push_back(stoi(exp));\n return results;\n }\n};\n",
"memory": "13600"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n int perform(int x, int y, char op) {\n if(op == '+') return x + y;\n if(op == '-') return x - y;\n if(op == '*') return x * y;\n return 0;\n }\n \n vector<int>helper(string exp){\n vector<int>ans;\n if(exp.size()==1)\n {\n ans.push_back(stoi(exp));\n return ans;\n }\n bool noop=true;\n for(int i=0;i<exp.size();i++){\n if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'){\n noop=false;\n vector<int>left=helper(exp.substr(0,i));\n vector<int>right=helper(exp.substr(i+1));\n for(auto x:left){\n for(auto y:right){\n ans.push_back(perform(x,y,exp[i]));\n }\n }\n }\n }\n if(noop)\n ans.push_back(stoi(exp));\n return ans;\n }\n vector<int> diffWaysToCompute(string exp) {\n vector<int>ans=helper(exp);\n return ans;\n }\n};",
"memory": "13700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n // function to get the result of the operation\n int perform(int x, int y, char op) {\n if(op == '+') return x + y;\n if(op == '-') return x - y;\n if(op == '*') return x * y;\n return 0;\n }\n \n vector<int> diffWaysToCompute(string exp) {\n \n vector<int> results;\n bool isNumber = 1;\n \n for(int i = 0; i < exp.length(); i++) {\n // check if current character is an operator\n if(!isdigit(exp[i])) {\n \n // if current character is not a digit then\n // exp is not purely a number\n isNumber = 0;\n \n // list of first operands\n vector<int> left = diffWaysToCompute(exp.substr(0, i));\n \n // list of second operands\n vector<int> right = diffWaysToCompute(exp.substr(i + 1));\n \n // performing operations\n for(int x : left) {\n for(int y : right) {\n int val = perform(x, y, exp[i]);\n results.push_back(val);\n }\n }\n \n }\n }\n \n if(isNumber == 1) results.push_back(stoi(exp));\n return results;\n }\n};\n\n",
"memory": "13700"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // function to get the result of the operation\n int perform(int x, int y, char op) {\n if(op == '+') return x + y;\n if(op == '-') return x - y;\n if(op == '*') return x * y;\n return 0;\n }\n \n vector<int> diffWaysToCompute(string exp) {\n \n vector<int> results;\n bool isNumber = 1;\n \n for(int i = 0; i < exp.length(); i++) {\n // check if current character is an operator\n if(!isdigit(exp[i])) {\n \n // if current character is not a digit then\n // exp is not purely a number\n isNumber = 0;\n \n // list of first operands\n vector<int> left = diffWaysToCompute(exp.substr(0, i));\n \n // list of second operands\n vector<int> right = diffWaysToCompute(exp.substr(i + 1));\n \n // performing operations\n for(int x : left) {\n for(int y : right) {\n int val = perform(x, y, exp[i]);\n results.push_back(val);\n }\n }\n \n }\n }\n \n if(isNumber == 1) results.push_back(stoi(exp));\n return results;\n }\n};\n\n",
"memory": "13800"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int perform(int x,int y,char op){\n if(op=='+') return x+y ;\n if(op=='-') return x-y ;\n if(op=='*') return x*y;\n return 0;\n }\n vector<int> diffWaysToCompute(string expression) {\n vector<int>ans;\n bool isNum = true;\n for(int i=0;i<expression.length();i++){\n if(!isdigit(expression[i])){\n isNum = false;\n\n vector<int>left = diffWaysToCompute(expression.substr(0,i));\n vector<int>right = diffWaysToCompute(expression.substr(i+1));\n\n for(int x : left){\n for(int y : right){\n int val = perform(x,y,expression[i]);\n ans.push_back(val);\n }\n }\n }\n }\n if(isNum==true) ans.push_back(stoi(expression));\n return ans;\n }\n};",
"memory": "13800"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> helper(string &s, int i, int j) {\n if (i > j) return {}; \n \n string num = s.substr(i, j - i + 1);\n if (num.find_first_not_of(\"0123456789\") == string::npos) {\n return {stoi(num)}; \n }\n\n vector<int> ans;\n for (int k = i; k <= j; k++) {\n if (s[k] == '+' || s[k] == '-' || s[k] == '*') {\n vector<int> left = helper(s, i, k - 1); \n vector<int> right = helper(s, k + 1, j); \n \n for (auto x : left) {\n for (auto y : right) {\n if (s[k] == '+') ans.push_back(x + y);\n else if (s[k] == '-') ans.push_back(x - y);\n else if (s[k] == '*') ans.push_back(x * y);\n }\n }\n }\n }\n return ans;\n }\n\n vector<int> diffWaysToCompute(string expression) {\n int j = expression.size() - 1;\n return helper(expression, 0, j);\n }\n};\n",
"memory": "13900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> ans;\n for (int i = 0; i < expression.size(); i++) {\n char curr = expression[i];\n if (curr == '*' || curr == '+' || curr == '-') {\n vector<int> left, right;\n //* Left subTree\n left = diffWaysToCompute(expression.substr(0, i));\n //* Right subTree\n right = diffWaysToCompute(expression.substr(i + 1));\n for (auto j : left) {\n for (auto k : right) {\n if (curr == '*') {\n ans.push_back(j * k);\n } else if (curr == '+') {\n ans.push_back(j + k);\n } else {\n ans.push_back(j - k);\n }\n }\n }\n }\n }\n if (ans.empty()) {\n ans.push_back(stoi(expression));\n }\n return ans;\n }\n};",
"memory": "13900"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string exp) {\n bool flag = true;\n vector<int>res;\n for(int i=0;i<exp.length();i++){\n if(!isdigit(exp[i])){\n flag = false;\n vector<int>left = diffWaysToCompute(exp.substr(0,i));\n vector<int>right = diffWaysToCompute(exp.substr(i+1));\n for(int k=0;k<left.size();k++){\n for(int j=0;j<right.size();j++){\n if(exp[i]=='+'){\n res.push_back(left[k] + right[j]);\n }\n if(exp[i] == '-'){\n res.push_back(left[k] - right[j]);\n }\n if(exp[i] == '*'){\n res.push_back(left[k]*right[j]);\n }\n }\n }\n }\n }\n if(flag){\n res.push_back(stoi(exp));\n }\n return res;\n }\n};",
"memory": "14000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> result;\n\n for(int i=0;i<expression.length();i++){\n char ch = expression[i];\n\n if(ch=='*' || ch=='+' || ch=='-'){\n vector <int> left = diffWaysToCompute(expression.substr(0,i));\n\n vector <int> right = diffWaysToCompute(expression.substr(i+1));\n\n for(auto l:left){\n for(auto r:right){\n if(ch=='+'){\n result.push_back(l+r);\n }\n if(ch=='-'){\n result.push_back(l-r);\n }\n if(ch=='*'){\n result.push_back(l*r);\n }\n }\n }\n }\n }\n\n if(result.empty()){\n result.push_back(stoi(expression));\n }\n\n return result;\n }\n};",
"memory": "14000"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string exp) {\n vector<int>ans;\n int flag=0;\n for(int i=0;i<exp.size();i++){\n if(exp[i]=='-' || exp[i]=='*' || exp[i]=='+'){\n flag=1;\n vector<int>x=diffWaysToCompute(exp.substr(0,i));\n vector<int>y=diffWaysToCompute(exp.substr(i+1));\n\n for(auto valx:x){\n for(auto valy:y){\n if(exp[i]=='*'){\n ans.push_back(valx*valy);\n }\n\n else if(exp[i]=='-'){\n ans.push_back(valx-valy);\n }\n\n else{\n ans.push_back(valx+valy);\n }\n }\n }\n }\n }\n if(flag==0){\n ans.push_back(stoi(exp));\n }\n return ans;\n }\n};",
"memory": "14100"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n vector<int> diffWaysToCompute(string input) {\n vector<int> ans;\n\n bool pureNum=true;\n\n for (int i=0; i<input.length(); i++) \n \n if (input[i]<'0' || input[i]>'9') {\n pureNum=false;\n \n vector<int> L=diffWaysToCompute(input.substr(0, i));\n vector<int> R=diffWaysToCompute(input.substr(i+1, input.length()-i-1));\n \n for (auto l : L)\n for (auto r : R)\n if (input[i]=='+') {\n ans.push_back(l+r);\n }\n else if (input[i]=='-') {\n ans.push_back(l-r);\n }\n else if (input[i]=='*') {\n ans.push_back(l*r);\n }\n }\n \n if (pureNum)\n ans.push_back(stoi(input));\n return ans;\n }\n};",
"memory": "14100"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evaluate(int a, int b, char op) {\n switch (op) {\n case '+': return a + b;\n case '-': return a - b;\n case '*': return a * b;\n case '/': return a / b; // Integer division\n default: return 0;\n }\n}\n vector<int> diffWaysToCompute(string expression) {\n vector<int> results;\n \n // Traverse through each character in the expression\n for (int i = 0; i < expression.size(); ++i) {\n char c = expression[i];\n \n // If the character is an operator\n if (c == '+' || c == '-' || c == '*' || c == '/') {\n // Divide the expression into two parts\n string left = expression.substr(0, i);\n string right = expression.substr(i + 1);\n \n // Recursively compute all results for the left and right parts\n vector<int> leftResults = diffWaysToCompute(left);\n vector<int> rightResults = diffWaysToCompute(right);\n \n // Combine the results from left and right using the current operator\n for (int l : leftResults) {\n for (int r : rightResults) {\n results.push_back(evaluate(l, r, c));\n }\n }\n }\n }\n \n // If there were no operators, the expression is just a number\n if (results.empty()) {\n results.push_back(stoi(expression));\n }\n \n return results;\n}\n\n\n};",
"memory": "14200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) \n {\n vector<int>ans;\n ll sz = expression.size();\n\n for(int i=0;i<sz;i++)\n {\n if(expression[i]=='+' ||expression[i]=='-'||expression[i]=='*')\n {\n string tempL = expression.substr(0,i);\n string tempR = expression.substr(i+1);\n\n vector<int> lR = diffWaysToCompute(tempL);\n vector<int> rR = diffWaysToCompute(tempR);\n\n for(auto it:lR)\n {\n for(auto it1:rR)\n {\n if(expression[i]=='+')\n {\n ans.push_back(it+it1);\n }\n else if(expression[i]=='-')\n {\n ans.push_back(it-it1);\n }\n else if(expression[i]=='*')\n {\n ans.push_back(it*it1);\n }\n }\n }\n }\n }\n if(ans.empty())\n {\n ans.push_back(stoi(expression));\n }\n\n return ans;\n }\n};",
"memory": "14200"
} |
241 | <p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>
<p>The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed <code>10<sup>4</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> expression = "2-1-1"
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0
(2-(1-1)) = 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> expression = "2*3-4*5"
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= expression.length <= 20</code></li>
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> diffWaysToCompute(string expression) {\n vector<int> result;\n for (int i = 0; i < expression.size(); i++) {\n if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*') {\n string left = expression.substr(0, i);\n string right = expression.substr(i + 1);\n vector<int> leftResults = diffWaysToCompute(left);\n vector<int> rightResults = diffWaysToCompute(right);\n for (int l : leftResults) {\n for (int r : rightResults) {\n if (expression[i] == '+') result.push_back(l + r);\n else if (expression[i] == '-') result.push_back(l - r);\n else result.push_back(l * r);\n }\n }\n }\n }\n if (result.empty()) {\n result.push_back(stoi(expression));\n }\n return result;\n }\n};",
"memory": "14300"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.