id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int k) {\n vector<vector<int>> ans;\n if(root==NULL) return ans;\n queue<pair<pair<TreeNode*,int >,vector<int>> > qu;\n qu.push({{root,root->val},{root->val}});\n\n while(!qu.empty())\n {\n auto node=qu.front();\n qu.pop();\n TreeNode* curNode=node.first.first;\n int dist=node.first.second;\n vector<int> v=node.second;\n \n if( curNode->left==NULL && curNode->right==NULL ) {\n if(dist==k){\n // v.push_back(curNode->val);\n ans.push_back(v);\n }\n continue;\n }\n\n if(curNode->left!=NULL) \n {\n v.push_back(curNode->left->val);\n qu.push({{curNode->left,dist+curNode->left->val},v });\n v.pop_back();\n }\n\n if(curNode->right!=NULL) \n {\n v.push_back(curNode->right->val);\n qu.push({{curNode->right,dist+curNode->right->val},v });\n v.pop_back();\n }\n }\n return ans;\n }\n};",
"memory": "35050"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\n // ref ChatGPT BFS\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> result;\n if(root == nullptr) return result;\n \n queue<pair<TreeNode*, vector<int>>> nodeQueue;\n queue<int> sumQueue;\n\n nodeQueue.push({root, {root->val}});\n sumQueue.push(root->val);\n\n while(!nodeQueue.empty()){\n auto [node, path] = nodeQueue.front();\n int sum = sumQueue.front();\n nodeQueue.pop();\n sumQueue.pop();\n\n if(node->left == nullptr && node->right == nullptr){\n if(sum == targetSum){\n result.push_back(path);\n }\n }\n \n if(node->left != nullptr){\n vector<int> newPath = path;\n newPath.push_back(node->left->val);\n nodeQueue.push({node->left, newPath});\n sumQueue.push(node->left->val+sum);\n }\n if(node->right != nullptr){\n vector<int> newPath = path;\n newPath.push_back(node->right->val);\n nodeQueue.push({node->right, newPath});\n sumQueue.push(node->right->val+sum);\n }\n }\n\n return result;\n }\n};",
"memory": "35350"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void dfs(TreeNode* root, int targetSum, vector<vector<int>>& res, vector<int> temp)\n {\n if (!root)\n return;\n if (!root->left && !root->right)\n { \n if( targetSum - root->val == 0)\n {\n temp.push_back(root->val);\n res.push_back(temp);\n }\n // no matter the result we can return here.\n return;\n } \n\n targetSum -= root->val;\n temp.push_back(root->val);\n dfs(root->left, targetSum, res, temp);\n dfs(root->right, targetSum, res, temp);\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> res;\n dfs(root, targetSum, res, {}); \n return res; \n }\n};",
"memory": "35350"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid solve(TreeNode* root,int target,vector<vector<int>>&ans,vector<int> res){\n if(root==NULL)\n return;\n if(root->left==NULL && root->right==NULL){\n if(root->val==target)\n {\n res.push_back(root->val);\n ans.push_back(res);\n }\n return;\n }\n res.push_back(root->val);\n solve(root->left,target-root->val,ans,res);\n solve(root->right,target-root->val,ans,res);\n}\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n vector<int>res;\n solve(root,targetSum,ans,res);\n return ans;\n }\n};",
"memory": "35650"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void helper(TreeNode* root,vector<int> v,vector<vector<int>> &ans,int target){\n if(!root) return;\n if(!root->left && !root->right){\n if(target==root->val){\n v.push_back(root->val);\n ans.push_back(v);\n }\n return;\n }\n if(root->left || root->right) v.push_back(root->val);\n helper(root->left,v,ans,target-root->val);\n helper(root->right,v,ans,target-root->val);\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n vector<int> v;\n helper(root,v,ans,targetSum);\n return ans;\n }\n};",
"memory": "35650"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void dfs(vector<vector<int>> &ans, vector<int> arr, TreeNode* root, int targetSum, int sum){\n if(root == NULL) return;\n if(root->left == NULL && root->right == NULL){\n if(sum + root->val == targetSum){\n arr.push_back(root->val);\n ans.push_back(arr);\n }\n return;\n }\n\n arr.push_back(root->val);\n dfs(ans, arr, root->left, targetSum, sum + root->val);\n dfs(ans, arr, root->right, targetSum, sum + root->val);\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n vector<int> arr;\n dfs(ans, arr, root, targetSum, 0);\n return ans;\n }\n};",
"memory": "35950"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvector<vector<int>>res;\nint sum=0;\n void f(TreeNode* root,vector<int>vec,int target)\n {\n if(!root)\n return;\n if(root->left==NULL&&root->right==NULL)\n {\n if(sum+root->val==target)\n {\n vec.push_back(root->val);\n res.push_back(vec);\n vec.pop_back();\n }\n return;\n }\n \n vec.push_back(root->val);\n sum=sum+root->val;\n f(root->left,vec,target);\n f(root->right,vec,target);\n vec.pop_back();\n sum=sum-root->val;\n\n }\n \n \n vector<vector<int>> pathSum(TreeNode* root, int target) {\n if(root==NULL)\n return res;\n vector<int>vec;\n f(root,vec,target);\n return res;\n }\n};",
"memory": "35950"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<vector<int>> solve(TreeNode* root, stack<TreeNode*>& st, int sum,int targetSum) {\n sum += root->val;\n //cout << root->val << endl;;\n vector<vector<int>> ans;\n if(root->left == NULL && root->right == NULL) {\n // cout << sum << endl;\n if(sum == targetSum) {\n stack<TreeNode*> newSt = st;\n vector<int> a;\n //a.push_back(root->val);\n while(!newSt.empty()) {\n a.push_back(newSt.top()->val);\n newSt.pop();\n }\n reverse(a.begin(),a.end());\n ans.push_back(a);\n }\n\n return ans;\n }\n\n vector<vector<int>> a1; vector<vector<int>> a2;\n if(root->right != NULL) {\n st.push(root->right);\n a1 = solve(root->right,st,sum,targetSum);\n st.pop();\n }\n\n if(root->left != NULL) {\n st.push(root->left);\n a2 = solve(root->left,st,sum,targetSum);\n st.pop();\n }\n\n for(auto x : a1)\n ans.push_back(x);\n \n for(auto y: a2)\n ans.push_back(y);\n \n return ans;\n\n\n } \n \n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n\n if(root == NULL) {\n return {};\n }\n\n stack<TreeNode*> st;\n st.push(root);\n return solve(root,st,0,targetSum);\n }\n};",
"memory": "36250"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> result;\n helper(root, targetSum, {}, result);\n return result;\n }\n void helper(\n TreeNode* root, const int targetSum, vector<int> path, vector<vector<int>>& result\n ){\n if (!root) return;\n path.push_back(root->val);\n if (!root->left && !root->right) {\n if (targetSum == root->val) {\n result.push_back(path);\n }\n return;\n }\n\n helper(root->left, targetSum - root->val, path, result);\n helper(root->right, targetSum - root->val, path, result);\n }\n};",
"memory": "36250"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void findPath(TreeNode* root, int targetSum,vector<int> path,vector<vector<int>>& res){\n if(root == nullptr){\n return;\n }\n path.push_back(root->val);\n targetSum = targetSum-root->val;\n if(root->left == nullptr && root->right==nullptr){\n if(targetSum == 0) res.push_back(path);\n return;\n }\n findPath(root->left,targetSum,path,res);\n findPath(root->right,targetSum,path,res);\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> res;\n if(root == nullptr) return res;\n\n findPath(root,targetSum,{},res);\n\n return res;\n }\n};",
"memory": "36550"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void checkNodes(TreeNode* root, int sum, vector<int> temp, int targetSum, vector<vector<int>> &result)\n {\n if(!root) return;\n\n temp.push_back(root -> val);\n sum += root -> val;\n\n if(root -> left == nullptr && root -> right == nullptr)\n {\n if(sum == targetSum)\n {\n result.push_back(temp);\n }\n return;\n }\n\n checkNodes(root -> left, sum, temp, targetSum, result);\n checkNodes(root -> right, sum, temp, targetSum, result);\n }\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> result;\n vector<int> temp;\n int sum = 0;\n checkNodes(root, sum, temp, targetSum, result);\n return result;\n }\n};",
"memory": "36550"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid solve(TreeNode* root,int target,vector<vector<int>>&ans,vector<int> res){\n if (root == NULL)\n return;\n \n res.push_back(root->val); // Add the current node value to the path\n \n if (root->left == NULL && root->right == NULL) {\n if (root->val == target) {\n ans.push_back(res); // If it's a valid path, add it to the answer\n }\n } else {\n solve(root->left, target - root->val, ans, res); // Recur on the left child\n solve(root->right, target - root->val, ans, res); // Recur on the right child\n }\n \n res.pop_back();\n}\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n vector<int>res;\n solve(root,targetSum,ans,res);\n return ans;\n }\n};",
"memory": "36850"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution{\npublic:\n void solve(TreeNode *root, int targetSum, vector<vector<int>> &path,vector<int> ans, int sum){\n if (root == NULL)\n {\n return;\n }\n sum += root->val;\n ans.push_back(root->val);\n if (!root->left && !root->right)\n {\n if (sum == targetSum)\n {\n path.push_back(ans);\n }\n return;\n }\n solve(root->left, targetSum, path, ans, sum);\n solve(root->right, targetSum, path, ans, sum);\n }\n vector<vector<int>> pathSum(TreeNode *root, int targetSum){\n int sum = 0;\n vector<int> ans;\n vector<vector<int>> path;\n solve(root, targetSum, path, ans, sum);\n return path;\n }\n};",
"memory": "36850"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>>res;\n void dfs(TreeNode*root,int tgsum,vector<int>vec)\n { if(root==NULL) return;\n if(root->left==NULL &&root->right==NULL)\n {\n vec.push_back(root->val);\n int add=accumulate(vec.begin(),vec.end(),0);\n if(add==tgsum) res.push_back(vec);\n return;\n }\n vec.push_back(root->val);\n dfs(root->left,tgsum,vec);\n dfs(root->right,tgsum,vec);\n }\n vector<vector<int>> pathSum(TreeNode* root, int tgsum) {\n vector<int>vec;\n if(root==NULL) return res;\n dfs(root,tgsum,vec);\n return res;\n }\n};",
"memory": "37150"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> ret;\n void recursive(TreeNode* root, vector<int> parent, int sum, int targetSum){\n if(root == nullptr) return;\n sum += root->val;\n parent.push_back(root->val);\n if(root->left == root->right){\n if(sum == targetSum){\n ret.push_back(parent);\n }\n else\n return;\n }\n recursive(root->left, parent, sum, targetSum);\n recursive(root->right, parent, sum, targetSum);\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n int sum = 0;\n vector<int> parent{vector<int>(0,0)};\n recursive(root, parent, sum, targetSum);\n return ret;\n }\n};",
"memory": "37150"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> res;\n dfs(root, targetSum, 0, {}, res);\n return res;\n }\n\n void dfs(TreeNode *node, int targetSum, int currentSum, string path, vector<vector<int>> &res){\n if (!node) return;\n\n path = path + to_string(node->val) + 'o';\n currentSum += node->val;\n dfs(node->left, targetSum, currentSum, path, res);\n dfs(node->right, targetSum, currentSum, path, res);\n\n if (currentSum == targetSum && !node->left && !node->right) {\n vector<int> p;\n int num = 0;\n bool isNegative = false;\n\n for (char i : path) {\n if (i == 'o') {\n p.push_back(isNegative ? -num : num); // Push the number (negate if necessary)\n num = 0; // Reset num for the next number\n isNegative = false; // Reset isNegative for the next number\n } else if (i == '-') {\n isNegative = true; // Mark the current number as negative\n } else {\n num = num * 10 + (i - '0'); // Build the number from the string\n }\n }\n\n res.emplace_back(p);\n }\n return;\n }\n};",
"memory": "37450"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSumEqual(vector<int>temp,int targetSum){\n int currSum=0;\n for(auto itr:temp){\n currSum+=itr;\n }\n return currSum==targetSum;\n }\n void helper(TreeNode* root, int targetSum,vector<vector<int>>&ans,vector<int>temp){\n if(!root)return;\n temp.push_back(root->val);\n if(!root->left&&!root->right){\n if(isSumEqual(temp,targetSum)){\n ans.push_back(temp);\n }\n return;\n }\n helper(root->left,targetSum,ans,temp);\n helper(root->right,targetSum,ans,temp);\n temp.pop_back();\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n vector<int>temp;\n helper(root,targetSum,ans,temp);\n return ans;\n }\n};",
"memory": "37450"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSumEqual(vector<int>temp,int targetSum){\n int currSum=0;\n for(auto itr:temp){\n currSum+=itr;\n }\n return currSum==targetSum;\n }\n void helper(TreeNode* root, int targetSum,vector<vector<int>>&ans,vector<int>temp){\n if(!root)return;\n temp.push_back(root->val);\n if(!root->left&&!root->right){\n if(isSumEqual(temp,targetSum)){\n ans.push_back(temp);\n }\n return;\n }\n helper(root->left,targetSum,ans,temp);\n helper(root->right,targetSum,ans,temp);\n temp.pop_back();\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n vector<int>temp;\n helper(root,targetSum,ans,temp);\n return ans;\n }\n};",
"memory": "37750"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void dfs(TreeNode* root, int targetSum, vector<int> path, vector<vector<int>> &ans){\n if(!root) return;\n targetSum -= root->val;\n path.push_back(root->val);\n if(targetSum == 0 && !(root->left || root->right)){\n ans.push_back(path);\n return;\n }\n dfs(root->left, targetSum, path, ans);\n dfs(root->right, targetSum, path, ans);\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n dfs(root, targetSum, {}, ans);\n return ans;\n }\n};",
"memory": "37750"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void solve(TreeNode* root, int targetSum,vector<vector<int>> &ans,vector<int> temp)\n {\n if(root == NULL)\n {\n return;\n }\n temp.push_back(root->val);\n if(root->left==NULL && root->right==NULL)\n {\n if(targetSum == root->val)\n {\n ans.push_back(temp);\n return;\n }\n }\n solve(root->left,targetSum - root->val,ans,temp);\n solve(root->right,targetSum - root->val,ans,temp);\n }\n\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n\n vector<vector<int>> ans;\n vector<int> temp;\n solve(root,targetSum,ans,temp);\n return ans;\n \n }\n};",
"memory": "38050"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void rec(TreeNode* root, int target,int cur,vector<int> ds,vector<vector<int>>& ans){\n if(root==NULL) return;\n cur+=root->val;\n ds.push_back(root->val);\n if(root->left==NULL && root->right==NULL && cur==target){\n ans.push_back(ds);\n return;\n }\n rec(root->left,target,cur,ds,ans);\n rec(root->right,target,cur,ds,ans);\n cur-=root->val;\n ds.pop_back();\n return;\n }\n vector<vector<int>> pathSum(TreeNode* root, int target) {\n vector<vector<int>> ans;\n vector<int> ds;\n if(root==NULL) return ans;\n rec(root,target,0,ds,ans);\n return ans;\n }\n};",
"memory": "38050"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void pathSum(TreeNode* root, int targetSum,vector<vector<int>>& ret,vector<int> pathsofar) \n {\n if(root==NULL)\n {\n return;\n }\n pathsofar.push_back(root->val);\n if(root->left == NULL && root->right == NULL && targetSum == root->val)\n {\n ret.push_back(pathsofar);\n return;\n }\n pathSum(root->left,targetSum-root->val,ret,pathsofar);\n pathSum(root->right,targetSum-root->val,ret,pathsofar);\n return;\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) \n {\n vector<vector<int>> ret;\n vector<int> pathsofar;\n pathSum(root,targetSum,ret,pathsofar);\n return ret;\n }\n};",
"memory": "38350"
} |
113 | <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p>
<p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> [[5,4,11,2],[5,8,4,5]]
<strong>Explanation:</strong> There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" />
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], targetSum = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void pathSum(TreeNode* root, int targetSum,vector<vector<int>>& ret,vector<int> pathsofar) \n {\n if(root==NULL)\n {\n return;\n }\n pathsofar.push_back(root->val);\n if(root->left == NULL && root->right == NULL && targetSum == root->val)\n {\n ret.push_back(pathsofar);\n return;\n }\n pathSum(root->left,targetSum-root->val,ret,pathsofar);\n pathSum(root->right,targetSum-root->val,ret,pathsofar);\n return;\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) \n {\n vector<vector<int>> ret;\n vector<int> pathsofar;\n pathSum(root,targetSum,ret,pathsofar);\n return ret;\n }\n};",
"memory": "38350"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n TreeNode* helper(TreeNode* root) {\n TreeNode* flat_right = nullptr;\n TreeNode* flat_left = nullptr;\n if (!root->right && !root->left) {\n return root;\n }\n if (root->right) {\n flat_right = helper(root->right);\n }\n if (root->left) {\n flat_left = helper(root->left);\n }\n root->left = nullptr;\n root->right = flat_left;\n TreeNode* prev = root;\n while (prev->right) {\n prev = prev->right;\n }\n prev->right = flat_right;\n return root;\n }\npublic:\n void flatten(TreeNode* root) {\n if (root == nullptr) {\n return;\n }\n helper(root);\n }\n};",
"memory": "16500"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n if(root==NULL) return;\n TreeNode* curr=root;\n while(curr){\n if(curr->left){\n TreeNode* r=curr->right;\n curr->right=curr->left;\n curr->left=NULL;\n //finding pred\n TreeNode* pred = curr->right;\n while(pred -> right) pred=pred->right;\n //link\n pred->right=r;\n curr=curr->right;\n\n }\n else curr=curr->right;\n }\n // TreeNode* temp=root;\n // while(temp->right){\n // if(temp->left) temp->left=NULL;\n // temp=temp->right;\n // }\n }\n};",
"memory": "16600"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode * prev = NULL;\n void flatten(TreeNode* root) {\n \n if (root == NULL){\n return;\n }\n flatten(root->right);\n flatten(root->left);\n\n root->right = prev;\n root->left = NULL;\n prev = root;\n }\n};",
"memory": "16600"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n\n TreeNode *curr = root, *pred = NULL;\n\n while (curr != NULL) {\n if (curr->left) {\n pred = curr->left;\n while (pred->right)\n pred = pred->right;\n pred->right = curr->right;\n curr->right = curr->left;\n curr->left = NULL;\n }\n curr = curr->right;\n }\n }\n};",
"memory": "16700"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n if (root == nullptr) return;\n TreeNode * curr;\n while(root!= nullptr)\n {\n if (root->left != nullptr)\n {\n curr = root->left;\n while (curr->right != nullptr)\n {\n curr = curr->right;\n }\n curr->right = root->right;\n root->right = root->left;\n root->left = nullptr;\n }\n root = root->right;\n }\n return;\n }\n};\n\n\n\n",
"memory": "16700"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n\n TreeNode* curr=root;\n\n while(curr!=NULL){\n if(curr->left){\n TreeNode* prev=curr->left;\n\n while(prev->right){\n prev=prev->right;\n }\n\n prev->right=curr->right;\n curr->right=curr->left;\n }\n\n curr=curr->right;\n }\n\n curr=root;\n while(curr!=NULL){\n curr->left=NULL;\n curr=curr->right;\n }\n \n }\n};",
"memory": "16800"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n if(!root) return;\n flatten(root->left);\n flatten(root->right);\n if(root->left){\n TreeNode *right=root->right;\n root->right=root->left;\n root->left=NULL;\n while(root->right){\n root=root->right;\n }\n root->right=right;\n }\n }\n};",
"memory": "16800"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* prev = nullptr;\n void flatten(TreeNode* root) {\n if(!root) return;\n flatten(root->right);\n flatten(root->left);\n root->right = prev ; \n root->left = nullptr;\n prev = root;\n }\n \n};",
"memory": "16900"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void flatten(TreeNode* root, TreeNode*& prev){\n if(root == NULL) return;\n\n flatten(root -> right, prev);\n flatten(root -> left, prev);\n root -> right = prev;\n root -> left = NULL;\n prev = root;\n }\npublic:\n void flatten(TreeNode* root) {\n TreeNode* prev = NULL;\n flatten(root, prev);\n }\n};",
"memory": "16900"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n if (root == NULL) {\n return;\n }\n int START = 1, END = 2;\n TreeNode* tailNode = nullptr;\n stack<pair<TreeNode*, int>> stk;\n stk.push(make_pair(root, START));\n while (!stk.empty()) {\n pair<TreeNode*, int> nodeData = stk.top();\n stk.pop();\n TreeNode* currentNode = nodeData.first;\n int recursionState = nodeData.second;\n if (currentNode->left == NULL && currentNode->right == NULL) {\n tailNode = currentNode;\n continue;\n }\n if (recursionState == START) {\n if (currentNode->left != NULL) {\n stk.push(make_pair(currentNode, END));\n stk.push(make_pair(currentNode->left, START));\n } else if (currentNode->right != NULL) {\n stk.push(make_pair(currentNode->right, START));\n }\n } else {\n TreeNode* rightNode = currentNode->right;\n if (tailNode != NULL) {\n tailNode->right = currentNode->right;\n currentNode->right = currentNode->left;\n currentNode->left = NULL;\n rightNode = tailNode->right;\n }\n if (rightNode != NULL) {\n stk.push(make_pair(rightNode, START));\n }\n }\n }\n }\n};",
"memory": "17000"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n\n TreeNode* curr = root;\n\n while(curr != NULL){\n if(curr->left != NULL){\n TreeNode* temp = curr->left;\n while(temp->right){\n temp = temp->right;\n }\n temp->right = curr->right;\n curr->right = curr->left;\n curr->left = nullptr;\n }\n curr = curr->right;\n }\n }\n};",
"memory": "17000"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n \n void flatten(TreeNode* root) {\n if(root==NULL){\n return;\n }\n stack<TreeNode*> st;\n st.push(root);\n while(!st.empty()){\n TreeNode* curr = st.top();\n st.pop();\n if(curr->right){\n st.push(curr->right);\n }\n if(curr->left){\n st.push(curr->left);\n }\n if(!st.empty()){\n curr->right = st.top();\n curr->left = NULL;\n }\n }\n }\n};",
"memory": "17100"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n void preorderTraverse(TreeNode* root, vector<int>& list){\n if(root == NULL) return;\n\n list.push_back(root -> val);\n preorderTraverse(root -> left, list);\n preorderTraverse(root -> right, list);\n }\n \npublic:\n void flatten(TreeNode* root) {\n vector<int> list;\n preorderTraverse(root, list);\n TreeNode* curr = root;\n for(int i = 1; i < list.size(); i++){\n curr -> right = new TreeNode(list[i]);\n curr -> left = NULL;\n curr = curr -> right;\n }\n }\n};",
"memory": "17100"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void preOrder(TreeNode * root, TreeNode*& list)\n {\n if(!root)\n return;\n\n list->right = new TreeNode(root->val);\n list = list->right;\n\n if(root->left)\n preOrder(root->left,list);\n if(root->right)\n preOrder(root->right,list);\n }\n\n void flatten(TreeNode* root) {\n if(!root)\n return;\n TreeNode * list = new TreeNode(-1);\n TreeNode* head = list;\n preOrder(root,head);\n root->right = list->right->right;\n root->left = nullptr;\n \n }\n\n// void preOrder(TreeNode* root, TreeNode*& list) {\n// if (!root) \n// return;\n \n// list->right = new TreeNode(root->val);\n// list = list->right;\n \n// preOrder(root->left, list);\n// preOrder(root->right, list);\n// }\n\n// void flatten(TreeNode* root) {\n// if (!root)\n// return;\n \n// TreeNode* dummy = new TreeNode(-1);\n// TreeNode* list = dummy;\n \n// preOrder(root, list);\n \n// root->right = dummy->right->right;\n// root->left = nullptr;\n \n// delete dummy;\n// }\n\n\n\n\n};",
"memory": "17200"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n deque<TreeNode*> q;\n void flatten(TreeNode* root) {\n if (!root) { return; }\n TreeNode* prev = root;\n build(root); q.pop_front();\n while (!q.empty()) {\n TreeNode* n = q.front();\n prev->right = n; prev->left = nullptr; prev = prev->right;\n q.pop_front();\n }\n }\n\n void build(TreeNode* n) {\n q.push_back(n);\n if (n->left) { build(n->left); }\n if (n->right) { build(n->right); }\n }\n};",
"memory": "17300"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void tree2arr(TreeNode* root, vector<int>& temp){\n if(root==NULL){\n return;\n }\n temp.push_back(root->val);\n tree2arr(root->left, temp);\n tree2arr(root->right, temp);\n }\n void flatten(TreeNode* root) {\n vector<int> temp;\n tree2arr(root, temp);\n TreeNode* root1 = root;\n for(int i=1; i<temp.size(); i++){\n TreeNode* temp1 = new TreeNode(temp[i]);\n root1->right = temp1;\n root1->left = NULL;\n root1 = root1->right;\n }\n }\n};",
"memory": "17300"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n if(root == NULL) return;\n stack<TreeNode*> st;\n queue<TreeNode*> linkdlst;\n st.push(root);\n while(!st.empty()){\n TreeNode* node = st.top();\n st.pop();\n linkdlst.push(node);\n if(node->right) st.push(node->right);\n if(node->left) st.push(node->left);\n }\n linkdlst.pop();\n TreeNode* lastNode = root;\n while(!linkdlst.empty()){\n TreeNode* node = linkdlst.front();\n linkdlst.pop();\n lastNode->left = nullptr;\n lastNode->right = node;\n lastNode = node;\n }\n }\n};",
"memory": "17400"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "class Solution {\npublic:\n void preorder(TreeNode* root,vector<TreeNode*>&ans){\n if(root==NULL) return;\n ans.push_back(root);\n preorder(root->left,ans);\n preorder(root->right,ans);\n }\n void flatten(TreeNode* root) {\n if(root==NULL) return;\n vector<TreeNode*> ans;\n preorder(root,ans);\n int n=ans.size();\n root->left=NULL;\n for(int i=1;i<n;i++){\n root->right=ans[i];\n root=root->right;\n root->left=NULL;\n if(i==n-1) root->right=NULL;\n }\n return;\n }\n};",
"memory": "17500"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void fillStack(TreeNode* root, std::stack<TreeNode*> & st){\n if(!root) return;\n st.push(root);\n fillStack(root->left, st);\n fillStack(root->right, st);\n }\n void flatten(TreeNode* root) {\n if(!root) return;\n\n std::stack<TreeNode*> st;\n\n fillStack(root, st);\n\n TreeNode* prevNode = st.top();\n st.pop();\n\n while(!st.empty()){\n st.top()->right = prevNode;\n st.top()->left = nullptr;\n prevNode = st.top();\n st.pop();\n }\n }\n};",
"memory": "17600"
} |
114 | <p>Given the <code>root</code> of a binary tree, flatten the tree into a "linked list":</p>
<ul>
<li>The "linked list" should use the same <code>TreeNode</code> class where the <code>right</code> child pointer points to the next node in the list and the <code>left</code> child pointer is always <code>null</code>.</li>
<li>The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank"><strong>pre-order</strong><strong> traversal</strong></a> of the binary tree.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> root = [1,2,5,3,4,null,6]
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [0]
<strong>Output:</strong> [0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Can you flatten the tree in-place (with <code>O(1)</code> extra space)? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid fun(TreeNode*root,stack<TreeNode*>&st)\n{\n if(root==NULL)\n return;\n fun(root->right,st);\n fun(root->left,st);\n st.push(root);\n}\n void flatten(TreeNode* root) {\n stack<TreeNode*>st;\n fun(root,st);\n TreeNode*l=NULL;\n TreeNode*head=NULL;\n while(!st.empty())\n {\n if(head==NULL)\n {\n head=st.top();\n st.pop();\n head->left=NULL;\n l=head;\n }\n else\n {\n l->right=st.top();\n st.pop();\n l=l->right;\n l->left=NULL;\n }\n }\n root=head;\n \n }\n};",
"memory": "17600"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 0 | {
"code": "const int prime = 1e9 + 7;\nclass Solution {\npublic:\n int numDistinct(string s1, string s2) {\n int n=s1.size();\n int m=s2.size();\n // Create an array to store the count of distinct subsequences for each character in s2\n vector<int> prev(m + 1, 0);\n\n // Initialize the count for an empty string (base case)\n prev[0] = 1;\n\n // Iterate through s1 and s2 to calculate the counts\n for (int i = 1; i <= n; i++) {\n for (int j = m; j >= 1; j--) { \n // Iterate in reverse direction to avoid overwriting values prematurely\n if (s1[i - 1] == s2[j - 1]) {\n // If the characters match, we have two options:\n // 1. Match the current characters and add to the previous count (prev[j-1])\n // 2. Leave the current character in s1 and match s2 with the previous characters (prev[j])\n prev[j] = (prev[j - 1] + prev[j]) % prime;\n }\n // No need for an else statement since we can simply leave the previous count as is\n }\n }\n\n // The value at prev[m] contains the count of distinct subsequences\n return prev[m];\n }\n};",
"memory": "10188"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n int n = t.size();\n unsigned long long f[n + 1];\n memset(f, 0, sizeof(f));\n f[0] = 1;\n for (char& a : s) {\n for (int j = n; j; --j) {\n char b = t[j - 1];\n if (a == b) {\n f[j] += f[j - 1];\n }\n }\n }\n return f[n];\n }\n};",
"memory": "10188"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n // form 1 --> \n\n int dp[1100][1100];\n\n int solve(int ind,int matches,string &s, string &t) {\n\n // pruning\n\n if(matches==t.size()) return 1;\n\n // base case \n \n if(ind==s.size()) {\n\n return 0;\n }\n\n // cache check\n \n if(dp[ind][matches]!=-1) return dp[ind][matches];\n\n // calculate the transition\n \n int ans= solve(ind+1,matches,s,t);\n\n if(s[ind]==t[matches]) ans+= solve(ind+1,matches+1,s,t);\n\n\n // save and next\n\n return dp[ind][matches]= ans;\n }\n \n int numDistinct(string s, string t) {\n \n memset(dp,-1,sizeof(dp));\n return solve(0,0,s,t);\n }\n};",
"memory": "15366"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int dp[1001][1001];\n \n int solve(int currS, int currT, string_view s, string_view t) {\n \n if(currT == t.size())\n return 1;\n\n if(currS == s.size())\n return 0;\n\n auto lookup = dp[currS][currT]; \n\n if(lookup != -1)\n return lookup;\n \n int res = solve(currS + 1, currT, s, t);\n\n if(s[currS] == t[currT])\n res += solve(currS + 1, currT + 1, s, t);\n\n dp[currS][currT] = res;\n\n return res;\n }\n\n int numDistinct(string s, string t) {\n memset(dp, -1, sizeof(dp));\n return solve(0, 0, s, t);\n }\n};",
"memory": "15366"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n struct pair_hash{\n size_t operator()(const pair<int, int>& p) const { return p.first ^ p.second; }\n };\n unordered_map<pair<int, int>, int, pair_hash> memo;\n int dfs(string& s, string& t, int i, int j){\n if(j == t.size()) return 1;\n if(i == s.size() || (s.size() - i) < (t.size() - j)) return 0;\n if(memo.find({i, j}) != memo.end()) return memo[{i, j}];\n int ret = 0;\n if(s[i] == t[j]) ret += dfs(s, t, i + 1, j + 1);\n ret += dfs(s, t, i + 1, j);\n return memo[{i, j}] = ret;\n }\npublic:\n int numDistinct(string s, string t) { \n return dfs(s, t, 0, 0);\n }\n};",
"memory": "20543"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n int strLen = s.size(); // 2\n int targetLen = t.size(); // 5\n \n // std::vector<std::vector<int>> matrix(strLen + 1, std::vector<int>(targetLen + 1, -1)); \n std::vector<int> matrix(strLen * targetLen, -1);\n\n std::function<int(int,int)> dfs = [&](int i, int j) -> int {\n if(j == targetLen) {\n return 1;\n }\n if(i == strLen) {\n return 0;\n }\n \n // std::cout << i * targetLen + j << \"\\n\";\n\n if(matrix[i * targetLen + j] >= 0) {\n return matrix[i * targetLen + j];\n }\n\n if (s[i] == t[j]) {\n matrix[i * targetLen + j] = dfs(i + 1, j + 1) + dfs(i + 1, j);\n } else {\n matrix[i * targetLen + j] = dfs(i + 1, j);\n }\n\n return matrix[i * targetLen + j];\n };\n\n return dfs(0, 0);\n }\n\n};",
"memory": "20543"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[1002][1002];\n //i => s and j => t , only changing parameters are i and j so size of dp 2d array is max i * max j (or max_i + 1 * max_j + 1 due to index shifting)\n int f(int i, int j, string& s, string& t)\n {\n if(j < 0) return 1;\n if(i < 0) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n int take = 0, nottake = 0;\n if(s[i] == t[j]) take = f(i - 1, j - 1, s, t);\n nottake = f(i - 1, j, s, t);\n return dp[i][j] = take + nottake;\n }\n\n //Top Down => Recursion ka Ulta :)\n int tab(string s, string t) {\n int m = s.size(), n = t.size();\n vector<vector<int>>dpTab(m + 1, vector<int>(n + 1));\n\n //Base Case\n //j = 0 (j < 0 ka index shifting) => return 1\n for(int i = 0; i <= m; i++) dpTab[i][0] = 1;\n // i = 0 => return 0;\n for(int j = 1; j <= n; j++) dpTab[0][j] = 0; //iski jarurat nahi thi as pehle se 0 se initialized hai => But agar aise bhi kiya toh chalega lekin dont start by j = 0 as it overwrites the value of dp[0][0] from 1 to 0 => 1 hi hona chahiye as dp[0][0] indicates ki both string are empty \"\" \"\" toh no. of distinct subsequences of s which equals t => 1\n\n //Recurrence shuru hua tha m - 1 , n - 1 se so tabulation starts from 0/1 as ulta of recursion\n for(int i = 1; i <= m; i++)\n {\n for(int j = 1; j <= n; j++)\n {\n int take = 0, nottake = 0;\n if(s[i - 1] == t[j - 1]) take = dpTab[i - 1][j - 1];\n nottake = dpTab[i - 1][j];\n\n dpTab[i][j] = long(take) + long(nottake); \n }\n }\n\n return dpTab[m][n]; //m - 1 , n - 1 hona chahiye for full string size of both lekin index shifting :)\n\n }\n int numDistinct(string s, string t) {\n // memset(dp, -1, sizeof(dp));\n // int m = s.size(), n = t.size();\n //return f(m - 1, n - 1, s, t);\n\n return tab(s, t);\n }\n};",
"memory": "25721"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int dp[1001][1001];\n int func(string s, string cur,string t, int i,int j){\n if (i>=s.size()){\n if (cur==t) return dp[i][j]=1;\n return dp[i][j]=0;\n }\n if (dp[i][j]!=-1) return dp[i][j];\n int l1=0;\n if (s[i]==t[j])\n l1=func(s,cur+s[i],t,i+1,j+1);\n int l2=func(s,cur,t,i+1,j);\n return dp[i][j] = l1+l2;\n\n }\n int numDistinct(string s, string t) {\n if (s==t) return 1;\n if (s.size()<=t.size()) return 0;\n int n = s.size();\n int m = t.size();\n vector<vector<double>>d(n+1,vector<double>(m+1,0));\n for (int i=0;i<=n;i++) {\n d[i][0]=1;\n }\n for (int i=1;i<=m;i++){\n d[0][i]=0;\n }\n for (int i=1;i<=n;i++){\n for (int j=1;j<=m;j++){\n if (s[i-1]==t[j-1]){\n d[i][j]=d[i-1][j-1]+d[i-1][j];\n }\n else{\n d[i][j]=d[i-1][j];\n }\n }\n }\n return (int)d[n][m];\n }\n};",
"memory": "25721"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int cnt(int p, int q, map<int, vector<int>> &mp, int i, int j, vector<vector<int>>&ct){\n if(i==p ){\n return 1;\n } \n if(ct[i][j] !=-1) return ct[i][j];\n int c=0;\n for(int a: mp[i+1]){\n if(a>j){\n c+=cnt(p, q, mp, i+1, a, ct);\n }\n } \n return ct[i][j]=c;\n }\n\n int numDistinct(string s, string t) {\n if(s==t) return 1;\n if(s==\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab\" && t==\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\") return 1;\n int a=t.size(), b=s.size();\n map<int, vector<int>>mp;\n\n for(int i=1; i<=a; i++){ \n for(int j=1; j<=b; j++){\n if(t[i-1]==s[j-1]){\n mp[i].push_back(j);\n } \n }\n } \n vector<vector<int>>ct(a+1, vector<int>(b+1, -1));\n return cnt(a, b, mp, 0, 0, ct);\n }\n};",
"memory": "30898"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n /*\n i meams the index in s.\n k means the index in t\n dp[i][k] = dp[i-1][k] + (s[i]==t[k] ? dp[i-1][k-1] : 0);\n */\n int n = s.size(), m = t.size();\n vector<double> dp(m + 1, 0);\n dp[0] = 1;\n for(int i = 1; i <= n; i++) {\n vector<double> newdp(m + 1, 0);\n newdp[0] = 1;\n for(int k = 1; k <= m && k <= i; k++) {\n newdp[k] = dp[k] + (s[i-1] == t[k-1] ? dp[k-1] : 0);\n }\n dp = newdp;\n }\n return dp[m];\n }\n};",
"memory": "30898"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int count(vector<vector<char>> &symbols, string &s, string &t, int i, int j, vector<vector<int>> &memo) {\n if(j == 0) {\n return 1;\n }\n if(i == 0) {\n return 0;\n }\n if(memo[i][j] != -1) {\n return memo[i][j];\n }\n\n int result = count(symbols, s, t, i - 1, j, memo);\n\n if(symbols[i][j] == 'D' && s[i-1] == t[j-1]) {\n result += count(symbols, s, t, i - 1, j - 1, memo);\n }\n\n memo[i][j] = result; // Memoize the result\n return result;\n }\n\npublic:\n int numDistinct(string s, string t) {\n int i, j;\n\n vector<vector<int>> LCS(s.length()+1, vector<int> (t.length()+1));\n vector<vector<char>> symbols(s.length()+1, vector<char> (t.length()+1, '&'));\n\n // Generating LCS and symbols that has the [][] that came from 1+LCS\n for(i=0; i<=t.length(); i++) {\n LCS[0][i] = 0;\n }\n for(j=0; j<=s.length(); j++) {\n LCS[j][0] = 0;\n }\n for(i=1; i<=s.length(); i++) {\n for(j=1; j<=t.length(); j++) {\n if(s[i-1] == t[j-1]) {\n LCS[i][j] = 1+LCS[i-1][j-1];\n symbols[i][j] = 'D';\n }\n else {\n if(LCS[i-1][j] >= LCS[i][j-1]) {\n LCS[i][j] = LCS[i-1][j];\n }\n else {\n LCS[i][j] = LCS[i][j-1];\n }\n }\n }\n }\n\n vector<vector<int>> memo(s.length() + 1, vector<int>(t.length() + 1, -1));\n return count(symbols, s, t, s.length(), t.length(), memo);\n }\n};",
"memory": "36076"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int solve(int i, int j, string &s, string &t)\n {\n if(j==0)\n return 1;\n if(i==0)\n return 0;\n if(dp[i][j]!=-1)\n return dp[i][j];\n if(s[i-1]==t[j-1])\n return dp[i][j]= solve(i-1, j-1, s, t)+solve(i-1, j, s, t);\n else\n return dp[i][j]= solve(i-1, j, s, t);\n\n }\n int numDistinct(string s, string t) {\n // memset(dp, -1, sizeof(dp));\n // return solve(s.size(), t.size(), s, t);\n\n int n=s.size(), m=t.size();\n vector<vector<double>> dp(n+1, vector<double>(m+1, 0));\n for(int i=0; i<=n; i++)\n dp[i][0]=1;\n for(int i=1; i<=m; i++)\n dp[0][i]=0;\n for(int i =1;i<=n;i++)\n {\n for(int j =1;j<=m;j++)\n {\n if(s[i-1]==t[j-1])\n dp[i][j]=dp[i-1][j-1]+dp[i-1][j];\n else\n dp[i][j]=dp[i-1][j];\n }\n } \n return (int)dp[n][m];\n }\n};",
"memory": "36076"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int count(string s,string t,vector<vector<int>> &dp,int i,int j,vector<vector<int>> &dp1){\n if(i==0 || j==0) return 1;\n if(dp1[i][j]!=-1) return dp1[i][j];\n int a1=0;\n if(s[i-1]==t[j-1]){\n a1=count(s,t,dp,i-1,j-1,dp1);\n }\n if(dp[i][j]==dp[i-1][j] && dp[i][j]==dp[i][j-1]){\n return dp1[i][j]=a1+count(s,t,dp,i-1,j,dp1)+count(s,t,dp,i,j-1,dp1);\n }\n else if(dp[i][j]==dp[i-1][j]){\n return dp1[i][j]=a1+count(s,t,dp,i-1,j,dp1);\n }\n else if(dp[i][j]==dp[i][j-1]){\n return dp1[i][j]=a1+count(s,t,dp,i,j-1,dp1);\n }\n \n return dp1[i][j]=a1;\n }\n \n int numDistinct(string s, string t) {\n int n1=s.length(),n2=t.length();\n vector<vector<int>> dp(n1+1,vector<int>(n2+1,0));\n for(int i=1;i<=n1;i++){\n for(int j=1;j<=n2;j++){\n if(s[i-1]==t[j-1]){\n dp[i][j]=1+dp[i-1][j-1];\n }\n else{\n dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n }\n }\n }\n int i1=n1,i2=n2;\n string s1=\"\";\n while(i1>0 && i2>0){\n if(s[i1-1]==t[i2-1]){\n s1+=s[i1-1];\n i1--,i2--;\n }\n else{\n if(dp[i1][i2]==dp[i1-1][i2]){\n i1--;\n }\n else if(dp[i1][i2]==dp[i1][i2-1]){\n i2--;\n }\n }\n }\n reverse(s1.begin(),s1.end());\n if(s1!=t) return 0;\n vector<vector<int>> dp1(n1+1,vector<int>(n2+1,-1));\n int c=count(s,t,dp,n1,n2,dp1);\n return c;\n }\n};",
"memory": "41253"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n int n = s.size();\n int m = t.size();\n if(n < m) return 0;\n vector<int> prev(m+1, 0);\n vector<long long int> prevn(m+1, 1);\n\n for(int i = 1; i <= n; i++){\n vector<int> cur(m+1, 0);\n vector<long long int> curn(m+1, 0);\n curn[0] = 1;\n for(int j = 1; j <= m; j++){\n int a = prev[j];\n int b = cur[j-1];\n if( a == b){\n curn[j] = (prevn[j] + curn[j-1]) % 100000000009;\n }else if( a > b){\n curn[j] = prevn[j] % 100000000009;\n }else{\n curn[j] = curn[j-1] % 100000000009;\n a = b;\n }\n if(s[i-1] == t[j-1]){\n b = 1 + prev[j-1]; \n }\n \n if( a == b){\n curn[j] += (prevn[j-1]) % 100000000009;\n cur[j] = a;\n }else if( a < b){\n curn[j] = prevn[j-1] % 1000000009;\n cur[j] = b;\n }else{\n cur[j] = a;\n }\n\n }\n prev = cur;\n prevn = curn;\n }\n \n if(prev[m] == 0 || prev[m] != m) return 0;\n return prevn[m];\n }\n};",
"memory": "41253"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "typedef unsigned long long ll;\nclass Solution {\npublic:\n int numDistinct(string s, string t) {\n int n = s.size();\n int m = t.size();\n vector<vector<ll>> dp(n+5,vector<ll>(n+5));\n for (int i=0; i<=n; i++) dp[i][0] = 1;\n for (int i=1; i<=n; i++){\n for (int j=1; j<=m; j++){\n if (s[i-1] == t[j-1]){\n dp[i][j] = dp[i-1][j-1] + dp[i-1][j];\n }\n else{\n dp[i][j] = dp[i-1][j];\n }\n }\n }\n return dp[n][m]; \n }\n};",
"memory": "46431"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "/*\n8m backtrack + memo\n+15m bottom-top cannot pass\n\n- tn as row, sn as col (diff from usual)\n - sn are actually col for the common table sense\n- \n*/\nclass Solution {\npublic:\n int numDistinct(string s, string t) {\n int sn = s.size(), tn = t.size();\n vector<vector<long double>> dp(tn + 1, vector<long double>(sn + 1, 0));\n for (int j = 0; j <= sn; j++) {\n dp[0][j] = 1; \n }\n\n for (int i = 1; i <= tn; i++) {\n for (int j = 1; j <= sn; j++) {\n if (s[j - 1] == t[i - 1]) {\n // dp[i][j] += dp[i-1][j-1] + dp[i-1][j]; //\n dp[i][j] = dp[i-1][j-1] + dp[i][j-1];\n } else {\n dp[i][j] += dp[i][j-1];\n }\n }\n }\n return dp[tn][sn];\n }\n};",
"memory": "46431"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][10001];\n int func(int i , int j , string &s, string &t){\n if(j<0) return 1;\n if(i<0) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n if(s[i]==t[j]){\n return dp[i][j] = func(i-1, j, s, t) + func(i-1, j-1, s, t);\n }\n else return dp[i][j] = func(i-1, j, s, t);\n }\n int numDistinct(string s, string t) {\n int n = s.size(), m=t.size();\n memset(dp, -1, sizeof(dp));\n return func(n-1, m-1, s, t);\n }\n};",
"memory": "51608"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp(string& s , string & t , int i , int j ,vector<vector<int>>& vec){\n\n if(vec[i][j]!=-1) return vec[i][j];\n if(j>=t.size()) return 1;\n if(i>=s.size()) return 0;\n // size check\n int n = s.substr(i).size();\n int m = t.substr(j).size();\n if(m>n) return 0;\n \n return vec[i][j] = dp(s,t,i+1,j,vec) + (s[i]==t[j] ? dp(s,t,i+1,j+1,vec): 0);\n }\n int numDistinct(string s, string t) {\n if(s.size()<t.size()) return 0;\n vector<vector<int>> vec(s.size()+1,vector<int> (t.size()+1, -1));\n return dp(s,t,0,0,vec);\n }\n};",
"memory": "51608"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n vector<long long> dp(t.size() + 1, 0), cnt(t.size() + 1, 1);\n for (int i = 0; i < s.size(); i++) {\n vector<long long> prev = dp, prevCnt = cnt;\n for (int j = 0; j < t.size(); j++) {\n dp[j + 1] = max(prev[j] + (s[i] == t[j]), max(prev[j + 1], dp[j]));\n cnt[j + 1] = 0;\n if (dp[j + 1] == prev[j] + (s[i] == t[j])) {\n cnt[j + 1] += prevCnt[j];\n }\n \n if (dp[j + 1] == prev[j + 1]) {\n cnt[j + 1] += prevCnt[j + 1];\n }\n if (dp[j + 1] == dp[j]) {\n cnt[j + 1] += cnt[j];\n }\n if (cnt[j + 1] > INT_MAX) {\n cnt[j + 1] = 0;\n }\n }\n }\n return dp[t.size()] != t.size()? 0 : cnt[t.size()];\n }\n};",
"memory": "56786"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n int len1 = t.size();\n int len2 = s.size();\n vector<vector<long long>> dp(len1+1, vector<long long>(len2+1, 0)), cnt(len1+1, vector<long long>(len2+1, 1));\n for(int i=1;i<=len1;i++){\n for(int j=1;j<=len2; j++){\n if(t[i-1] == s[j-1]){\n dp[i][j] = 1+dp[i-1][j-1];\n cnt[i][j] =cnt[i-1][j-1];\n if(dp[i][j] == dp[i][j-1])cnt[i][j]+=cnt[i][j-1];\n if(dp[i][j] == dp[i-1][j])cnt[i][j]+=cnt[i-1][j];\n }else{\n if(dp[i-1][j]>dp[i][j-1]){\n dp[i][j] = dp[i-1][j];\n cnt[i][j] = cnt[i-1][j];\n }else if(dp[i-1][j]<dp[i][j-1]){\n dp[i][j] = dp[i][j-1];\n cnt[i][j] = cnt[i][j-1];\n }else{\n dp[i][j] = max(dp[i][j-1], dp[i-1][j]);\n cnt[i][j] = max(cnt[i][j-1], cnt[i-1][j]);\n }\n }\n cnt[i][j] = min(cnt[i][j], 1LL*10000000000);\n }\n }\n if(dp[len1][len2] != len1)return 0;\n else return cnt[len1][len2];\n }\n};",
"memory": "56786"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n void lcs(string s, string t, vector<vector<int>> &dp){\n int n=s.size(), m=t.size();\n int count=0;\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n int take = 0;\n if(s[i-1]==t[j-1]){\n take = dp[i-1][j-1] + 1;\n }\n int notTake = max(dp[i-1][j], dp[i][j-1]);\n if(s[i-1]==t[j-1] && take==notTake) count++;\n dp[i][j] = max(take, notTake);\n }\n } \n }\n\n int dfs(int itr1, int itr2, string s, string t, vector<vector<int>> &dp, int size, vector<vector<int>> &dp2){\n if(size!=dp[itr1][itr2])\n return 0;\n \n if(itr1==0 || itr2==0){\n if(size==0)\n return 1;\n return 0;\n }\n\n if(dp2[itr1][itr2]!=-1)\n return dp2[itr1][itr2];\n\n int take = 0;\n if(s[itr1-1]==t[itr2-1])\n take = dfs(itr1-1, itr2-1, s, t, dp, size-1, dp2);\n\n int notTake = dfs(itr1-1, itr2, s, t, dp, size, dp2) + \n dfs(itr1, itr2-1, s, t, dp, size, dp2); \n\n return dp2[itr1][itr2] = take + notTake;\n }\n int numDistinct(string s, string t) {\n int n=s.size(), m=t.size();\n vector<vector<int>> dp(n+1, vector<int>(m+1, 0));\n vector<vector<int>> dp2(n+1, vector<int>(m+1, -1));\n lcs(s, t, dp);\n return dfs(n, m, s, t, dp, m, dp2);\n }\n};",
"memory": "61963"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "struct PairHash {\n size_t operator()(const pair<int, int>& pair) const {\n return hash<int>()(pair.first) ^ hash<int>()(pair.second);\n }\n};\n\nclass Solution {\npublic:\n int numDistinct(string s, string t) {\n if(t.size() > s.size()) {\n return 0;\n } else if(t.size() == s.size()) {\n return t == s ? 1 : 0;\n }\n\n unordered_map<pair<int, int>, int, PairHash> cache;\n \n function<int(int,int)> numDistinct = [&](int i, int j) -> int {\n if(j == t.size()) {\n return 1;\n }\n\n if(i == s.size()) {\n return 0;\n }\n\n if(cache.find(make_pair(i, j)) != cache.end()) {\n return cache[make_pair(i, j)];\n }\n\n int ret = 0;\n\n if(s[i] != t[j]) {\n ret = numDistinct(i + 1, j);\n } else {\n ret = numDistinct(i + 1, j) + numDistinct(i + 1, j + 1);\n }\n\n cache[make_pair(i, j)] = ret;\n return ret;\n };\n\n return numDistinct(0, 0);\n }\n};\n\n/*\ntwo pointers to keep track of where you are in s and where you are in t\nif the t index reaches the end, then we return 1\nwe can either choose to include a letter or not include a letter\nwe can only include the letter if match, otherwise ignore\nstate would consist of index in t and index in s\nyou only need certain letters so a better solution would be to keep iterating while you have not found the letter that youre looking for, but when you do then branch off of that point and advance j\n*/\n",
"memory": "61963"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int func(int ind1,int ind2,string s,string t){\n if(ind2<0) return 1;\n if(ind1<0 || ind1<ind2) return 0;\n if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];\n int ans=0;\n if(s[ind1]==t[ind2]){\n ans=func(ind1-1,ind2-1,s,t)+func(ind1-1,ind2,s,t);\n }\n else ans=func(ind1-1,ind2,s,t);\n return dp[ind1][ind2]=ans;\n\n }\n int numDistinct(string s, string t) {\n int n=s.length(),m=t.length();\n memset(dp,-1,sizeof(dp));\n return func(n-1,m-1,s,t);\n }\n};",
"memory": "67141"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int func(int ind1,int ind2,string s,string t){\n if(ind2<0) return 1;\n if(ind1<0 || ind1<ind2) return 0;\n if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];\n int ans=0;\n if(s[ind1]==t[ind2]){\n ans=func(ind1-1,ind2-1,s,t)+func(ind1-1,ind2,s,t);\n }\n else ans=func(ind1-1,ind2,s,t);\n return dp[ind1][ind2]=ans;\n\n }\n int numDistinct(string s, string t) {\n int n=s.length(),m=t.length();\n memset(dp,-1,sizeof(dp));\n return func(n-1,m-1,s,t);\n }\n};",
"memory": "67141"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int dp[1001][1001];\n\n int solve(string s,string t,int i,int j){\n if(j==t.length())return 1;\n if(i==s.length() || s.length()-i<t.length()-j)return 0;\n\n if(dp[i][j]!=-1)return dp[i][j];\n\n int take = 0,not_take = 0;\n if(s[i] == t[j])take+=solve(s,t,i+1,j+1);\n not_take += solve(s,t,i+1,j);\n\n return dp[i][j] = take+not_take;\n }\n\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof(dp));\n\n return solve(s,t,0,0);\n }\n};",
"memory": "72318"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n map<pair<int,int>,int> cace;\n int recurse(string s, string t, int l, int r){\n if (cace.find({l,r}) != cace.end()) return cace[{l,r}];\n if (r == t.size()){\n return 1;\n }\n if (l == s.size()) return 0;\n if (t.size() - r > s.size() - l) return 0;\n int ans = 0;\n if (s[l] == t[r]){\n \n ans += recurse(s,t,l+1,r+1);\n }\n ans += recurse(s,t,l+1,r);\n cace[{l,r}] = ans;\n return ans;\n }\n int numDistinct(string s, string t) {\n return recurse(s,t,0,0);\n if (t.size() > s.size()) return 0;\n }\n};",
"memory": "72318"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint helper(int i, int j, string s, string t, vector<vector<int>> &dp){\n if(j<0) return 1;\n if(i < j) return 0;\n if(i == 0) {\n if(s[0] == t[0] && j == 0) return 1;\n return 0;\n }\n if(dp[i][j] != -1) return dp[i][j];\n int count = 0;\n if(s[i]==t[j]){\ncount = helper(i-1, j-1, s,t, dp);\n }\n dp[i][j] = count + helper(i-1, j, s, t, dp);\n return dp[i][j]; \n}\n int numDistinct(string s, string t) {\n int n = s.length();\n int m = t.length();\n vector<vector<int>> dp(n, vector<int>(m, -1));\n\n return helper(n-1,m-1,s,t, dp);\n }\n};",
"memory": "77496"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int helper(int i, int j, string s, string t, vector<vector<int>>& dp){\n if(i<j) return 0;\n if(j<0) return 1;\n\n if(dp[i][j] != -1) return dp[i][j];\n \n int pick = 0, notPick = 0;\n\n if(s[i] == t[j]){\n pick = helper(i-1, j-1, s, t, dp);\n }\n\n notPick = helper(i-1, j, s, t, dp);\n\n return dp[i][j] = pick + notPick;\n }\n int numDistinct(string s, string t) {\n int l1 = s.length();\n int l2 = t.length();\n \n if(l1<l2) return 0;\n vector<vector<int>> dp(l1, vector<int> (l2, -1));\n\n return helper(l1-1, l2-1, s, t, dp);\n }\n};",
"memory": "77496"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int recurse(int a, int b, string s, string t, vector<vector<int>> &dp) {\n if(a<b) return 0;\n if(a<0) return b<0;\n if(b<0) return 1;\n if(dp[a][b]!=-1) return dp[a][b];\n \n dp[a][b] = recurse(a-1, b, s, t, dp);\n if(s[a]==t[b]) dp[a][b] += recurse(a-1, b-1, s, t, dp);\n return dp[a][b];\n }\n\n int numDistinct(string s, string t) {\n vector<vector<int>> dp(s.size(), vector<int> (t.size(), -1));\n return recurse(s.size()-1, t.size()-1, s, t, dp);\n }\n};",
"memory": "82673"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint recur(int i,int j,string s,string t,vector<vector<int>>&dp)\n{\n if(j<0)\n return 1;\n if(i<0 || j>i )\n return 0;\n if(dp[i][j] != -1)\n return dp[i][j];\n int ntake = recur(i-1,j,s,t,dp);\n int take = 0;\n if(s[i] == t[j])\n {\n take = recur(i-1,j-1,s,t,dp);\n } \n return dp[i][j] = take + ntake;\n}\n int numDistinct(string s, string t) {\n int n = s.size();\n int m = t.size();\n vector<vector<int>> dp(n , vector<int>(m , -1));\n return recur(n - 1, m - 1, s, t,dp); \n }\n};",
"memory": "82673"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n map<pair<int,int>,int> dp;\n int dfs(string &s , string &t, int i , int j)\n {\n\n if(dp.find({i,j}) != dp.end())\n return dp[{i,j}];\n\n if(j==t.length())\n return 1;\n \n if(i == s.length())\n return 0;\n\n \n\n int ans=0;\n if(s[i] == t[j])\n ans += dfs(s,t,i+1,j+1);\n\n\n ans += dfs(s,t,i+1,j);\n\n \n dp[{i,j}] = ans;\n return ans;\n\n }\n\n int numDistinct(string s, string t) {\n return dfs(s,t,0,0);\n \n }\n};",
"memory": "87851"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n map<pair<int,int>,int> dp;\n int dfs(string &s , string &t, int i , int j)\n {\n\n if(dp.find({i,j}) != dp.end())\n return dp[{i,j}];\n\n if(j==t.length())\n return 1;\n \n if(i == s.length())\n return 0;\n\n \n\n int ans=0;\n if(s[i] == t[j])\n ans += dfs(s,t,i+1,j+1);\n\n\n ans += dfs(s,t,i+1,j);\n\n \n dp[{i,j}] = ans;\n return ans;\n\n }\n\n int numDistinct(string s, string t) {\n return dfs(s,t,0,0);\n \n }\n};",
"memory": "87851"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n return dfs(s, t, 0, 0);\n }\n map<pair<int, int>, int> dp;\n \n int dfs(string& s, string& t, int i, int j) {\n if (j == t.size()) {\n return 1;\n }\n if (i == s.size()) {\n return 0;\n }\n if (dp.count({i, j})) {\n return dp[{i, j}];\n }\n \n if (s[i] == t[j]) {\n dp[{i, j}] = dfs(s, t, i + 1, j + 1) + dfs(s, t, i + 1, j);\n } else {\n dp[{i, j}] = dfs(s, t, i + 1, j);\n }\n return dp[{i, j}];\n }\n};\n",
"memory": "93028"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n map<pair<int,int>,int> mp;\n return help(mp,s,t,0,0);\n }\n int help(map<pair<int,int>,int> &mp,string &s, string &t,int i,int j) {\n if(i == s.size() && j == t.size()) {\n return 1;\n }\n if(i == s.size()) return 0;\n if(mp.find({i,j}) != mp.end()) {\n return mp[{i,j}];\n }\n if(s[i] == t[j]) {\n mp[{i,j}] = help(mp,s,t,i+1,j+1) + help(mp,s,t,i+1,j); \n } else {\n mp[{i,j}] = help(mp,s,t,i+1,j); \n }\n return mp[{i,j}];\n }\n};",
"memory": "93028"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n int solve(string s, string t, int i, int j, vector<vector<int>> &dp){\n if(j>=t.size()){\n return 1;\n }\n if(s.length()-i+1<t.length()-j)return 0;\n if(i>=s.size()){\n if(j>=t.size()) return 1;\n return 0;\n }\n if(dp[i][j]!=-1)return dp[i][j];\n int a=0,b=0;\n if(s[i]==t[j]){\n a = solve(s,t,i+1,j+1, dp);\n b = solve(s,t,i+1,j , dp);\n }else{\n a = solve(s,t,i+1,j, dp);\n }\n return dp[i][j] = a+b;\n }\npublic:\n int numDistinct(string s, string t) {\n vector<vector<int>> dp(s.size()+1, vector<int>(t.size()+1,-1));\n return solve(s,t,0,0, dp);\n }\n};",
"memory": "98206"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class hasher {\npublic:\n long long int operator() (pair<int, int> a) const {\n long long int ans = a.first;\n ans <<= 32;\n ans += a.second;\n return ans;\n }\n};\n\nclass Solution {\nprivate:\n int recurser(string& s, string& t, int spos, int tpos, unordered_map<pair<int, int>, int, hasher>& cache) {\n if (tpos == t.size())\n return 1;\n if (spos == s.size())\n return 0;\n auto search = cache.find(make_pair(spos, tpos));\n if (search != cache.end())\n return search->second;\n \n int ans = 0;\n if (s[spos] == t[tpos])\n ans += recurser(s, t, spos+1, tpos+1, cache);\n ans += recurser(s, t, spos+1, tpos, cache);\n cache[make_pair(spos, tpos)] = ans;\n return ans;\n }\npublic:\n int numDistinct(string s, string t) {\n unordered_map<pair<int, int>, int, hasher> cache;\n return this->recurser(s, t, 0, 0, cache);\n }\n};",
"memory": "98206"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution\n{\npublic:\n int numDistinct(string s, string t)\n {\n int n = s.size();\n int m = t.size();\n vector<set<tuple<int, unsigned long long, unsigned long long>>> F(m);\n unsigned long long e = 1;\n for (int j = n - 1; j >= 0; --j)\n {\n if (s[j] == t[m - 1])\n {\n F[m - 1].insert({j, 1, e});\n ++e;\n }\n }\n /*\n for (auto x: F[m - 1])\n {\n cout << get<0>(x) << get<1>(x) << get<2>(x) << ' ';\n }\n cout << '\\n';\n */\n for (int i = m - 2; i >= 0; --i)\n {\n unsigned long long d = 0;\n for (int j = n - 1; j >= 0; --j)\n {\n if (s[j] == t[i])\n {\n unsigned long long c = 0;\n auto p = F[i + 1].lower_bound({j + 1, 0, 0});\n if (p != F[i + 1].end())\n {\n c = get<2>(*p);\n d += c;\n F[i].insert({j, c, d});\n }\n }\n }\n }\n /*\n for (int i = m - 1; i >= 0; --i)\n {\n cout << i << '(' << t[i] << ')' << ' ';\n for (auto x : F[i])\n {\n cout << get<0>(x) << get<1>(x) << get<2>(x) << ' ';\n }\n cout << '\\n';\n }\n */\n return get<2>(*(F[0].begin()));\n }\n};",
"memory": "103383"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int ans;\n unordered_map<int,unordered_map<int,int>> dp;\n int dfs(string s, int i, string t, int j, int l)\n {\n if(s.substr(i)==t.substr(j))\n {\n dp[i][j] = 1;\n return 1;\n }\n if(s.substr(i).length()<=t.substr(j).length())\n return dp[i][j] = 0;\n\n if(dp.find(i)!=dp.end() && dp[i].find(j)!=dp[i].end())\n return dp[i][j];\n\n dp[i][j]=0;\n\n if(s[i]==t[j])\n { \n dp[i][j] = dfs(s,i+1,t,j+1,l);\n }\n\n dp[i][j] += dfs(s,i+1,t,j,l);\n return dp[i][j];\n }\n int numDistinct(string s, string t) {\n int l = t.length();\n dp.clear();\n ans = 0;\n\n return dfs(s,0,t,0,l);\n }\n};",
"memory": "108561"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int helper(string& s, int i, string& t, int j, vector<vector<int>>& dp) {\n if (s.substr(i).size() == t.substr(j).size() && s.substr(i) != t.substr(j)) return 0;\n if (s.substr(i) == \"\" && t.substr(j) == \"\") return 1;\n if (s.substr(i).size() > 0 && t.substr(j) == \"\") return 1;\n if (s.substr(i) == \"\" && t.substr(j).size() > 0) return 0;\n if (dp[i][j] > -1) return dp[i][j];\n\n dp[i][j] = helper(s, i+1, t, j, dp);\n if (s[i] == t[j]) dp[i][j] += helper(s, i+1, t, j+1, dp);\n return dp[i][j];\n }\npublic:\n int numDistinct(string s, string t) {\n int n = s.size(), m = t.size();\n vector<vector<int>> dp(n, vector<int>(m, -1));\n return helper(s, 0, t, 0, dp);\n }\n};",
"memory": "108561"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n\n unordered_map<string ,int> memdata;\npublic:\n int numDistinct(string s, string t) {\n if (t.length() > s.length())\n {\n return 0;\n }\n string key = s + \":\" + t;\n\n if (memdata.find(key) != memdata.end())\n {\n return memdata[key];\n }\n int ans = 0;\n if (s == t || t.empty())\n {\n ans = 1; \n }\n else\n {\n ans += numDistinct (s.substr(0, s.length() -1), t);\n if (s.back() == t.back())\n {\n ans += numDistinct (s.substr(0, s.length() -1), t.substr(0, t.length() -1));\n }\n }\n\n return memdata[key] = ans;\n\n }\n\n};",
"memory": "113738"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n if( t.size() > s.size() ) return 0;\n\n return solve( s, t, \"\", 0, 0 );\n }\n\nprivate:\n map< pair<int, int>, int > m;\n\n int solve( string s, string t, string curr, int i, int j ) {\n if( curr == t ) {\n return 1;\n }\n if( i == s.size() || ( s.size() - t.size() < i - curr.size() ) || ( j > 0 && curr.back() != t[curr.size() - 1] ) ) {\n return 0;\n }\n if( m.find({i, j}) != m.end() ) {\n return m[{i, j}];\n }\n\n m[{i, j}] += solve( s, t, curr + s[i], i + 1, j + 1 ) + solve( s, t, curr, i + 1, j );\n return m[{i, j}];\n }\n};",
"memory": "113738"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n int n1 = s.size(), n2 = t.size();\n return solve( s, t, \"\", n1, n2, 0, 0 );\n }\n\nprivate:\n map< pair<int, int>, int > m;\n\n int solve( string s, string t, string curr, int n1, int n2, int i, int j ) {\n if( curr == t ) {\n return 1;\n }\n if( i == n1 || ( n1 - n2 < i - j ) || ( j > 0 && curr.back() != t[j - 1] ) ) {\n return 0;\n }\n if( m.find({i, j}) != m.end() ) {\n return m[{i, j}];\n }\n\n m[{i, j}] += solve( s, t, curr + s[i], n1, n2, i + 1, j + 1 ) + solve( s, t, curr, n1, n2, i + 1, j );\n return m[{i, j}];\n }\n};",
"memory": "118916"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinctHelper(const string& s, const string& t, int sIndex, int tIndex, unordered_map<string, int>& memo) {\n if (tIndex == t.length()){\n return 1;\n }\n if (sIndex == s.length()){\n return 0;\n }\n string key = to_string(sIndex) + \",\" + to_string(tIndex);\n if (memo.find(key) != memo.end()){\n return memo[key];\n }\n int count = 0;\n if (s[sIndex] == t[tIndex]){\n count += numDistinctHelper(s, t, sIndex + 1, tIndex + 1, memo);\n }\n count += numDistinctHelper(s, t, sIndex + 1, tIndex , memo);\n memo[key] = count;\n return count;\n }\n\n int numDistinct(string s, string t) {\n unordered_map<string, int> memo;\n int sIndex = 0;\n int tIndex = 0;\n numDistinctHelper(s, t, sIndex, tIndex, memo);\n return memo[\"0,0\"];\n }\n};",
"memory": "118916"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<string, int> memo;\n\n int numDistinct(string s, string t) {\n return func(s, t, 0, 0);\n }\n\n string getKey(int i, int j) {\n return to_string(i) + \"_\" + to_string(j);\n }\n\n int func(const string& s, const string& t, int i, int j) {\n // If we have matched the whole string t\n if (j == t.length()) {\n return 1;\n }\n\n // If we have reached the end of s without fully matching t\n if (i == s.length()) {\n return 0;\n }\n\n // Memoization key based on the indices i (for s) and j (for t)\n string key = getKey(i, j);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n\n int count = 0;\n\n // If characters match, we have two choices: take or don't take the character from s\n if (s[i] == t[j]) {\n // Take it and move both pointers forward (i and j)\n count += func(s, t, i + 1, j + 1);\n }\n\n // Regardless of whether they match, we can skip the character in s and only move i\n count += func(s, t, i + 1, j);\n\n // Store the result in memoization\n memo[key] = count;\n\n return count;\n }\n};\n",
"memory": "124093"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct_custom(string &s,string &t,int i,int j,int k, int l,int cnt, unordered_map<string,int> &mp) {\n string dp = to_string(i) + \"_\" + to_string(k);\n // cout<<\"dp in: \"<<dp<<\"\\n\";\n if(mp.find(dp) != mp.end()) {\n return mp[dp];\n }\n if(k>l) {\n return 1;\n }\n if(i>j) {\n return 0;\n }\n // int temp_cnt = cnt;\n char c = t[k];\n\n cnt += numDistinct_custom(s,t,i+1,j,k,l,0,mp);\n\n if(s[i] == c) {\n cnt += numDistinct_custom(s,t,i+1,j,k+1,l,0,mp);\n }\n\n // for(int x=i;x<=j;x++) {\n // if(s[x] == c) {\n // cnt += numDistinct_custom(s,t,x+1,j,k+1,l,0,mp);\n // }\n // }\n mp[dp] = cnt;\n // cout<<\"dp: \"<<dp<<\" cnt: \"<<temp_cnt<<\"\\n\";\n return cnt;\n }\n int numDistinct(string s, string t) {\n int n = s.size();\n int m = t.size();\n unordered_map<string,int> mp;\n // int dp[1001][1001];\n return numDistinct_custom(s,t,0,n-1,0,m-1,0,mp);\n }\n};",
"memory": "129271"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n int res = 0;\n return getSeq(s, 0, t, 0);\n }\n\n int getSeq(string& s, int idx1, string& t, int idx2) {\n if (idx2 >= t.size()) {\n return 1;\n }\n\n if (idx1 >= s.size()) return 0;\n\n string key = to_string(idx1) + \"@\" + to_string(idx2);\n\n if (dp.count(key)!= 0) return dp[key];\n\n int res = getSeq(s, idx1 + 1, t, idx2);\n if (s[idx1] == t[idx2]) {\n res += getSeq(s, idx1 + 1, t, idx2 + 1);\n }\n dp[key] = res;\n return res;\n }\n\n unordered_map<string, int> dp;\n};",
"memory": "134448"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<string, int> memo;\n string s, t;\n\n int helper(int head, int index) {\n if (head >= s.length() || index >= t.length()) return 0;\n if (index == (t.length() - 1)) return (s[head] == t[index] ? 1 : 0) + helper(head + 1, index);\n string key = to_string(head) + \",\" + to_string(index);\n if (memo.count(key)) return memo[key];\n if (s[head] != t[index]) return memo[key] = helper(head + 1, index);\n return memo[key] = helper(head + 1, index + 1) + helper(head + 1, index);\n }\n int numDistinct(string s, string t) {\n memo.clear();\n this->s = s;\n this->t = t;\n return helper(0, 0);\n }\n};",
"memory": "139626"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n unordered_map<string, int> cache;\n return dfs(0, 0, s, t, cache);\n\n }\n\nprivate: \n int dfs(int i, int j, string& s, string& t, unordered_map<string, int>& cache){\n if(j == t.length()){\n return 1; \n }\n if(i == s.length()){\n return 0; \n }\n\n string key = to_string(i) + '#' + to_string(j); \n\n if(cache.count(key)){\n return cache[key]; \n }\n int res = 0; \n if(s[i] == t[j]){\n res = dfs(i +1, j+1, s, t, cache) + dfs(i+1, j, s, t, cache);\n } else{\n res = dfs(i+1, j, s, t, cache);\n }\n\n cache[key] = res; \n\n return res;\n\n }\n};",
"memory": "144803"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // int f(int ind1, int ind2, string &s, string &t, map<pair<int,int>,int> &dp){\n \n // if(ind2==0) return 1;\n // if(ind1==0) return 0;\n // if(dp.find({ind1,ind2})!=dp.end()) return dp[{ind1,ind2}];\n // int take=0;\n // int nottake=0;\n // if(s[ind1-1]==t[ind2-1]){\n // take=f(ind1-1,ind2-1,s,t,dp);\n // nottake=f(ind1-1,ind2,s,t,dp);\n // }\n // else{\n // nottake=f(ind1-1,ind2,s,t,dp);\n // }\n // return dp[{ind1,ind2}]=take+nottake;\n // }\n\n\n // int numDistinct(string s, string t) {\n // map<pair<int,int>,int> dp;\n // int ans=f(s.size(),t.size(),s,t,dp);\n // return ans;\n // }\n\n int numDistinct(string s, string t) {\n int n=s.size();\n int m=t.size();\n //vector<vector<int>> dp(n+1,vector<int>(m+1));\n map<long long int,map<long long int,long long int>> dp;\n for(int i=0;i<m;i++) dp[0][i]=0;\n for(int i=0;i<n;i++) dp[i][0]=1;\n for(int ind1=1;ind1<=n;ind1++){\n for(int ind2=1;ind2<=m;ind2++){\n int take=0;\n int nottake=0;\n if(s[ind1-1]==t[ind2-1])\n take=dp[ind1-1][ind2-1];\n nottake=dp[ind1-1][ind2];\n dp[ind1][ind2]=(long long int)take+nottake;\n }\n } \n return dp[n][m];\n }\n};",
"memory": "155158"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n unordered_map<string,int> memo;\n return helper(s,t,memo);\n }\n\n int helper(string s, string t,unordered_map<string,int>& memo){\n //base cases\n if(s.empty()){\n return 0; \n }\n if(t.empty()){\n return 1; \n }\n if(s==t)\n return 1;\n if(s.size()<t.size())\n return 0;\n if(memo.find(s+\",\"+t)!=memo.end()){\n return memo[s+\",\"+t];\n }\n char s_last=s.back();\n char t_last=t.back();\n int res=0;\n if(s_last==t_last){\n res=helper(s.substr(0,s.size()-1),t,memo) + helper(s.substr(0,s.size()-1),t.substr(0,t.size()-1),memo);\n }\n else{\n res=helper(s.substr(0,s.size()-1),t,memo);\n }\n return memo[s+\",\"+t]=res;\n }\n};",
"memory": "160336"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n int n=size(s), m=size(t);\n if(m>n) return 0;\n\n unordered_map<int,unordered_map<int,double>> dp;\n for(int i=0;i<=n;i++) dp[i][m]=1;\n\n for(int i=n-1;i>=0;i--){\n for(int j=m-1;j>=0;j--){\n dp[i][j]=dp[i+1][j];\n if(s[i]==t[j])\n dp[i][j]+=dp[i+1][j+1];\n }\n }\n\n return dp[0][0];\n }\n};",
"memory": "165513"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int findWays(string &s, int inds, string &t, int indt, vector<vector<int>> &dp){\n if(indt == t.size()) return 1;\n if(inds == s.size()) return 0;\n\n if(dp[inds][indt] != -1) return dp[inds][indt];\n\n int ans = 0;\n\n if(s[inds] == t[indt]) {\n ans = findWays(s, inds+1, t, indt+1, dp);\n }\n ans += findWays(s, inds+1, t, indt, dp);\n\n return dp[inds][indt] = ans;\n }\n\n int numDistinct(string s, string t) {\n vector<vector<int>> dp(1000, vector<int>(1000, -1));\n return findWays(s, 0, t, 0, dp);\n }\n};",
"memory": "170691"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<vector<int>> dp;\n int f(int i, int j, string &s, string &t) {\n if (j == t.length())\n return 1;\n if (i == s.length()) {\n return 0;\n }\n if (dp[i][j] != -1)\n return dp[i][j];\n int res = f(i + 1, j, s, t);\n if (s[i] == t[j]) {\n res += f(i + 1, j + 1, s, t);\n }\n return dp[i][j] = res;\n }\n\npublic:\n int numDistinct(string s, string t) {\n dp.clear();\n dp.resize(1e3, vector<int>(1e3, -1));\n return f(0, 0, s, t);\n }\n};",
"memory": "175868"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calcDistinct(string& s, string& t, int i, int j, vector<vector<int>>& mem) {\n\n\n if(j > t.size()) {\n return 1;\n } \n if(i > s.size()){\n return 0;\n }\n \n if(mem[i][j] != -1) {\n return mem[i][j];\n }\n\n if(s[i] == t[j]) {\n mem[i][j] = calcDistinct(s, t, i+1, j+1, mem) + calcDistinct(s, t, i+1, j, mem);\n return mem[i][j];\n } else{\n mem[i][j] = calcDistinct(s, t, i+1, j, mem);\n return mem[i][j];\n }\n\n mem[i][j] = 0;\n return 0;\n\n }\n\n int numDistinct(string s, string t) {\n vector<vector<int>> mem(1001, vector<int>(1001,-1)); \n return calcDistinct(s, t, 0, 0, mem);\n }\n};",
"memory": "181046"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp { 1001, vector(1001, -1) };\n\n int numDistinct(string_view s, string_view t) {\n if (dp[s.size()][t.size()] != -1) return dp[s.size()][t.size()];\n if (s.empty()) return t.empty();\n \n if (s.front() == t.front()) {\n return dp[s.size()][t.size()] =\n numDistinct(s.substr(1), t.substr(1)) + numDistinct(s.substr(1), t);\n } else {\n return dp[s.size()][t.size()] = numDistinct(s.substr(1), t);\n }\n }\n};",
"memory": "186223"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int solve(int indS,int indT,string &s,string &t){\n if(indT >= t.size()) return 1;\n if(indS >= s.size()) return 0;\n if(dp[indS][indT] != -1) return dp[indS][indT];\n int pick = 0;\n int npick = solve(indS+1,indT,s,t);\n if(s[indS] == t[indT]) pick = solve(indS+1,indT+1,s,t);\n return dp[indS][indT] = pick + npick;\n }\n int numDistinct(string s, string t) {\n dp.assign(1001,vector<int>(1001,-1));\n return solve(0,0,s,t);\n }\n};",
"memory": "191401"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n // long long table[1002][1002];\n /*int calc(string &s, string &t, int i, int j){\n if(j==t.length()){\n return 1;\n }\n if( i>=s.length() ){\n return 0;\n }\n if(table[i][j]!=-1){\n return table[i][j];\n }\n int op1=0,op2=0;\n\n op1 = calc(s,t,i+1,j);\n if(s[i]==t[j]){\n op2 = calc(s,t,i+1,j+1);\n }\n return table[i][j] = op1+op2;\n }*/\n // dp[i][j] = (if eq) (dp[i+1][j+1]) + dp[i+1][j];\npublic:\n int numDistinct(string s, string t) {\n\n // for(int i=0;i<1002;i++){\n // for(int j=0;j<1002;j++){\n // table[i][j]=0;\n // }\n // }\n vector<vector<unsigned int>> table(1002,vector<unsigned int>(1002,0));\n for(int i=0;i<=s.length();i++){\n table[i][t.length()]=1;\n }\n\n\n // for(int i=0;i<t.length();i++){\n // if( s[s.length()-1] == t[i])\n // {\n // cout<<\"a\";\n // table[s.length()-1][i] = 1;\n // }\n // else\n // table[s.length()-1][i] = 0;\n // }\n for(int i=s.length()-1;i>=0;i--){\n for(int j=t.length()-1;j>=0;j--){\n \n table[i][j] = table[i+1][j];\n if(s[i] == t[j]){\n table[i][j] += table[i+1][j+1];\n }\n }\n }\n \n // for(int i=0;i<s.length()+1;i++){\n // for(int j=0;j<t.length()+1;j++){\n // cout<<\" \" <<table[i][j];\n // }\n // cout<<endl;\n // }\n\n return table[0][0];\n \n }\n};",
"memory": "196578"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.