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</...
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 ...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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 ...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
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</...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
114
<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p> <ul> <li>The &quot;linked list&quot; 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 <c...
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, TreeNod...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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(looku...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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() ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 *...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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) r...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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]=...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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[...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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]= so...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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, ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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>...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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=...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 tak...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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....
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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(ind...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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(ind...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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[...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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-...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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[...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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\...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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\...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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<...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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)...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 && ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ) {...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 strin...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 retu...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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--){...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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[ind...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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, ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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]...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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,ind...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s...
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 }...