id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n // int solve( vector<int>& coins , int amount , int sum , int index){\n // if(index == coins.size()){\n // if(sum == amount) return 1;\n // else return 0;\n // }\n // int includeCoin = solve(coins, amount, sum + coins[index], index);\n // int skipCoin = solve(coins, amount, sum, index + 1);\n\n // return includeCoin + skipCoin;\n // }\n\n int solve(vector<int>& coins,int amount,int sum,int index){\n\n if (sum == amount) return 1; \n if (sum > amount || index == coins.size()) return 0; \n\n int ways = 0;\n for (int i = index; i < coins.size(); ++i) {\n ways += solve(coins, amount, sum + coins[i], i); \n \n }\n return ways;\n \n }\n\npublic:\n int t[201][5001];\n // int unbounded(vector<int>& coins,int amount,int n){\n\n // if(amount == 0) return 1;\n \n // if (n ==0) return 0;\n // if (n <= 0 || amount < 0) return 0;\n // if(t[n][amount] !=-1) return t[n][amount];\n // if(coins[n-1]<=amount){\n // return t[n][amount] = unbounded(coins,amount-coins[n-1],n) + unbounded(coins,amount,n-1);\n // }else{\n // return t[n][amount] = unbounded(coins,amount,n-1);\n // }\n // }\n\n void solveTab(vector<int>& coins,int amount,int n,vector<vector<int>> &dp){\n\n for(int i=0;i<=n;i++){\n dp[i][0] = 1;\n }\n \n for(int i=1;i<=n;i++){\n for(int j=1;j<=amount;j++){\n if(coins[i-1]<=j){\n dp[i][j] = dp[i][j-coins[i-1]] + dp[i-1][j];\n }\n else{\n dp[i][j] = dp[i-1][j];\n }\n }\n }\n\n }\n int change(int amount, vector<int>& coins) {\n // return solve(coins,amount,0,0);\n int n = coins.size();\n // memset(t,-1,sizeof(t));\n vector<vector<int>> dp(n+1,vector<int>(amount+1,0));\n solveTab(coins,amount,n,dp);\n return dp[n][amount];\n }\n};",
"memory": "24485"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "int DP[301][15001];\n\nint func(int idx,int amount,vector<int>& coins){\n if(amount==0)return 1;\n if(idx<0)return 0;\n\n if(DP[idx][amount]!=-1)return DP[idx][amount];\n\n int ways=0;\n for(int coinamount=0;coinamount<=amount;coinamount+=coins[idx]){\n ways+=func(idx-1,amount-coinamount,coins);\n }\n\n return DP[idx][amount]=ways;\n}\n\n\nclass Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n\n for(int i=0;i<301;i++){\n for(int j=0;j<15001;j++)DP[i][j]=-1;\n }\n return func(coins.size()-1,amount,coins);\n }\n};",
"memory": "25355"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n int dp[400][10001];\n int solve(int i, int amount, vector<int>& coins) {\n if (amount < 0 || i >= coins.size()) return 0;\n if (amount == 0) return 1;\n if (dp[i][amount] != -1) return dp[i][amount];\n if (amount >= coins[i]) {\n return dp[i][amount] = solve(i, amount - coins[i], coins) + solve(i + 1, amount, coins);\n }\n return dp[i][amount] = solve(i + 1, amount, coins);\n\n }\n int change(int amount,vector<int>& coins) {\n for (int i = 0; i < 400; i++) {\n for (int j = 0; j < 10001; j++) {\n dp[i][j] = -1;\n }\n }\n int ans = solve(0, amount, coins);\n\n return ans ;\n }\n};",
"memory": "25645"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[302][5002];\n int solvememo(int amount,vector<int>&arr,int idx){\n if(idx==arr.size()){\n if(amount==0) return 1;\n return 0;\n }\n if(dp[idx][amount]!=-1) return dp[idx][amount];\n int take = 0;\n int nottake=solvememo(amount,arr,idx+1);\n if(arr[idx]<=amount)\n take=solvememo(amount-arr[idx],arr,idx);\n return dp[idx][amount]=take+nottake;\n }\n int solvetabulation(vector<int>&arr,int amount){\n int n= arr.size();\n int dp[n+1][amount+1];\n for(int i=0;i<=n;i++){\n for(int j=0;j<=amount;j++){\n dp[i][j]=0;\n if(j==0) dp[i][j]=1;\n }\n }\n for(int i=1;i<=n;i++){\n for(int j=1;j<=amount;j++){\n if(arr[i-1]<=j){\n dp[i][j]=dp[i-1][j]+dp[i][j-arr[i-1]];\n }\n else{\n dp[i][j]=dp[i-1][j];\n }\n }\n }\n return dp[n][amount];\n }\n int solvetabulationspaceoptimisation(vector<int>&arr,int amount){\n int n= arr.size();\n vector<int> pre(amount+1,0);\n pre[0]=1;\n for(int i=1;i<=n;i++){\n vector<int> curr(amount+1,0);\n curr[0]=1;\n for(int j=1;j<=amount;j++){\n if(arr[i-1]<=j){\n curr[j]=pre[j]+curr[j-arr[i-1]];\n }\n else{\n curr[j]=pre[j];\n }\n }\n pre=curr;\n }\n return pre[amount];\n }\n int change(int amount, vector<int>& arr) {\n // memset(dp,-1,sizeof(dp));\n // solvememo(amount,arr,0);\n //solvetabulation(arr,amount);\n return solvetabulationspaceoptimisation(arr,amount);\n }\n};",
"memory": "25935"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "// e.g:- 1,1,1,2 and 2,1,1,1 are same {that's why we don't use for loop}\n\n//Approach-1 (Recursion + Mempozation) : O(n*amount)\nclass Solution {\npublic:\n int n;\n int t[301][5001];\n \n\n // int solve(int i, vector<int>& coins, int amount) {\n \n // if(amount == 0)\n // return t[i][amount] = 1;\n \n // if(i == n || amount < 0)\n // return 0;\n \n // if(t[i][amount] != -1)\n // return t[i][amount];\n \n // if(coins[i] > amount)\n // return t[i][amount] = solve(i+1, coins, amount);\n \n // int take = solve(i, coins, amount-coins[i]);\n // int skip = solve(i+1, coins, amount);\n \n // return t[i][amount] = take+skip;\n \n // }\n \n\n int solveTab(vector<int>& coins, int amt) {\n int n = coins.size();\n vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n \n // There's exactly one way to make amount 0 (by not using any coins)\n for(int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n \n // Fill the DP table\n for(int i = 1; i <= n; i++) {\n for(int amount = 1; amount <= amt; amount++) {\n int take = 0;\n if(amount >= coins[i - 1]) // Use i-1 because dp is 1-based indexing\n take = dp[i][amount - coins[i - 1]];\n int skip = dp[i - 1][amount];\n \n dp[i][amount] = take + skip;\n }\n }\n \n return dp[n][amt]; // Return the number of ways to make amount using all coins\n }\n int change(int amount, vector<int>& coins) {\n n = coins.size();\n // memset(t, -1, sizeof(t));\n // return solve(0, coins, amount);\n\n return solveTab(coins,amount);\n\n }\n};",
"memory": "26225"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "//Approch 1\nclass Solution {\npublic:\n /*this code find ways and its combination in ques we have to not find combination*/\n int solve(vector<int>&arr,int target,vector<int> path) {\n int n=arr.size();\n if(target==0) {\n for(auto &x:path) {\n cout<<x<<\" \";\n }cout<<endl;\n return 1;\n }\n if(target<0) {\n return 0;\n }\n int ans=0;\n for(int i=0;i<n;i++) {\n path.push_back(arr[i]);\n ans+=solve(arr,target-arr[i],path);\n path.pop_back();\n }\n return ans;\n }\n int dp[301][5001];\n int solve2(vector<int>&arr,int index,int target) {\n if(index>=arr.size()) {\n return target==0;\n }\n if(target==0) {\n return 1;\n }\n if(dp[index][target]!=-1) {\n return dp[index][target];\n }\n int take=0;\n int not_take=solve2(arr,index+1,target);\n if(target-arr[index]>=0) {\n //here by not change index we take all arr[i] coin at once\n take=solve2(arr,index,target-arr[index]);\n }\n return dp[index][target]=take+not_take;\n }\n\n int tab(vector<int>&arr,int amount) {\n int n=arr.size();\n vector<vector<int>>dp(n+1,vector<int>(amount+1,0));\n for(int i=0;i<=n;i++) {\n dp[i][0]=1;\n }\n for(int index=n-1;index>-1;index--) {\n for(int target=0;target<=amount;target++) {\n int take=0;\n int not_take=dp[index+1][target];\n if(target-arr[index]>=0) {\n //here by not change index we take all arr[i] coin at once\n take=dp[index][target-arr[index]];\n }\n dp[index][target]=take+not_take;\n } \n }\n return dp[0][amount];\n }\n\n int change(int amount, vector<int>& coins) {\n // memset(dp,-1,sizeof(dp));\n // int ans=solve2(coins,0,amount);\n int ans=tab(coins,amount);\n return ans;\n }\n};",
"memory": "26515"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "// e.g:- 1,1,1,2 and 2,1,1,1 are same {that's why we don't use for loop}\n\n//Approach-1 (Recursion + Mempozation) : O(n*amount)\nclass Solution {\npublic:\n int n;\n int t[301][5001];\n \n\n // int solve(int i, vector<int>& coins, int amount) {\n \n // if(amount == 0)\n // return t[i][amount] = 1;\n \n // if(i == n || amount < 0)\n // return 0;\n \n // if(t[i][amount] != -1)\n // return t[i][amount];\n \n // if(coins[i] > amount)\n // return t[i][amount] = solve(i+1, coins, amount);\n \n // int take = solve(i, coins, amount-coins[i]);\n // int skip = solve(i+1, coins, amount);\n \n // return t[i][amount] = take+skip;\n \n // }\n \n\n int solveTab(vector<int>& coins, int amt) {\n int n=coins.size();\n vector<vector<int>>dp(n+1,vector<int>(amt+1,0));\n \n for(int i=0;i<n;i++){\n dp[i][0]=1;\n }\n \n for(int i=n-1;i>=0;i--)\n {\n for(int amount=1;amount<=amt;amount++)\n {\n int take=0;\n if(amount>=coins[i])\n take = dp[i][amount-coins[i]];\n int skip = dp[i+1][amount];\n \n dp[i][amount] = take+skip;\n }\n }\n return dp[0][amt];\n \n \n }\n int change(int amount, vector<int>& coins) {\n n = coins.size();\n // memset(t, -1, sizeof(t));\n // return solve(0, coins, amount);\n\n return solveTab(coins,amount);\n\n }\n};",
"memory": "26805"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "\n class MyHash\n{\npublic:\n size_t operator () (const pair<int, int>& tuple) const\n {\n return hash<int>()(tuple.first) ^ hash<int>()(tuple.second);\n }\n};\n\nclass Solution {\n vector<vector<int>> tab;\n vector<int> tab_cc;\n\npublic:\n \n int changeCoins (vector<int>& coins, int n, int amount)\n {\n //assert (amount >= 0);\n \n if (amount == 0)\n return 1;\n\n if (n <= 0)\n {\n return 0;\n }\n\n if (tab[amount][n] != -1)\n return tab[amount][n];\n\n \n int changedCoins = changeCoins (coins, n-1, amount);\n \n if (amount >= coins[n-1]) \n changedCoins += changeCoins (coins, n, amount - coins[n-1]);\n\n return tab[amount][n] = changedCoins;\n }\n\n\n// int changeCoins2_leetcode518_dp (vector<int>& coins, int amount)\n// {\n// int n = coins.size(), curr_amount , curr_n;\n// vector<vector<int>> dp(amount+1, vector <int>(n + 1, 0));\n \n// dp[0][0] = 1;\n// for (curr_n = 1; curr_n <= n; curr_n++)\n// {\n// for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n// {\n// dp[curr_amount][curr_n] = dp[curr_amount][curr_n - 1];\n// if (curr_amount >= coins[curr_n - 1])\n// {\n// dp[curr_amount][curr_n] += dp[curr_amount - coins[curr_n-1]][curr_n];\n// }\n// }\n// } \n\n// return dp[amount][n];\n// }\n\nint changeCoins2_leetcode518_dp (vector<int>& coins, int amount)\n {\n int n = coins.size(), curr_amount , curr_n;\n vector<vector<int>> dp(amount+1, vector <int>(n + 1, 0));\n \n dp[0][0] = 1;\n for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n {\n for (curr_n = 1; curr_n <= n; curr_n++) \n {\n dp[curr_amount][curr_n] = dp[curr_amount][curr_n - 1];\n if (curr_amount >= coins[curr_n - 1])\n {\n dp[curr_amount][curr_n] += dp[curr_amount - coins[curr_n-1]][curr_n];\n }\n }\n } \n\n return dp[amount][n];\n }\n\n// int changeCoins2_leetcode518_dp2 (vector<int>& coins, int amount)\n// {\n// int n = coins.size(), curr_amount , curr_n;\n// vector<int> dp(amount+1);\n \n// dp[0][0] = 1;\n// for (curr_n = 1; curr_n <= n; curr_n++)\n// {\n// for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n// {\n// if (curr_amount >= coins[curr_n - 1])\n// {\n// dp[curr_amount] += dp[curr_amount - coins[curr_n-1]];\n// }\n// }\n// } \n\n// return dp[amount];\n// }\n\n int changeCoinsMap (vector<int>& coins, int n, int amount, unordered_map<pair<int, int>, int, MyHash>& cacheMap)\n {\n assert (amount >= 0);\n \n if (n == 0)\n {\n if (amount ==0 ) \n return 1;\n\n return 0;\n \n }\n \n auto it = cacheMap.find ({amount, n});\n if (it != cacheMap.end())\n return it->second;\n\n int changedCoins = 0, curr_amount = amount;\n int countLastCoin = amount / coins[n-1];\n for (int i = 0; i<=countLastCoin; i++)\n {\n changedCoins += changeCoinsMap (coins, n-1, curr_amount ,cacheMap);\n curr_amount -= coins[n-1];\n }\n\n return cacheMap[{amount, n}] = changedCoins;\n }\n\n\n // int changeCoins2 (vector<int>& coins, int amount)\n // {\n // int n = coins.size() ;\n // vector<vector<int>> dp(n+1, vector <int>(amount + 1, 0));\n \n // dp[0][0] = 1;\n // for (int curr_n = 1; curr_n <=n ; curr_n++)\n // {\n // for (int curr_amount = 0; curr_amount <= amount; curr_amount++)\n // {\n // int changedCoins = 0, temp_amount = curr_amount;\n // int countLastCoin = curr_amount / coins[curr_n-1];\n // for (int i = 0; i<=countLastCoin; i++)\n // {\n // changedCoins += dp[curr_n-1][temp_amount];\n // temp_amount -= coins[curr_n-1];\n // }\n\n // dp[curr_n][curr_amount] = changedCoins;\n // }\n // }\n\n // return dp[n][amount];\n // }\n\n\n // int changeCoins3 (vector<int>& coins, int amount)\n // {\n // int n = coins.size(), curr_amount ;\n // vector<vector<int>> dp(2, vector <int>(amount + 1, 0));\n \n\n // dp[0][0] = 1;\n // int prev_n_dp_idx =0, n_dp_idx = 1;\n // for (int curr_n = 1; curr_n <=n ; curr_n++)\n // {\n // for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n // {\n // int changedCoins = 0, temp_amount = curr_amount;\n // int countLastCoin = curr_amount / coins[curr_n-1];\n // for (int i = 0; i<=countLastCoin; i++)\n // {\n // changedCoins += dp[prev_n_dp_idx][temp_amount];\n // temp_amount -= coins[curr_n-1];\n // }\n\n // dp[n_dp_idx][curr_amount] = changedCoins;\n // }\n\n // prev_n_dp_idx = n_dp_idx;\n // n_dp_idx = (n_dp_idx + 1) & 1; \n // }\n\n // return dp[prev_n_dp_idx][amount];\n // }\n\n // int changeCoins4 (vector<int>& coins, int amount)\n // {\n // assert (amount >= 0);\n\n // if (amount ==0 )\n // return 1;\n \n // if (tab_cc[amount] != -1)\n // return tab_cc[amount];\n\n // int changedCoins = 0;\n // for (int i = 0; i<=coins.size(); i++)\n // {\n // if (amount < coins[i])\n // continue;\n\n // changedCoins += changeCoins4 (coins, amount - coins[i]);\n // // }\n\n // // return tab_cc[amount] = changedCoins;\n // // }\n\n\n\n int change(int amount, vector<int>& coins) {\n\n unordered_map<pair<int, int>, int, MyHash> cacheMap;\n return changeCoinsMap (coins, coins.size(), amount, \n cacheMap);\n \n // tab_cc = vector<int> (amount+1, -1);\n // return changeCoins4 (coins, amount);\n \n // return changeCoins3(coins, amount);\n // if (amount < 0)\n // return 0;\n\n //return changeCoins2_leetcode518_dp (coins,amount);\n tab = vector<vector<int>> (amount+1, vector <int>(coins.size()+1, -1));\n return changeCoins (coins, coins.size(), amount);\n }\n};",
"memory": "26805"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "\n class MyHash\n{\npublic:\n size_t operator () (const pair<int, int>& tuple) const\n {\n return hash<int>()(tuple.first) ^ hash<int>()(tuple.second);\n }\n};\n\nclass Solution {\n vector<vector<int>> tab;\n vector<int> tab_cc;\n\npublic:\n \n int changeCoins (vector<int>& coins, int n, int amount)\n {\n //assert (amount >= 0);\n \n if (amount == 0)\n return 1;\n\n if (n <= 0)\n {\n return 0;\n }\n\n if (tab[amount][n] != -1)\n return tab[amount][n];\n\n \n int changedCoins = changeCoins (coins, n-1, amount);\n \n if (amount >= coins[n-1]) \n changedCoins += changeCoins (coins, n, amount - coins[n-1]);\n\n return tab[amount][n] = changedCoins;\n }\n\n\n// int changeCoins2_leetcode518_dp (vector<int>& coins, int amount)\n// {\n// int n = coins.size(), curr_amount , curr_n;\n// vector<vector<int>> dp(amount+1, vector <int>(n + 1, 0));\n \n// dp[0][0] = 1;\n// for (curr_n = 1; curr_n <= n; curr_n++)\n// {\n// for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n// {\n// dp[curr_amount][curr_n] = dp[curr_amount][curr_n - 1];\n// if (curr_amount >= coins[curr_n - 1])\n// {\n// dp[curr_amount][curr_n] += dp[curr_amount - coins[curr_n-1]][curr_n];\n// }\n// }\n// } \n\n// return dp[amount][n];\n// }\n\nint changeCoins2_leetcode518_dp (vector<int>& coins, int amount)\n {\n int n = coins.size(), curr_amount , curr_n;\n vector<vector<int>> dp(amount+1, vector <int>(n + 1, 0));\n \n dp[0][0] = 1;\n for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n {\n for (curr_n = 1; curr_n <= n; curr_n++) \n {\n dp[curr_amount][curr_n] = dp[curr_amount][curr_n - 1];\n if (curr_amount >= coins[curr_n - 1])\n {\n dp[curr_amount][curr_n] += dp[curr_amount - coins[curr_n-1]][curr_n];\n }\n }\n } \n\n return dp[amount][n];\n }\n\n// int changeCoins2_leetcode518_dp2 (vector<int>& coins, int amount)\n// {\n// int n = coins.size(), curr_amount , curr_n;\n// vector<int> dp(amount+1);\n \n// dp[0][0] = 1;\n// for (curr_n = 1; curr_n <= n; curr_n++)\n// {\n// for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n// {\n// if (curr_amount >= coins[curr_n - 1])\n// {\n// dp[curr_amount] += dp[curr_amount - coins[curr_n-1]];\n// }\n// }\n// } \n\n// return dp[amount];\n// }\n\n int changeCoinsMap (vector<int>& coins, int n, int amount, unordered_map<pair<int, int>, int, MyHash>& cacheMap)\n {\n assert (amount >= 0);\n \n if (n == 0)\n {\n if (amount ==0 ) \n return 1;\n\n return 0;\n \n }\n \n auto it = cacheMap.find ({amount, n});\n if (it != cacheMap.end())\n return it->second;\n\n int changedCoins = 0, curr_amount = amount;\n int countLastCoin = amount / coins[n-1];\n for (int i = 0; i<=countLastCoin; i++)\n {\n changedCoins += changeCoinsMap (coins, n-1, curr_amount ,cacheMap);\n curr_amount -= coins[n-1];\n }\n\n return cacheMap[{amount, n}] = changedCoins;\n }\n\n\n // int changeCoins2 (vector<int>& coins, int amount)\n // {\n // int n = coins.size() ;\n // vector<vector<int>> dp(n+1, vector <int>(amount + 1, 0));\n \n // dp[0][0] = 1;\n // for (int curr_n = 1; curr_n <=n ; curr_n++)\n // {\n // for (int curr_amount = 0; curr_amount <= amount; curr_amount++)\n // {\n // int changedCoins = 0, temp_amount = curr_amount;\n // int countLastCoin = curr_amount / coins[curr_n-1];\n // for (int i = 0; i<=countLastCoin; i++)\n // {\n // changedCoins += dp[curr_n-1][temp_amount];\n // temp_amount -= coins[curr_n-1];\n // }\n\n // dp[curr_n][curr_amount] = changedCoins;\n // }\n // }\n\n // return dp[n][amount];\n // }\n\n\n // int changeCoins3 (vector<int>& coins, int amount)\n // {\n // int n = coins.size(), curr_amount ;\n // vector<vector<int>> dp(2, vector <int>(amount + 1, 0));\n \n\n // dp[0][0] = 1;\n // int prev_n_dp_idx =0, n_dp_idx = 1;\n // for (int curr_n = 1; curr_n <=n ; curr_n++)\n // {\n // for (curr_amount = 0; curr_amount <= amount; curr_amount++)\n // {\n // int changedCoins = 0, temp_amount = curr_amount;\n // int countLastCoin = curr_amount / coins[curr_n-1];\n // for (int i = 0; i<=countLastCoin; i++)\n // {\n // changedCoins += dp[prev_n_dp_idx][temp_amount];\n // temp_amount -= coins[curr_n-1];\n // }\n\n // dp[n_dp_idx][curr_amount] = changedCoins;\n // }\n\n // prev_n_dp_idx = n_dp_idx;\n // n_dp_idx = (n_dp_idx + 1) & 1; \n // }\n\n // return dp[prev_n_dp_idx][amount];\n // }\n\n // int changeCoins4 (vector<int>& coins, int amount)\n // {\n // assert (amount >= 0);\n\n // if (amount ==0 )\n // return 1;\n \n // if (tab_cc[amount] != -1)\n // return tab_cc[amount];\n\n // int changedCoins = 0;\n // for (int i = 0; i<=coins.size(); i++)\n // {\n // if (amount < coins[i])\n // continue;\n\n // changedCoins += changeCoins4 (coins, amount - coins[i]);\n // // }\n\n // // return tab_cc[amount] = changedCoins;\n // // }\n\n\n\n int change(int amount, vector<int>& coins) {\n\n unordered_map<pair<int, int>, int, MyHash> cacheMap;\n return changeCoinsMap (coins, coins.size(), amount, \n cacheMap);\n \n // tab_cc = vector<int> (amount+1, -1);\n // return changeCoins4 (coins, amount);\n \n // return changeCoins3(coins, amount);\n // if (amount < 0)\n // return 0;\n\n //return changeCoins2_leetcode518_dp (coins,amount);\n tab = vector<vector<int>> (amount+1, vector <int>(coins.size()+1, -1));\n return changeCoins (coins, coins.size(), amount);\n }\n};",
"memory": "27095"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int solve(int idx, int target, vector<int>& coins){\n if(idx==0){\n if(target%coins[idx]==0)\n return 1;\n return 0;\n }\n if(dp[idx][target]!=-1)\n return dp[idx][target];\n int not_take = solve(idx-1, target, coins);\n int take=0;\n if(coins[idx] <= target)\n take = solve(idx, target-coins[idx], coins);\n \n return dp[idx][target] = not_take + take;\n }\n int change(int amount, vector<int>& coins) {\n dp.resize(coins.size(), vector<int>(5001, -1));\n int res = solve(coins.size()-1, amount, coins);\n if(res == 1e9)\n return -1;\n return res;\n }\n};",
"memory": "27095"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(vector<int>& coins,int amount,int i,vector<vector<int>>& dp){\n if(i<0)return 0;\n if(amount==0)return 1;\n if(dp[i][amount]!=-1)return dp[i][amount];\n int notake=solve(coins,amount,i-1,dp);\n int take=0;\n if(amount>=coins[i])\n take=solve(coins,amount-coins[i],i,dp);\n return dp[i][amount]=take+notake;\n }\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n vector<vector<int>>dp(n,vector<int>(5005,-1));\n return solve(coins,amount,n-1,dp);\n }\n};",
"memory": "27385"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int coin(int i, vector<int> &coins, int amount, int curr,vector<vector<int>> &dp) {\n if (i >= coins.size()) {\n return curr == amount ? 1 : 0;\n }\n if (curr > amount) {\n return 0;\n }\n if(dp[i][curr]!=-1){\n return dp[i][curr];\n }\n\n int ans = 0;\n ans += coin(i, coins, amount, curr + coins[i],dp);\n ans += coin(i + 1, coins, amount, curr,dp);\n\n return dp[i][curr]=ans;\n }\n\n int change(int amount, vector<int>& coins) {\n vector<vector<int>> dp(coins.size(),vector<int> (5000+1,-1));\n return coin(0, coins, amount, 0,dp);\n }\n};\n",
"memory": "27385"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size(), i, j;\n vector<vector<int>> dp(n+1, vector<int>(5001,0));\n for(i=0; i<=n; i++){\n dp[i][0] = 1;\n }\n \n // rec relation is -> include atleast one of coins[i]\n // f(i)(j) = f(i-1)(j-coins[i])+f(i-1)(j)\n for(i=1; i<=n; i++){\n for(j=1; j<=amount; j++){\n int contri = 0;\n if(j-coins[i-1]>=0)\n contri = dp[i][j-coins[i-1]];\n dp[i][j] = contri+dp[i-1][j];\n }\n }\n return dp[n][amount];\n }\n};",
"memory": "27675"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size(), i, j;\n vector<vector<int>> dp(n+1, vector<int>(5001,0));\n for(i=0; i<=n; i++){\n dp[i][0] = 1;\n }\n \n // rec relation is -> include atleast one of coins[i]\n // f(i)(j) = f(i)(j-coins[i])+f(i-1)(j)\n for(i=1; i<=n; i++){\n for(j=1; j<=amount; j++){\n int contri = 0;\n if(j-coins[i-1]>=0)\n contri = dp[i][j-coins[i-1]];\n dp[i][j] = contri+dp[i-1][j];\n }\n }\n return dp[n][amount];\n }\n};",
"memory": "27675"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int solve(vector<int>& coins,int i,int x)\n {\n if(x==0) return 1;\n if(i>=coins.size() || x<0) return 0;\n if(dp[i][x]!=-1) return dp[i][x];\n return dp[i][x] = solve(coins,i,x-coins[i])+solve(coins,i+1,x);\n }\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n dp.resize(n+1,vector<int>(5001,-1));\n return solve(coins,0,amount);\n }\n};",
"memory": "27965"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int f(int amt, vector<int>& c, int ind, int sum, vector<vector<int>>& dp) {\n if (sum==amt) {\n return 1;\n }\n if (sum > amt) return 0;\n if (ind < 0) {\n return (sum==amt);\n }\n if (dp[ind][sum] != -1) return dp[ind][sum];\n int take = f(amt, c, ind, sum+c[ind], dp);\n int notake = f(amt, c, ind-1, sum, dp);\n return dp[ind][sum] = take+notake;\n }\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<vector<int>> dp(n+1,vector<int> (5001 , -1));\n return f(amount, coins,coins.size()-1, 0, dp);\n }\n};",
"memory": "27965"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(int ind, int currsum, vector<int>& coins, int amount, vector<vector<int>>& dp) {\n if(currsum > amount) return 0;\n if(currsum == amount) return 1;\n if(ind == coins.size()) return 0;\n if(dp[ind][currsum] != -1) return dp[ind][currsum];\n int a = solve(ind, currsum + coins[ind], coins, amount, dp);\n int b = solve(ind + 1, currsum, coins, amount, dp);\n return dp[ind][currsum] = a + b;\n }\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<vector<int>> dp(n+1, vector<int>(5001,-1));\n int ans = solve(0, 0, coins, amount, dp);\n return ans;\n }\n};",
"memory": "28255"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\tint change(int amount, const vector<int>& coins) {\n\t\tif ((amount == 0) && (!coins.empty()))\n\t\t\treturn 1;\n\n\t\tauto sortedCoins = coins;\n\t\tstd::sort(sortedCoins.begin(), sortedCoins.end(), std::greater<int>());\n\t\treturn changeImpl(amount, sortedCoins, 0);\n\t}\n\n\tint changeImpl(int amount, const vector<int>& coins, int coinIndex) {\n\t\tauto itSolution = _solutions.find(amount);\n\t\tif (itSolution != _solutions.end())\n\t\t{\n\t\t\tauto itSolutionEx = itSolution->second.find(coinIndex);\n\t\t\tif (itSolutionEx != itSolution->second.end())\n\t\t\t\treturn itSolutionEx->second;\n\t\t}\n\t\t\n\t\tif (coinIndex == coins.size() - 1)\n\t\t{\n\t\t\tint result = (amount % coins[coins.size() - 1] == 0) ? 1 : 0;\n\t\t\t_solutions[amount][coinIndex] = result;\n\t\t\treturn result;\n\t\t}\n\n\t\tint result = 0;\n\t\tif (amount % coins[coinIndex] == 0)\n\t\t\tresult += 1;\n\n\t\tfor (auto temp = 0; temp < amount; temp += coins[coinIndex])\n\t\t{\n\t\t\tresult += changeImpl(amount - temp, coins, coinIndex + 1);\n\t\t}\n\t\t_solutions[amount][coinIndex] = result;\n\t\treturn result;\n\t}\nprivate:\n\tstd::map<int, std::map<int, int>> _solutions;\n};",
"memory": "28255"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nunordered_map<int ,unordered_map<int,int>>memo;\n int f(int ind,int target, vector<int>&coins){\n if(ind==0) return (target % coins[0]==0);\n if(memo[ind].find(target)!=memo[ind].end()) return memo[ind][target];\n int not_take=f(ind-1,target,coins);\n int take=0;\n if(target>=coins[ind]) take=f(ind,target-coins[ind],coins);\n return memo[ind][target]=not_take+take; \n }\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n return f(n-1,amount,coins);\n }\n};",
"memory": "28545"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nunordered_map<int ,unordered_map<int,int>>memo;\n int f(int ind,int target, vector<int>&coins){\n if(ind==0) return (target % coins[0]==0);\n if(memo[ind].find(target)!=memo[ind].end()) return memo[ind][target];\n int not_take=f(ind-1,target,coins);\n int take=0;\n if(target>=coins[ind]) take=f(ind,target-coins[ind],coins);\n return memo[ind][target]=not_take+take; \n }\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n return f(n-1,amount,coins);\n }\n};",
"memory": "28545"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int dp[301][5001];\n int solve(int amount,int idx,vector<int>&coins){\n \n if(amount==0)return 1;\n if(dp[idx][amount]!=-1)return dp[idx][amount];\n int count=0;\n for(int i=idx;i<coins.size();i++){\n if(amount>=coins[i]){\n count+=solve(amount-coins[i],i,coins);\n }\n }\n return dp[idx][amount]=count;\n }\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n vector<vector<int>>dp(amount+1,vector<int>(n+1,0));\n for(int i=0;i<n;i++){\n dp[0][i]=1;\n }\n\n int ans=0;\n\n for(int val=1;val<=amount;val++){\n for(int i=0;i<n;i++){\n if(i>0){\n dp[val][i]=dp[val][i-1];\n }\n if(val-coins[i]>=0){\n dp[val][i]+=dp[val-coins[i]][i];\n }\n }\n \n }\n\n \n return dp[amount][n-1];\n }\n};",
"memory": "28835"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[5001][301];\n int help(int amt, int idx, vector<int>& nums) {\n if (amt == 0)\n return 1;\n if (idx >= nums.size())\n return 0;\n if (dp[amt][idx] != -1)\n return dp[amt][idx];\n int take = 0;\n if (amt - nums[idx] >= 0)\n take = help(amt - nums[idx], idx, nums);\n int notTake = help(amt, idx + 1, nums);\n\n return dp[amt][idx] = take + notTake;\n }\n int change(int amount, vector<int>& coins) {\n // memset(dp , -1 , sizeof (dp)) ;\n // return help(amount, 0, coins);\n int n = coins.size();\n vector<vector<int>> dp(amount + 2, vector<int>(n + 2, 0));\n\n for (int idx = 0; idx <= n; idx++)\n dp[0][idx] = 1;\n\n for (int amt = 0; amt <= amount; amt++)\n for (int idx = n - 1; idx >= 0; idx--) {\n int take = 0;\n if (amt - coins[idx] >= 0)\n take = dp[amt - coins[idx]][idx];\n int notTake = dp[amt][idx + 1]; //(amt, idx + 1, nums);\n\n dp[amt][idx] = take + notTake;\n }\n return dp[amount][0];\n }\n};",
"memory": "29125"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size() ;\n vector<long> prev(amount+1,0) ; // prev[T] = Number of ways of forming amount T using coins upto previous index (initially 0)\n\n // BASE CASE : If only one coin is left to choose from , then it is possible to form the target amount only if target is a multiple of the coin \n for (int i = 0; i <= amount; i++) {\n if (i % coins[0] == 0)\n prev[i] = 1; // There is one way to make change for target equal to multiples of the first coin\n // Else condition is automatically fulfilled, as the prev vector is initialized to zero\n }\n\n // Bottom-Up Tabulation\n for (int ind = 1; ind < n; ind++) {\n vector<long> curr(amount + 1, 0); // Create a vector to store the current DP state\n for (int target = 0; target <= amount; target++) {\n long notTaken = prev[target]; // Number of ways without taking the current coin\n\n long taken = 0;\n if (coins[ind] <= target)\n taken = curr[target - coins[ind]]; // Number of ways by taking the current coin\n \n curr[target] = notTaken + taken; // Total number of ways for the current target\n }\n prev = curr; // Update the previous DP state with the current state for the next coin\n }\n\n return prev[amount]; // Return the total number of ways to make change for the target \n }\n};",
"memory": "29415"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int T, vector<int>& arr) {\n int n=arr.size();\n \n vector<long> prev(T + 1, 0); // Create a vector to store the previous DP state\n\n // Initialize base condition\n for (int i = 0; i <= T; i++) {\n if (i % arr[0] == 0)\n prev[i] = 1; // There is one way to make change for multiples of the first coin\n // Else condition is automatically fulfilled,\n // as the prev vector is initialized to zero\n }\n\n for (int ind = 1; ind < n; ind++) {\n vector<long> cur(T + 1, 0); // Create a vector to store the current DP state\n for (int target = 0; target <= T; target++) {\n long notTaken = prev[target]; // Number of ways without taking the current coin\n\n long taken = 0;\n if (arr[ind] <= target)\n taken = cur[target - arr[ind]]; // Number of ways by taking the current coin\n \n cur[target] = notTaken + taken; // Total number of ways for the current target\n }\n prev = cur; // Update the previous DP state with the current state for the next coin\n }\n\n return prev[T];\n }\n};",
"memory": "29705"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n int countWaysToMakeChangeUtil(vector<int>& arr, int ind, int T, vector<vector<int>>& dp) {\n // Base case: if we're at the first element\n if (ind == 0) {\n // Check if the target sum is divisible by the first element\n return (T % arr[0] == 0);\n }\n \n // If the result for this index and target sum is already calculated, return it\n if (dp[ind][T] != -1)\n return dp[ind][T];\n \n // Calculate the number of ways without taking the current element\n long notTaken = countWaysToMakeChangeUtil(arr, ind - 1, T, dp);\n \n // Calculate the number of ways by taking the current element\n long taken = 0;\n if (arr[ind] <= T)\n taken = countWaysToMakeChangeUtil(arr, ind, T - arr[ind], dp);\n \n // Store the sum of ways in the DP table and return it\n return dp[ind][T] = notTaken + taken;\n}\n\n long countWaysToMakeChange(vector<int>& arr, int n, int T) {\n vector<vector<long>> dp(n, vector<long>(T + 1, 0)); // Create a DP table\n\n // Initializing base condition\n for (int i = 0; i <= T; i++) {\n if (i % arr[0] == 0)\n dp[0][i] = 1;\n // Else condition is automatically fulfilled,\n // as dp array is initialized to zero\n }\n\n for (int ind = 1; ind < n; ind++) {\n for (int target = 0; target <= T; target++) {\n long notTaken = dp[ind - 1][target];\n\n long taken = 0;\n if (arr[ind] <= target)\n taken = dp[ind][target - arr[ind]];\n\n dp[ind][target] = notTaken + taken;\n }\n }\n\n return dp[n - 1][T];\n }\n\n long countWaysToMakeChangeSO(vector<int>& arr, int n, int T) {\n vector<long> prev(T + 1, 0); // Create a vector to store the previous DP state\n\n // Initialize base condition\n for (int i = 0; i <= T; i++) {\n if (i % arr[0] == 0)\n prev[i] = 1; // There is one way to make change for multiples of the first coin\n // Else condition is automatically fulfilled,\n // as the prev vector is initialized to zero\n }\n\n for (int ind = 1; ind < n; ind++) {\n vector<long> cur(T + 1, 0); // Create a vector to store the current DP state\n for (int target = 0; target <= T; target++) {\n long notTaken = prev[target]; // Number of ways without taking the current coin\n\n long taken = 0;\n if (arr[ind] <= target)\n taken = cur[target - arr[ind]]; // Number of ways by taking the current coin\n \n cur[target] = notTaken + taken; // Total number of ways for the current target\n }\n prev = cur; // Update the previous DP state with the current state for the next coin\n }\n\n return prev[T]; // Return the total number of ways to make change for the target\n }\n\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n // vector<vector<int>> dp(n, vector<int>(amount + 1, -1)); // Create a DP table\n \n // // Call the utility function to calculate the answer\n // return countWaysToMakeChangeUtil(coins, n - 1, amount, dp); \n\n return countWaysToMakeChangeSO(coins, n, amount);\n }\n};",
"memory": "29995"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n vector<long> prev(amount+1,0); \n for (int i=0;i<=amount;i++){\n if (i%coins[0]==0)\n prev[i]=1; \n }\n\n for (int ind = 1; ind < coins.size(); ind++) {\n vector<long> cur(amount + 1, 0); \n for (int target = 0; target <= amount; target++) {\n long notTaken = prev[target]; \n\n long taken = 0;\n if (coins[ind] <= target)\n taken =cur[target - coins[ind]]; \n \n cur[target] = notTaken + taken;\n }\n prev = cur; \n }\n\n return prev[amount]; \n}\n \n};",
"memory": "30285"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n int countWaysToMakeChangeUtil(vector<int>& arr, int ind, int T, vector<vector<int>>& dp) {\n // Base case: if we're at the first element\n if (ind == 0) {\n // Check if the target sum is divisible by the first element\n return (T % arr[0] == 0);\n }\n \n // If the result for this index and target sum is already calculated, return it\n if (dp[ind][T] != -1)\n return dp[ind][T];\n \n // Calculate the number of ways without taking the current element\n long notTaken = countWaysToMakeChangeUtil(arr, ind - 1, T, dp);\n \n // Calculate the number of ways by taking the current element\n long taken = 0;\n if (arr[ind] <= T)\n taken = countWaysToMakeChangeUtil(arr, ind, T - arr[ind], dp);\n \n // Store the sum of ways in the DP table and return it\n return dp[ind][T] = notTaken + taken;\n}\n\n long countWaysToMakeChange(vector<int>& arr, int n, int T) {\n vector<vector<long>> dp(n, vector<long>(T + 1, 0)); // Create a DP table\n\n // Initializing base condition\n for (int i = 0; i <= T; i++) {\n if (i % arr[0] == 0)\n dp[0][i] = 1;\n // Else condition is automatically fulfilled,\n // as dp array is initialized to zero\n }\n\n for (int ind = 1; ind < n; ind++) {\n for (int target = 0; target <= T; target++) {\n long notTaken = dp[ind - 1][target];\n\n long taken = 0;\n if (arr[ind] <= target)\n taken = dp[ind][target - arr[ind]];\n\n dp[ind][target] = notTaken + taken;\n }\n }\n\n return dp[n - 1][T];\n }\n\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n // vector<vector<int>> dp(n, vector<int>(amount + 1, -1)); // Create a DP table\n \n // // Call the utility function to calculate the answer\n // return countWaysToMakeChangeUtil(coins, n - 1, amount, dp); \n\n return countWaysToMakeChange(coins, n, amount);\n }\n};",
"memory": "30575"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int count_ways(int idx, int target, vector<vector<int>>& dp, vector<int>& coins){\n if(idx == 0){\n if(target % coins[idx] == 0){\n return 1;\n }\n else{\n return 0;\n }\n }\n if(dp[idx][target] != -1){\n return dp[idx][target];\n }\n int not_take = count_ways(idx-1, target, dp, coins);\n int take = 0;\n if(coins[idx] <= target){\n take = count_ways(idx, target - coins[idx], dp, coins);\n }\n return dp[idx][target] = take + not_take;\n }\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<vector<long>> dp(n, vector<long> (amount + 1, 0));\n for(int i = 0;i <= amount;i++){\n if(i % coins[0] == 0){\n dp[0][i] = 1;\n }\n }\n for(int i = 1;i < n;i++){\n for(int j = 0;j <= amount;j++){\n long not_take = dp[i-1][j];\n long take = 0;\n if(coins[i] <= j){\n take = dp[i][j - coins[i]];\n }\n dp[i][j] = take + not_take;\n }\n }\n return dp[n - 1][amount];\n }\n};",
"memory": "30865"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n\n// Function to count the number of ways to make change for a given target sum\nlong countWaysToMakeChangeUtil(vector<int>& arr, int ind, int T, vector<vector<long>>& dp) {\n // Base case: if we're at the first element\n if (ind == 0) {\n if(T%arr[0]==0) return 1;\n else return 0;\n // Check if the target sum is divisible by the first element\n // return (T % arr[0] == 0);\n }\n \n // If the result for this index and target sum is already calculated, return it\n if (dp[ind][T] != -1)\n return dp[ind][T];\n \n // Calculate the number of ways without taking the current element\n long notTaken = countWaysToMakeChangeUtil(arr, ind - 1, T, dp);\n \n // Calculate the number of ways by taking the current element\n long taken = 0;\n if (arr[ind] <= T)\n taken = countWaysToMakeChangeUtil(arr, ind, T - arr[ind], dp);\n \n // Store the sum of ways in the DP table and return it\n return dp[ind][T] = notTaken + taken;\n}\n\n\n\n\n int change(int T, vector<int>& arr) {\n int n=arr.size();\n vector<vector<long>> dp(n, vector<long>(T + 1, 0));\n // return countWaysToMakeChangeUtil(arr, n - 1, T, dp);\n for(int i=0;i<=T;i++){\n if(i%arr[0]==0)\n dp[0][i]=1;\n\n }\n\n for (int ind = 1; ind < n; ind++) {\n for (int target = 0; target <= T; target++) {\n long notTaken = dp[ind - 1][target];\n\n long taken = 0;\n if (arr[ind] <= target)\n taken = dp[ind][target - arr[ind]];\n\n dp[ind][target] = notTaken + taken;\n }\n }\n return dp[n-1][T]; }\n};",
"memory": "30865"
} |
518 | <p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic: long countWaysToMakeChangeUtil(vector<int>& arr, int ind, int T, vector<vector<long>>& dp) {\n // Base case: if we're at the first element\n if (ind == 0) {\n // Check if the target sum is divisible by the first element\n return (T % arr[0] == 0);\n }\n \n // If the result for this index and target sum is already calculated, return it\n if (dp[ind][T] != -1)\n return dp[ind][T];\n \n // Calculate the number of ways without taking the current element\n long notTaken = countWaysToMakeChangeUtil(arr, ind - 1, T, dp);\n \n // Calculate the number of ways by taking the current element\n long taken = 0;\n if (arr[ind] <= T)\n taken = countWaysToMakeChangeUtil(arr, ind, T - arr[ind], dp);\n \n // Store the sum of ways in the DP table and return it\n return dp[ind][T] = notTaken + taken;\n}\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n \n vector<vector<long>> dp(n, vector<long>(amount+ 1, -1)); // Create a DP table\n \n // Call the utility function to calculate the answer\n return countWaysToMakeChangeUtil(coins, n - 1, amount, dp);\n }\n};",
"memory": "31155"
} |
903 | <p>Given the <strong>API</strong> <code>rand7()</code> that generates a uniform random integer in the range <code>[1, 7]</code>, write a function <code>rand10()</code> that generates a uniform random integer in the range <code>[1, 10]</code>. You can only call the API <code>rand7()</code>, and you shouldn't call any other API. Please <strong>do not</strong> use a language's built-in random API.</p>
<p>Each test case will have one <strong>internal</strong> argument <code>n</code>, the number of times that your implemented function <code>rand10()</code> will be called while testing. Note that this is <strong>not an argument</strong> passed to <code>rand10()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> n = 1
<strong>Output:</strong> [2]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> n = 2
<strong>Output:</strong> [2,8]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> n = 3
<strong>Output:</strong> [3,8,10]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What is the <a href="https://en.wikipedia.org/wiki/Expected_value" target="_blank">expected value</a> for the number of calls to <code>rand7()</code> function?</li>
<li>Could you minimize the number of calls to <code>rand7()</code>?</li>
</ul>
| 0 | {
"code": "// The rand7() API is already defined for you.\n// int rand7();\n// @return a random integer in the range 1 to 7\n\nclass Solution {\n public:\n int rand10() {\n int num = 40;\n\n while (num >= 40)\n num = (rand7() - 1) * 7 + rand7() - 1;\n\n return num % 10 + 1;\n }\n};",
"memory": "9800"
} |
903 | <p>Given the <strong>API</strong> <code>rand7()</code> that generates a uniform random integer in the range <code>[1, 7]</code>, write a function <code>rand10()</code> that generates a uniform random integer in the range <code>[1, 10]</code>. You can only call the API <code>rand7()</code>, and you shouldn't call any other API. Please <strong>do not</strong> use a language's built-in random API.</p>
<p>Each test case will have one <strong>internal</strong> argument <code>n</code>, the number of times that your implemented function <code>rand10()</code> will be called while testing. Note that this is <strong>not an argument</strong> passed to <code>rand10()</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> n = 1
<strong>Output:</strong> [2]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> n = 2
<strong>Output:</strong> [2,8]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> n = 3
<strong>Output:</strong> [3,8,10]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What is the <a href="https://en.wikipedia.org/wiki/Expected_value" target="_blank">expected value</a> for the number of calls to <code>rand7()</code> function?</li>
<li>Could you minimize the number of calls to <code>rand7()</code>?</li>
</ul>
| 0 | {
"code": "// The rand7() API is already defined for you.\n// int rand7();\n// @return a random integer in the range 1 to 7\n\nclass Solution {\npublic:\n int rand10() {\n int ans=INT_MAX;\n while(ans>=40){\n ans=7*(rand7()-1)+rand7()-1;\n }\n return ans%10+1;\n\n }\n};",
"memory": "9800"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unordered_set<string> dict;\n unordered_map<string, bool> m;\n vector<int> dp;\n int n;\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n n = words.size();\n for(string w: words){\n dict.insert(w);\n }\n vector<string> res;\n for(string w : words) {\n int len = w.length();\n dp.assign(len, -1);\n if(solve(0, w, len)) {\n res.push_back(w);\n }\n }\n return res;\n }\n\n bool solve(int idx, string& s, int& len) { \n if(idx == len) {\n return true;\n }\n if(dp[idx] != -1) {\n return dp[idx];\n }\n for(int i=idx; i<len; i++) {\n string prefix = s.substr(idx, i-idx+1);\n if(!dict.count(prefix)) {\n continue;\n }\n string suffix = s.substr(i+1, len-i-1);\n cout << prefix << ',' << suffix << endl;\n if(dict.count(suffix) || (solve(i+1, s, len) && prefix != s)) {\n return true;\n }\n }\n return dp[idx] = false;\n }\n};",
"memory": "47398"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> result;\n unordered_set<string_view> wordList;\n for (auto& word : words) wordList.insert(string_view(word));\n\n for (auto& word : words) {\n vector<bool> dp(word.size(), false);\n string_view view(word);\n for (int i = 0; i < word.size(); i++) {\n for (int start = (i == (word.size() - 1)); start <= i; start++) {\n int size = i - start + 1;\n if ((start == 0 || dp[start - 1]) && wordList.contains(view.substr(start, size))) {\n dp[i] = true;\n break;\n }\n }\n }\n if (dp.back() == true)\n result.push_back(string(word.begin(), word.end()));\n }\n\n return result;\n }\n};",
"memory": "47398"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n\n unordered_set<string> s(words.begin(),words.end());\n int n = words.size();\n\n for(int k = 0; k < n; k++){\n int length = words[k].size();\n vector<bool> dp(length+1);\n dp[0] = true;\n for(int i = 1; i <= length; i++){\n for(int j = (i == length) ? 1:0; j < i && !dp[i]; j++){\n string sub = words[k].substr(j,i-j);\n dp[i] = dp[j] && s.count(sub);\n }\n if(dp[length]){\n ans.push_back(words[k]);\n }\n }\n }\n return ans;\n \n }\n};",
"memory": "50541"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n\n unordered_set<string> s(words.begin(),words.end());\n int n = words.size();\n\n for(int k = 0; k < n; k++){\n int length = words[k].size();\n vector<bool> dp(length+1);\n dp[0] = true;\n for(int i = 1; i <= length; i++){\n for(int j = (i == length) ? 1:0; j < i && !dp[i]; j++){\n string sub = words[k].substr(j,i-j);\n dp[i] = dp[j] && s.count(sub);\n }\n if(dp[length]){\n ans.push_back(words[k]);\n }\n }\n }\n return ans;\n \n }\n};",
"memory": "50541"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unordered_set<string> st;\n bool check(int ind, string &s , vector<int> &dp){\n \n int n = s.size();\n if(ind == n) return 1;\n\n if(dp[ind]!=-1) return dp[ind];\n string temp =\"\";\n bool a= false;\n //i 0 hoga to n-2 pe ruk jaega baki sab me n-1 tak jaega\n for(int i =0; i<min(n-1,n-ind+1);i++){\n temp+=s[i+ind];\n if(st.find(temp)!=st.end()){\n a= a || check(i+1+ind,s,dp);\n }\n }\n return dp[ind]=a;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n\n for(auto it: words){\n st.insert(it);\n }\n\n int n =words.size();\n for(int i =0;i<n; i++){\n int m = words[i].size();\n vector<int> dp(m,-1);\n if(check(0,words[i],dp)) ans.push_back(words[i]);\n }\n return ans;\n }\n};",
"memory": "53683"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\nmap<string, int> mp;\n for (const string& s : words) {\n mp[s]++;\n }\n vector<string> ans;\n for ( auto & s : words) {\n int n = s.length();\n mp[s]--;\n vector<bool> dp(n + 1, false);\n dp[0] = true; \n\n for (int i = 1; i <= n; ++i) {\n for (int j = 0; j < i; ++j) {\n if (dp[j] && mp.find(s.substr(j, i - j)) != mp.end() && mp[s.substr(j, i - j)] > 0) {\n dp[i] = true;\n break;\n }\n }\n }\n\n if (dp[n]) {\n ans.push_back(s);\n }\n mp[s]++;\n }\n\n return ans;\n\n }\n};",
"memory": "53683"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool check(unordered_set<string> &s, string word){\n int n = word.size();\n vector<int> dp(n+1,0);\n dp[0]=1;\n for (int i=1;i<=n;i++){\n for (int j=0;j<i;j++){\n if (dp[j] && (s.count(word.substr(j,i-j))) && (j>0 || i<n)){\n dp[i]=1;\n }\n }\n }\n return dp[n];\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string>s(words.begin(),words.end());\n vector<string> ans;\n for (auto word:s){\n if (check(s,word)){\n ans.push_back(word);\n }\n }\n return ans;\n }\n};",
"memory": "56826"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n struct CompareLength {\n inline bool operator() (const string& l, const string& r) {\n return l.length() < r.length();\n }\n };\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n = words.size();\n sort(words.begin(), words.end(), CompareLength());\n unordered_set<string> exists;\n vector<string> ans;\n vector<vector<bool>> dp(n, vector<bool>(30, false));\n for (int l=0;l<n;l++) {\n // cout << w << \"\\n\";\n const string w = words[l];\n int m = w.length();\n for (int i=0;i<m;i++) {\n for (int j=i;j>=0;j--) {\n if (exists.find(w.substr(j, i-j+1)) != exists.end()) {\n if (j == 0) dp[l][i] = true;\n else dp[l][i] = dp[l][i] || dp[l][j-1];\n }\n }\n }\n if (dp[l][m-1]) ans.push_back(w);\n exists.insert(w);\n }\n\n return ans;\n }\n};",
"memory": "56826"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> wordSet(words.begin(), words.end());\n vector<string> concatenatedWords;\n \n for (const string& word : words) {\n wordSet.erase(word); // Temporarily remove the word from the set\n if (canForm(word, wordSet)) {\n concatenatedWords.push_back(word);\n }\n wordSet.insert(word); // Add the word back to the set\n }\n \n return concatenatedWords;\n }\n\nprivate:\n bool canForm(const string& word, const unordered_set<string>& wordSet) {\n vector<bool> dp(word.size() + 1, false);\n dp[0] = true;\n\n for (size_t i = 1; i <= word.size(); ++i) {\n for (size_t j = 0; j < i; ++j) {\n if (dp[j] && wordSet.count(word.substr(j, i - j))) {\n dp[i] = true;\n break;\n }\n }\n }\n\n return dp[word.size()];\n }\n};",
"memory": "59968"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool canForm(const string& word, unordered_set<string>& wordSet) {\n int n = word.size();\n vector<bool> dp(n + 1, false);\n dp[0] = true;\n\n for (int i = 1; i <= n; ++i) {\n for (int j = 0; j < i; ++j) {\n if (dp[j] && wordSet.find(word.substr(j, i - j)) != wordSet.end()) {\n dp[i] = true;\n break;\n }\n }\n }\n\n return dp[n];\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> wordSet(words.begin(), words.end());\n vector<string> result;\n\n for (const string& word : words) {\n wordSet.erase(word); // Temporarily remove the word to avoid using itself\n if (canForm(word, wordSet)) {\n result.push_back(word);\n }\n wordSet.insert(word); // Add the word back to the set\n }\n\n return result;\n }\n};",
"memory": "59968"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool solve(string &s,unordered_set<string>& st,int i,vector<int>&dp){\n\n if(i == s.length())\n return true ;\n\n if(dp[i] != -1)\n return dp[i];\n\n for(int j=i; j<s.length(); j++){\n string temp = s.substr(i,j-i+1);\n if(st.find(temp) != st.end()){\n bool aageKaAns = solve(s,st,j+1,dp);\n if(aageKaAns) return dp[i] = true ;\n }\n }\n\n return dp[i] = false ;\n }\n\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans ;\n unordered_set<string> st(words.begin(),words.end());\n\n for(auto s:words){\n vector<int> dp(s.length()+1,-1);\n st.erase(s);\n if(solve(s,st,0,dp)){\n ans.push_back(s);\n }\n st.insert(s);\n \n }\n\n return ans ;\n\n }\n};",
"memory": "63111"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n\n // normal word break - 1\n // this helper will run for average O(N^2)\n // iterative\n bool wordBreak(string s, unordered_set<string> &st,\n vector<int> &dp) {\n int n = s.size();\n dp[n]=1;\n for(int i=n-1;i>=0;i--){\n string temp;\n for(int j=i;j<n;j++){\n temp.push_back(s[j]);\n if(st.find(temp)!=st.end() && dp[j+1]){\n dp[i]=true;\n break;\n }\n }\n }\n return dp[0];\n }\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> st;\n for(auto x : words){\n st.insert(x);\n }\n vector<string>ans;\n for(auto x : words){\n // now problem has become similar to word break - I\n int n = x.size();\n vector<int> dp(n+1,0);\n // as elements are distinct and we are going to check for each word\n // set contains the same word. so remove it and check\n st.erase(x);\n if(wordBreak(x,st,dp)){\n ans.push_back(x);\n }\n // after checking put that word again\n st.insert(x);\n }\n return ans;\n }\n //total T.C. will be O(N^2)*M. M is length of words. that is words.length\n};",
"memory": "63111"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> dict(words.begin(), words.end());\n vector<string> ans;\n for(string word : words) {\n dict.erase(word);\n vector<int> dp(word.size(), -1);\n if(check(word, dict, dp, 0)) {\n ans.push_back(word);\n }\n\n dict.insert(word);\n }\n\n return ans;\n }\n\n bool check(string word, unordered_set<string> &dict, vector<int> &dp, int idx) {\n if(idx == word.size()) {\n return true;\n }\n\n if(dp[idx] != -1) return dp[idx];\n\n bool ans = false;\n\n for(int i=idx; i<word.size(); i++) {\n if(dict.find(word.substr(idx, i-idx+1)) != dict.end()) {\n ans = ans || check(word, dict, dp, i+1);\n }\n }\n\n return dp[idx] = ans;\n }\n};",
"memory": "66253"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<string,int>mp;\n bool solve(int ind, string str,vector<int>& dp){\n if(ind == str.size()) return true;\n if(dp[ind] != -1) return dp[ind];\n\n string temp = \"\";\n bool ans = false;\n\n for(int i = ind;i<str.size();i++){\n temp += str[i];\n if(mp.find(temp) != mp.end() && temp != str) ans |= solve(i+1,str,dp);\n }\n\n return dp[ind] = ans;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for(auto i:words) mp[i]++;\n vector<string>ans;\n for(auto i:words){\n vector<int>dp(i.size(),-1);\n if(solve(0,i,dp)) ans.push_back(i);\n }\n return ans;\n }\n};",
"memory": "66253"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n set<string> st;\n int n;\n int solve(int ind, string s, vector<int> &dp) {\n if(ind >= n)\n return 1;\n\n if(dp[ind] != -1)\n return dp[ind];\n\n string temp = \"\";\n for(int i=ind; i<n; i++) {\n temp += s[i];\n if(st.count(temp)) {\n if(solve(i+1, s, dp))\n return dp[ind] = 1;\n }\n }\n\n return dp[ind] = 0;\n }\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for(auto word : words)\n st.insert(word);\n \n vector<string> ans;\n for(auto word : words) {\n n = word.length();\n vector<int> dp(n, -1);\n st.erase(word);\n if(solve(0, word, dp))\n ans.push_back(word);\n st.insert(word);\n }\n\n return ans;\n }\n};",
"memory": "69396"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n bool checkInList(string &s, int i, unordered_map<string, int> &mp, vector<int> &dp){\n if(i>=s.length()){\n return true;\n }\n if(dp[i]!=-1) return dp[i];\n string str=\"\";\n for(int j=i ; j<s.length() ; j++){\n str+=s[j];\n if(mp.find(str)!=mp.end()){\n if(checkInList(s, j+1, mp, dp)==true){\n return dp[i]=true;\n }\n }\n }\n return dp[i]=false;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n unordered_map<string, int> mp;\n for(int i=0 ; i<words.size() ; i++) mp[words[i]]++;\n for(int i=0 ; i<words.size() ; i++){\n vector<int> dp(words[i].length()+2, -1);\n mp.erase(words[i]);\n if(checkInList(words[i], 0, mp, dp)==true) ans.push_back(words[i]);\n mp[words[i]]++;\n }\n return ans;\n }\n};",
"memory": "69396"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<string, bool> mp;\n\n bool find(string s, unordered_set<string>& st) {\n\n if (mp.count(s))\n return mp[s];\n\n string current = \"\";\n\n for (int i = 0; i < s.size(); i++) {\n current += s[i];\n\n if (st.find(current) != st.end()) {\n string remaining = s.substr(i + 1);\n\n if (st.find(remaining) != st.end() || find(remaining, st)) {\n return mp[s] = true;\n }\n }\n }\n\n return mp[s] = false;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n unordered_set<string> st(words.begin(), words.end());\n mp.clear();\n\n for (string& s : words) {\n st.erase(s);\n if (find(s, st)) {\n ans.push_back(s);\n }\n st.insert(s);\n }\n\n return ans;\n }\n};\n",
"memory": "72538"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<string, bool> memo;\n\n bool helper(string& s, unordered_set<string>& st, int start) {\n if (start >= s.size()) return false;\n if (memo.find(s.substr(start)) != memo.end()) return memo[s.substr(start)];\n\n string current = \"\";\n for (int i = start; i < s.size(); ++i) {\n current += s[i];\n if (st.count(current)) {\n if (i + 1 < s.size() && st.count(s.substr(i + 1))) {\n memo[s.substr(start)] = true;\n return true;\n }\n if (helper(s, st, i + 1)) {\n memo[s.substr(start)] = true;\n return true;\n }\n }\n }\n\n memo[s.substr(start)] = false;\n return false;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> st(words.begin(), words.end());\n vector<string> ans;\n\n for (string& word : words) {\n st.erase(word);\n if (helper(word, st, 0)) {\n ans.push_back(word);\n }\n st.insert(word);\n }\n\n return ans;\n }\n};\n",
"memory": "72538"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n map<pair<string,int>,bool> dp;\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n unordered_set<string> s(words.begin(),words.end());\n\n for(auto &w : words){\n string temp = w;\n s.erase(w);\n if(help(s,w,0))\n ans.push_back(w);\n s.insert(temp);\n } \n\n return ans;\n }\n\n bool help(unordered_set<string> &s,string &w,int idx){\n if(idx == w.size()) return true;\n\n if(dp.find({w,idx}) != dp.end()) return dp[{w,idx}];\n for(int i=idx;i<w.size();i++){\n string curr = w.substr(idx,i-idx+1);\n if(s.find(curr) != s.end()){\n if(help(s,w,i+1))\n return dp[{w,idx}] = true;\n }\n }\n\n return dp[{w,idx}] = false;\n }\n\n // bool wordBreak(string s, vector<string>& wordDict) {\n // int n = s.size();\n // vector<bool> dp(n+1,false);\n // dp[n] = true;\n \n // for(int i=n-1;i>=0;i--){\n // for(auto w: wordDict){\n // if(w == s) continue;\n // if((i+w.size()-1 < s.size()) && s.substr(i,w.size()) == w){\n // dp[i] = dp[i+w.size()];\n // }\n // if(dp[i] == true)\n // break;\n // }\n // }\n // return dp[0];\n // }\n};",
"memory": "75681"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n map<pair<string,int>,bool> dp;\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n unordered_set<string> s(words.begin(),words.end());\n\n for(auto &w : words){\n string temp = w;\n s.erase(w);\n if(help(s,w,0))\n ans.push_back(w);\n s.insert(temp);\n } \n\n return ans;\n }\n\n bool help(unordered_set<string> &s,string &w,int idx){\n if(idx == w.size()) return true;\n\n if(dp.find({w,idx}) != dp.end()) return dp[{w,idx}];\n for(int i=idx;i<w.size();i++){\n string curr = w.substr(idx,i-idx+1);\n if(s.find(curr) != s.end()){\n if(help(s,w,i+1))\n return dp[{w,idx}] = true;\n }\n }\n\n return dp[{w,idx}] = false;\n }\n\n // bool wordBreak(string s, vector<string>& wordDict) {\n // int n = s.size();\n // vector<bool> dp(n+1,false);\n // dp[n] = true;\n \n // for(int i=n-1;i>=0;i--){\n // for(auto w: wordDict){\n // if(w == s) continue;\n // if((i+w.size()-1 < s.size()) && s.substr(i,w.size()) == w){\n // dp[i] = dp[i+w.size()];\n // }\n // if(dp[i] == true)\n // break;\n // }\n // }\n // return dp[0];\n // }\n};",
"memory": "75681"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <string>\n\nusing namespace std;\n\nclass Solution {\npublic:\n bool isConcatenated(string str, unordered_map<string, bool>& wordMap, unordered_map<string, bool>& memo) {\n if (memo.count(str)) return memo[str];\n if (str.empty()) return false;\n\n bool result = false;\n for (int i = 1; i < str.length(); ++i) {\n string prefix = str.substr(0, i);\n string suffix = str.substr(i);\n\n if (wordMap.count(prefix)) {\n if (wordMap.count(suffix) || isConcatenated(suffix, wordMap, memo)) {\n result = true;\n break;\n }\n }\n }\n\n memo[str] = result;\n return result;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_map<string, bool> wordMap;\n for (const string& word : words) {\n wordMap[word] = true;\n }\n\n vector<string> result;\n unordered_map<string, bool> memo;\n\n for (const string& word : words) {\n wordMap.erase(word);\n if (isConcatenated(word, wordMap, memo)) {\n result.push_back(word);\n }\n wordMap[word] = true;\n }\n\n return result;\n }\n};\n",
"memory": "78823"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> finalAns;\n\n bool check(string s, set<string>& st, map<string,bool>& dp)\n {\n if(s.size() == 0)return true;\n if(dp.find(s) != dp.end())return dp[s];\n\n bool ans = false;\n for(int i = 0; i < s.size(); i++)\n {\n string temp = s.substr(0,i+1);\n if(st.count(temp))\n {\n bool val = check(s.substr(i+1),st,dp);\n ans = ans || val;\n }\n }\n\n return dp[s] = ans;\n }\n\n void solve(set<string>& st, vector<string>& words)\n {\n int n = words.size();\n for(int i = 0; i < n; i++)\n {\n string s = words[i];\n st.erase(s);\n map<string,bool> dp;\n\n if(check(s,st,dp))\n {\n finalAns.push_back(s);\n }\n\n st.insert(s);\n }\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n set<string> st(words.begin(),words.end());\n solve(st,words);\n return finalAns;\n }\n};",
"memory": "78823"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool helper(string s,int i,map<string,int>&h,map<string,int>&ans,map<string,int>&cuts){\n if(ans.count(s.substr(i))){\n return ans[s.substr(i)];\n }\n for(int j=i;j<s.size();j++){\n string s1=s.substr(i,j-i+1) ;\n if(h.count(s1)==0){\n continue;\n }\n if(j==s.size()-1){\n return ans[s.substr(i)]=1;\n }\n string s2=s.substr(j+1);\n bool c=helper(s,j+1,h,ans,cuts);\n if(c){\n cuts[s.substr(i)]=cuts[s2]+1;\n return ans[s.substr(i)]=1;\n }\n }\n return ans[s.substr(i)]=0;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n=words.size();\n map<string,int>h;\n for(int i=0;i<n;i++){\n h[words[i]]=1;\n }\n vector<string>v;\n // return v;\n map<string,int>ans;\n map<string,int>cuts;\n for(int i=0;i<n;i++){\n string s=words[i];\n bool c=helper(s,0,h,ans,cuts);\n if(c&&cuts[s]>0){\n v.push_back(s);\n }\n cout<<s<<\" \"<<cuts[s]<<endl;\n }\n return v;\n }\n \n\n\n};",
"memory": "81966"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool helper(string s,int i,map<string,int>&h,map<string,int>&ans,map<string,int>&cuts){\n if(ans.count(s.substr(i))){\n return ans[s.substr(i)];\n }\n for(int j=i;j<s.size();j++){\n string s1=s.substr(i,j-i+1) ;\n if(h.count(s1)==0){\n continue;\n }\n if(j==s.size()-1){\n return ans[s.substr(i)]=1;\n }\n string s2=s.substr(j+1);\n bool c=helper(s,j+1,h,ans,cuts);\n if(c){\n cuts[s.substr(i)]=cuts[s2]+1;\n return ans[s.substr(i)]=1;\n }\n }\n return ans[s.substr(i)]=0;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n=words.size();\n map<string,int>h;\n for(int i=0;i<n;i++){\n h[words[i]]=1;\n }\n vector<string>v;\n // return v;\n map<string,int>ans;\n map<string,int>cuts;\n for(int i=0;i<n;i++){\n string s=words[i];\n bool c=helper(s,0,h,ans,cuts);\n if(c&&cuts[s]>0){\n v.push_back(s);\n }\n cout<<s<<\" \"<<cuts[s]<<endl;\n }\n return v;\n }\n \n\n\n};",
"memory": "81966"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool check(string a, int idx, set<string> &st, string ignore, unordered_map<string, bool> &memo) {\n if (idx == a.size()) {\n return true;\n }\n \n string key = a.substr(idx);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n\n string temp = \"\";\n for (int k = idx; k < a.size(); k++) {\n temp.push_back(a[k]);\n if (temp != ignore && st.find(temp) != st.end()) {\n if (check(a, k + 1, st, ignore, memo)) {\n return memo[key] = true;\n }\n }\n }\n return memo[key] = false;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n = words.size();\n set<string> st(words.begin(), words.end());\n vector<string> ans;\n\n for (int i = 0; i < n; i++) {\n unordered_map<string, bool> memo;\n if (check(words[i], 0, st, words[i], memo)) {\n ans.push_back(words[i]);\n }\n }\n return ans;\n }\n};\n",
"memory": "85108"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n struct TrieNode {\n char c = '\\0';\n bool isEnd = false;\n };\n vector<TrieNode> nodes;\n vector<vector<size_t>> edges;\n\n nodes.push_back(TrieNode{});\n edges.emplace_back();\n const size_t rootNode = 0;\n\n // build a trie\n for (const string& word : words) {\n size_t curr = rootNode;\n for (const char c : word) {\n // try to transition\n size_t next = curr;\n for (const size_t dst : edges[curr]) {\n if (nodes[dst].c == c) {\n next = dst;\n break;\n }\n }\n\n // if the transition failed, create the node\n if (next == curr) {\n next = nodes.size();\n nodes.push_back(TrieNode{.c = c});\n edges[curr].push_back(next);\n edges.emplace_back();\n }\n\n curr = next;\n }\n\n // mark the end of the word\n nodes[curr].isEnd = true;\n }\n\n unordered_set<string_view> result;\n unordered_map<string_view, bool> cache;\n const auto dfs = [&](const string_view word, size_t prev, size_t depth,\n const auto& recFun) {\n if (word.empty()) {\n return true;\n }\n\n for (size_t pos = 0; pos < word.length(); ++pos) {\n // try to find the corresponding node\n // cout << \"At \" << nodes[prev].c << endl;\n //cout << \"Looking for \" << word[pos] << endl;\n\n size_t curr = prev;\n for (const size_t dst : edges[curr]) {\n if (nodes[dst].c == word[pos]) {\n curr = dst;\n break;\n }\n }\n\n if (curr == prev) {\n //cout << \"No way\" << endl;\n return false;\n }\n\n // Try to split at the current position\n if (nodes[curr].isEnd) {\n //cout << \"Trying to split \" << endl;\n const auto suffix = string_view(word).substr(pos + 1);\n bool isConcat = false;\n if (const auto hit = cache.find(suffix); hit != cache.end()) {\n isConcat = hit->second;\n } else {\n isConcat = cache[suffix] = recFun(suffix, rootNode, depth + 1, recFun);\n }\n if (isConcat) {\n //cout << \"Split success: \" << suffix << endl;\n if (depth == 0 && !suffix.empty()) {\n //cout << \"Add \" << word << endl;\n result.insert(word);\n }\n return true;\n }\n }\n\n //cout << \"Split fail \" << endl;\n prev = curr;\n }\n\n return false;\n };\n\n for (const string& word : words) {\n dfs(word, rootNode, 0, dfs);\n }\n\n return vector<string>(result.begin(), result.end());\n }\n};",
"memory": "97678"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n struct TrieNode {\n char c = '\\0';\n bool isEnd = false;\n };\n vector<TrieNode> nodes;\n vector<vector<size_t>> edges;\n\n nodes.push_back(TrieNode{});\n edges.emplace_back();\n const size_t rootNode = 0;\n\n // build a trie\n for (const string& word : words) {\n size_t curr = rootNode;\n for (const char c : word) {\n // try to transition\n size_t next = curr;\n for (const size_t dst : edges[curr]) {\n if (nodes[dst].c == c) {\n next = dst;\n break;\n }\n }\n\n // if the transition failed, create the node\n if (next == curr) {\n next = nodes.size();\n nodes.push_back(TrieNode{.c = c});\n edges[curr].push_back(next);\n edges.emplace_back();\n }\n\n curr = next;\n }\n\n // mark the end of the word\n nodes[curr].isEnd = true;\n }\n\n unordered_map<string_view, bool> cache;\n const auto dfs = [&](const string_view word, size_t prev,\n const auto& dfs) -> size_t {\n if (word.empty()) {\n return 1;\n }\n\n for (size_t pos = 0; pos < word.length(); ++pos) {\n // try to find the corresponding node\n size_t curr = prev;\n for (const size_t dst : edges[curr]) {\n if (nodes[dst].c == word[pos]) {\n curr = dst;\n break;\n }\n }\n\n if (curr == prev) {\n return 0;\n }\n\n // Try to split at the current position\n if (nodes[curr].isEnd) {\n const auto suffix = string_view(word).substr(pos + 1);\n size_t numConcat = 0;\n if (const auto hit = cache.find(suffix); hit != cache.end()) {\n numConcat = hit->second;\n } else {\n numConcat = cache[suffix] = dfs(suffix, rootNode, dfs);\n }\n if (numConcat) {\n return numConcat + !suffix.empty();\n }\n }\n\n prev = curr;\n }\n\n return 0;\n };\n\n vector<string> result;\n for (const string& word : words) {\n if (dfs(word, rootNode, dfs) > 1) {\n result.push_back(word);\n }\n }\n return result;\n }\n};",
"memory": "97678"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[31][3];\n bool fn (string& s, map<string, int>& ht, int i, int cnt) {\n if (i == s.size()) {\n if (cnt == 2) return dp[i][cnt] = true;\n return dp[i][cnt] = false;\n }\n if (dp[i][cnt] != -1) return dp[i][cnt];\n\n string x;\n for (int j=i ; j<s.size() ; j++) {\n x += s[j];\n if (ht[x] > 0) {\n if (cnt >= 1) {\n if (fn (s, ht, j+1, 2)) return dp[i][cnt] = true;\n }\n else {\n if (fn (s, ht, j+1, cnt+1)) return dp[i][cnt] = true;\n }\n }\n }\n\n return dp[i][cnt] = false;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n map<string, int> ht;\n for (auto &it:words) {\n ht[it]++;\n }\n\n vector<string> ans;\n for (int i=0 ; i<words.size() ; i++) {\n memset(dp, -1, sizeof(dp));\n if (fn (words[i], ht, 0, 0)) ans.push_back(words[i]);\n }\n\n return ans;\n }\n};",
"memory": "100821"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nvector<string> ans;\nint dp[31][31];\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n map<string,int> mp;\n for(auto it:words){\n mp[it]++;\n }\n for(int i=0;i<words.size();i++){\n if(!words[i].empty()){\n memset(dp,-1,sizeof(dp));\n if(recurse(words[i],0,0,mp)){\n ans.push_back(words[i]);\n }\n }\n }\n return ans;\n }\n bool recurse(string &word,int start,int count,map<string,int>& mp){\n if(start==word.size()){\n return count>=2;\n }\n if(dp[start][count]!=-1){\n return dp[start][count];\n }\n string curr=\"\";\n for(int j=start;j<word.size();j++){\n curr+=word[j];\n if(mp[curr]>0 && curr!=word){\n if(recurse(word,j+1,count+1,mp)){\n return dp[start][count]=true;\n }\n }\n }\n return dp[start][count]=false;\n }\n};",
"memory": "103963"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<string, bool> mp;\n vector<string> final_ans;\n int dp[10010]; \n\n bool back(int pos, string& s){\n if(pos == s.length()) return true;\n if(dp[pos] != -1) return dp[pos];\n \n string currentWord = \"\";\n for(int i = pos; i < s.length(); ++i){\n currentWord += s[i];\n if(mp[currentWord] && currentWord != s && back(i + 1, s)){\n return dp[pos] = 1;\n }\n }\n return dp[pos] = 0;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for(auto& word : words) mp[word] = true;\n \n for(auto& word : words){\n memset(dp, -1, sizeof(dp)); \n if(back(0, word)){\n final_ans.push_back(word);\n }\n }\n return final_ans;\n }\n};\n\n\n// class Solution {\n// struct TrieNode {\n// TrieNode* arr[26];\n// bool is_end;\n// TrieNode() {\n// for (int i = 0; i < 26; i ++) arr[i] = NULL;\n// is_end = false;\n// } \n// };\n\n// void insert(TrieNode* root, string key) {\n// TrieNode* curr = root;\n// for (int i = 0; i < key.size(); i ++) {\n// int idx = key[i] - 'a';\n// if (curr->arr[idx] == NULL)\n// curr->arr[idx] = new TrieNode();\n// curr = curr->arr[idx];\n// }\n// curr->is_end = true;\n// }\n\n// bool dfs(TrieNode* root, string key, int index, int count) {\n// if (index >= key.size())\n// return count > 1;\n// TrieNode* curr = root;\n// for (int i = index; i < key.size(); i ++) {\n// int p = key[i] - 'a';\n// if (curr->arr[p] == NULL) {\n// return false;\n// }\n// curr = curr->arr[p];\n// if (curr->is_end) {\n// if (dfs(root, key, i+1, count+1))\n// return true;\n// }\n// }\n// return false;\n// }\n// public:\n// vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n// TrieNode* root = new TrieNode();\n// for (int i = 0; i < words.size(); i ++) {\n// insert(root, words[i]);\n// }\n// vector<string> ans;\n// for (int i = 0; i < words.size(); i ++) {\n// if (dfs(root, words[i], 0, 0))\n// ans.push_back(words[i]);\n// }\n// return ans; \n// }\n// };",
"memory": "107106"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n static int compare(string& a, string& b) {\n return a.size() < b.size();\n }\n bool solve(string s, map<string,bool>& dict) {\n if (dict.find(s) != dict.end())\n {\n if (dict[s])\n return true;\n else\n return false;\n }\n \n for (int i=0;i<s.size()-1;i++)\n {\n string left = s.substr(0,i+1);\n string right = s.substr(i+1,s.size()-(i+1));\n if (solve(left, dict) && solve(right, dict))\n {\n dict[s] = true;\n return true;\n }\n }\n dict[s] = false;\n return false;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n sort(words.begin(),words.end(),compare);\n map<string,bool> dict;\n vector<string> res;\n for (int i=0;i<words.size();i++)\n {\n if (solve(words[i], dict))\n res.push_back(words[i]);\n dict[words[i]] = true;\n }\n return res;\n }\n};",
"memory": "110248"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int dp[32];\n\n int f(int idx,string&s,unordered_map<string,int>&m){\n if(idx==s.length())return 1;\n if(dp[idx]!=-1)return dp[idx];\n string temp=\"\";\n int ans=0;\n for(int i=idx;i<s.length();i++){\n temp.push_back(s[i]);\n if(m[temp]){\n ans+=f(i+1,s,m);\n }\n }\n return dp[idx]= ans;\n }\n\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& v) {\n unordered_map<string,int>m;\n for(auto &c:v){\n m[c]++;\n }\n vector<string>re;\n for(auto &s:v){\n memset(dp,-1,sizeof(dp));\n int ans=f(0,s,m);\n if(ans>1){\n re.push_back(s);\n }\n\n }\n return re;\n\n }\n};",
"memory": "113391"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<string,bool> mp ;\n int dp[35] ;\n\n bool help(int i, string &word ){\n int n = word.size() ; \n if(i==n) return true;\n\n if(dp[i] != -1) return dp[i] ;\n\n bool is = false ;\n string s = \"\" ;\n for(int j = i ; j < n ; j++){\n s += word[j] ;\n if(mp[s]) is = is || help(j+1, word) ;\n } \n return dp[i] = is ;\n }\n bool check(string &word ) {\n memset(dp,-1,sizeof(dp)) ;\n return help(0,word) ;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n mp.clear() ;\n for(auto str: words) mp[str] = true ;\n\n vector<string> ans ;\n for(auto &word: words){\n mp[word] = false ;\n if(check(word)) ans.push_back(word) ;\n mp[word] = true ;\n }\n return ans ;\n }\n};",
"memory": "116533"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nunordered_map<string,int>mp;\nint dp[31];\n bool conc(string &word,int i)\n {\n int n=word.size();\n if(i==n)\n return true;\n if(dp[i]!=-1)\n return dp[i];\n bool a=false;\n for(int j=1;j<=min(n-1,n-i);j++)//j is length of string from i'th index\n {\n if(mp[word.substr(i,j)]>0)\n {\n a=a||conc(word,i+j);\n }\n }\n return dp[i]=a;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n sort(words.begin(),words.end());\n int n=words.size();\n for(int i=0;i<n;i++)\n {\n mp[words[i]]++;\n } \n vector<string>ans;\n for(int i=0;i<n;i++)\n {\n memset(dp,-1,sizeof(dp));\n if(conc(words[i],0))\n ans.push_back(words[i]);\n } \n return ans;\n }\n};",
"memory": "116533"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int help(int ind, string& s, map<string,int>& mp, vector<int>& dp) {\n if(ind == s.size()) {\n return 1;\n }\n if(dp[ind] != -1) {\n return dp[ind];\n }\n string tmp = \"\";\n for(int k=ind; k<s.size(); k++) {\n tmp += s[k];\n if(tmp.size() != s.size() && mp[tmp] && help(k+1, s, mp, dp)) {\n return dp[ind] = 1;\n }\n }\n return dp[ind] = 0;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n map<string, int> mp;\n for(auto s: words) {\n mp[s]++;\n } \n vector<string> ans;\n for(auto s: words) {\n vector<int> dp(s.size(), -1);\n if(help(0, s, mp, dp)) {\n ans.push_back(s);\n }\n }\n return ans;\n }\n};",
"memory": "119676"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "#include <unordered_map>\n\nclass Solution {\npublic:\n \n map<string, bool> memo;\n\n bool solve(string& target, string& temp, vector<vector<string>>& adj) {\n if (temp.length() == target.length()) {\n return temp == target;\n }\n \n if (temp.length() > target.length()) return false;\n \n char start = target[temp.length()];\n\n if (memo.find(temp) != memo.end()) return memo[temp];\n\n for (auto& it : adj[start - 'a']) {\n string new_temp = temp + it;\n if (new_temp.length() > target.length()) continue;\n\n if(it==target.substr(temp.length(),it.length())){\n if (solve(target, new_temp, adj)) {\n memo[temp] = true; \n return true;\n }\n }\n }\n\n memo[temp] = false; \n return false;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<vector<string>> adj(26);\n int n = words.size();\n\n for (int i = 0; i < n; i++) {\n adj[words[i][0] - 'a'].push_back(words[i]);\n }\n\n vector<string> ans;\n\n for (int i = 0; i < n; i++) {\n memo.clear(); \n bool flag = false;\n\n for (auto& it : adj[words[i][0] - 'a']) {\n if (it != words[i] && it==words[i].substr(0,it.length())) {\n string temp = it;\n if (solve(words[i], temp, adj)) {\n flag = true;\n break;\n }\n }\n }\n\n if (flag) ans.push_back(words[i]);\n }\n\n return ans;\n }\n};\n",
"memory": "119676"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool check(string &s,int idx,vector<int>&dp,unordered_map<string,int>&mp){\n if(idx>=s.size())return 1;\n if(dp[idx]!=-1)return dp[idx];\n string temp=\"\";\n bool ans=0;\n for(int j=idx;j<s.size();j++){\n if(idx==0 and j==s.size()-1)continue;\n temp+=s[j];\n if(mp[temp]){\n ans|=check(s,j+1,dp,mp);\n }\n if(ans)return dp[idx]=ans;\n }\n return dp[idx]=ans;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_map<string,int>idx;\n vector<string>ans;\n for(int i=0;i<words.size();i++){\n idx[words[i]]=i+1;\n }\n for(int i=0;i<words.size();i++){\n vector<int>dp(words[i].size(),-1);\n if(check(words[i],0,dp,idx))ans.push_back(words[i]);\n }\n return ans;\n }\n};",
"memory": "122818"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n #define pb push_back\n int dp[35][35][4];\n bool func(int index,int last, int cnt , string temp, string& s1, map<string,int>& mp){\n if(index == s1.size()){\n if(temp.size()==0 && cnt==2) return true;\n else return false;\n }\n if( dp[index][last+2][cnt] != -1) return dp[index][last+2][cnt];\n \n bool ans1=false,ans2=false; \n temp.pb(s1[index]); string temp2;\n ans1=func(index+1,last,cnt,temp,s1,mp);\n if( mp[temp]==1){\n int d=0;\n if( cnt<2) cnt++,d++;\n ans2=func(index+1,index,cnt,temp2,s1,mp);\n if( d != 0) cnt--;\n } \n return dp[index][last+2][cnt]= ans1|ans2;\n }\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& v1) {\n map<string,int> mp; vector<string> res;\n for(int i=0; i<v1.size(); i++) mp[v1[i]]=1;\n for(int i=0; i<v1.size(); i++){\n string s1=v1[i]; string temp;\n memset(dp,-1,sizeof(dp));\n if( func(0,0,0,temp,s1,mp)) res.push_back(s1); \n }\n return res;\n }\n};",
"memory": "122818"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n for(auto& word: words)\n {\n reverse(word.begin(), word.end());\n }\n sort(words.begin(), words.end(), [&](string& lhs, string& rhs) -> bool {\n return lhs.length() < rhs.length();\n });\n unordered_set<char> visChar;\n vector<unordered_set<string>> vis(10001);\n function<bool(string, bool)> isConcated = [&](string word, bool isFirst) -> bool {\n if (isFirst && word.length() <= 1)\n {\n return false;\n }\n if (vis[word.length()].find(word) != vis[word.length()].end())\n {\n return true;\n }\n for(int preLen = 1; preLen < word.length(); preLen++)\n {\n if (vis[preLen].find(word.substr(0, preLen)) != vis[preLen].end())\n {\n if (isConcated(word.substr(preLen), false))\n {\n vis[word.substr(preLen).length()].insert(word.substr(preLen));\n return true;\n }\n }\n }\n return false;\n };\n for(string& word: words)\n {\n if (word.length() == 1)\n {\n vis[1].insert(word);\n visChar.insert(word[0]);\n continue;\n }\n {\n bool ctn = false;\n for(char& ch: word)\n {\n if (visChar.find(ch) == visChar.end())\n {\n vis[word.length()].insert(word);\n ctn = true;\n break;\n }\n }\n for(char& ch: word)\n {\n visChar.insert(ch);\n }\n if (ctn)\n {\n continue;\n }\n }\n {\n unordered_set<char> prev;\n for(char ch: word)\n {\n prev.insert(ch);\n }\n if (prev.size() == 1 && vis[1].find(word.substr(0, 1)) != vis[1].end())\n {\n ans.push_back(word);\n for(int i = 2; i <= word.length(); i++)\n {\n vis[i].insert(word.substr(0, i));\n }\n continue;\n }\n }\n if (isConcated(word, true)) {\n ans.push_back(word);\n }\n vis[word.length()].insert(word);\n }\n for(auto& word: ans)\n {\n reverse(word.begin(), word.end());\n }\n return ans;\n }\n};",
"memory": "125961"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\npublic:\n bool dfs(const string& word, int length, vector<bool>& visited, const unordered_set<string>& dictionary) {\n if(length == word.size()) {\n return true;\n }\n if(visited[length]) {\n return false;\n }\n visited[length] = true;\n for(int i = word.length() - (length == 0 ? 1 : 0); i > length; --i) {\n if(dictionary.count(word.substr(length, i - length)) && dfs(word, i, visited, dictionary)) {\n return true;\n }\n }\n return false;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> dictionary(words.begin(), words.end());\n vector<string> answer;\n for(const string& word: words) {\n vector<bool> visited(words.size());\n if(dfs(word, 0, visited, dictionary)) {\n answer.push_back(word);\n }\n }\n return answer;\n }\n};",
"memory": "129103"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool backtracking(const string& word, unordered_set<string>& words_set, vector<bool>& visited, int index) {\n if (index == word.size()) return true;\n visited[index] = true;\n for (int i = word.size() - (index == 0 ? 1 : 0); i >= index; --i) {\n if (words_set.count(word.substr(index, i - index)) && !visited[i]) {\n if (backtracking(word, words_set, visited, i)) {\n return true;\n }\n }\n }\n return false;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> words_set(words.begin(), words.end());\n vector<string> ans;\n for (const auto& w: words) {\n vector<bool> visited(words.size(), false);\n if (backtracking(w, words_set, visited, 0)) {\n ans.push_back(w);\n }\n }\n return ans;\n }\n};",
"memory": "129103"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic: \n unordered_map<string,bool> m;\n bool dpf(string & w, vector<string>& words, unordered_map<string,int>&mp){\n if(m.count(w)) return m[w];\n int n=w.size();\n for(int i=1;i<n;i++){\n string p=w.substr(0,i);\n string s=w.substr(i);\n if(mp[p] && (mp[s] || dpf(s,words,mp))) return m[w]=1;\n }\n return m[w]=0;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> re;\n unordered_map<string,int> mp;\n for(auto k:words) mp[k]++;\n\n for(auto &w:words){\n if(dpf(w,words,mp)) re.push_back(w);\n }\n return re;\n }\n};",
"memory": "132246"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<string, bool>um;\n unordered_map<string, bool>dp;\n bool helper(string &s) {\n if(s.length() == 0) return true;\n if(dp.count(s)) return dp[s];\n for(int i=1; i<s.length(); i++) {\n string p1 = s.substr(0, i);\n string p2 = s.substr(i);\n \n if(um[p1]) {\n if(um[p2]) return dp[s] = true;\n if(helper(p2)) return dp[s] = true;\n }\n }\n\n return dp[s] = false;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string>ans;\n for(auto i: words) um[i] = true;\n for(auto i: words) {\n if(helper(i)) {\n ans.push_back(i);\n }\n }\n return ans;\n }\n};",
"memory": "132246"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<string,int> mp;\n vector<vector<int>> dp;\n string s; \n int rec(int l,int r){\n if(dp[l][r] != -1 ) return dp[l][r];\n \n string temp = s.substr(l,r-l+1);\n \n if(mp.find(temp)!= mp.end() and mp[temp] == 1){\n \n dp[l][r] = 1;\n \n return dp[l][r];\n }\n if(l==r) return dp[l][r] = 0;\n int ans = 0;\n for(int mid=l; mid<r; mid++){\n ans |= (rec(l,mid)&rec(mid+1,r));\n }\n return dp[l][r] = ans; \n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) { \n for(auto v:words){\n mp[v] = 1;\n }\n vector<string> ans;\n for(auto v:words){\n int n = v.length();\n dp.clear();\n dp.assign(n,vector<int>(n,-1));\n mp[v] = 0;\n s = v;\n if(rec(0,n-1)){\n ans.push_back(v);\n }\n mp[v] = 1;\n }\n return ans;\n }\n};",
"memory": "135388"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> words_set;\n for (string word : words) words_set.insert(word);\n vector<string> res;\n \n for (string word : words) {\n int n = word.size();\n vector<bool> dp(n + 1, 0);\n dp[0] = 1;\n queue<int> q;\n q.push(0);\n while (!q.empty()) {\n // if(k==2) cout<<1<<endl;\n if(dp[n]==1) break;\n int ind = q.front();\n q.pop();\n for (int j = ind + 1; j <= n&&(j-ind<n); ++j) {\n if(dp[j]==1) continue;\n if (words_set.count(word.substr(ind, j - ind))) {\n dp[j] = 1;\n q.push(j);\n }\n }\n }\n if(dp[n]==1){\n res.push_back(word);\n }\n }\n return res;\n }\n};\n",
"memory": "135388"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool func(int i,string& s,map<string,int>& mp,vector<int>& dp){\n int n = s.size();\n if(i>=n)return 0;\n if(i>0 && mp[s.substr(i)])return 1;\n if(dp[i]!=-1)return dp[i];\n bool ans = 0;\n for(int j=1;i+j<n;j++){\n if(mp[s.substr(i,j)] && func(i+j,s,mp,dp)){\n ans = 1;\n }\n }\n return dp[i]=ans;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n = words.size();\n \n vector<string> ans;\n map<string,int> mp;\n for(auto it : words)mp[it]=1;\n for(int i=0;i<n;i++){\n vector<int> dp(30,-1);\n if(func(0,words[i],mp,dp)){\n ans.push_back(words[i]);\n }\n }\n return ans;\n }\n};",
"memory": "138531"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n map<pair<string,int>,bool>mpp2;\n unordered_map<string,int>mpp;\n bool solve(string s, int i)\n {\n if(i>=s.length())return true;\n if(mpp2.find({s,i})!=mpp2.end())return mpp2[{s,i}];\n for(int j=i+1;j<s.length()+1;++j)\n {\n string s2=s.substr(i,j-i);\n if(mpp[s2]>0)\n {\n if(solve(s,j))\n return mpp2[{s,i}]= true;\n }\n }\n return mpp2[{s,i}]= false;\n \n }\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for(int i=0;i<words.size();++i)\n {\n mpp[words[i]]++;\n }\n int n=words.size();\n vector<string>ans;\n for(int i=0;i<n;++i)\n {\n mpp[words[i]]--;\n if(solve(words[i],0))\n {\n ans.push_back(words[i]);\n }\n mpp[words[i]]++;\n }\n return ans;\n }\n};",
"memory": "138531"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int rec(int i,int j,string s,string temp,unordered_map<string,int> &mp,vector<vector<int>> &dp)\n {\n int n=s.size();\n temp+=s[i];\n\n if(dp[i][j]!=-1)\n return dp[i][j];\n\n if(i==n-1)\n {\n if(mp[temp])\n return dp[i][j]=1;\n else\n return dp[i][j]=INT_MIN;\n }\n if(mp[temp])\n {\n int c1=1+rec(i+1,i+1,s,\"\",mp,dp);\n int c2=rec(i+1,j,s,temp,mp,dp);\n return dp[i][j]=max(c1,c2);\n }\n else\n {\n return dp[i][j]=rec(i+1,j,s,temp,mp,dp);\n }\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n=words.size();\n unordered_map<string,int> mp;\n for(int i=0;i<n;i++)\n {\n mp[words[i]]++;\n }\n vector<string> ans;\n vector<vector<int>> dp(40,vector<int>(40,-1));\n for(int i=0;i<n;i++)\n {\n int temp=rec(0,0,words[i],\"\",mp,dp);\n if(temp>=2)\n ans.push_back(words[i]);\n for(int i=0;i<40;i++)\n {\n for(int j=0;j<40;j++)\n dp[i][j]=-1;\n }\n }\n return ans;\n }\n};",
"memory": "141673"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int rec(int i,int j,string s,string temp,unordered_map<string,int> &mp,vector<vector<int>> &dp)\n {\n int n=s.size();\n temp+=s[i];\n\n if(dp[i][j]!=-1)\n return dp[i][j];\n\n if(i==n-1)\n {\n if(mp[temp])\n return dp[i][j]=1;\n else\n return dp[i][j]=INT_MIN;\n }\n if(mp[temp])\n {\n int c1=1+rec(i+1,i+1,s,\"\",mp,dp);\n int c2=rec(i+1,j,s,temp,mp,dp);\n return dp[i][j]=max(c1,c2);\n }\n else\n {\n return dp[i][j]=rec(i+1,j,s,temp,mp,dp);\n }\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n=words.size();\n unordered_map<string,int> mp;\n for(int i=0;i<n;i++)\n {\n mp[words[i]]++;\n }\n vector<string> ans;\n vector<vector<int>> dp(40,vector<int>(40,-1));\n for(int i=0;i<n;i++)\n {\n int temp=rec(0,0,words[i],\"\",mp,dp);\n if(temp>=2)\n ans.push_back(words[i]);\n for(int i=0;i<40;i++)\n {\n for(int j=0;j<40;j++)\n dp[i][j]=-1;\n }\n }\n return ans;\n }\n};",
"memory": "141673"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> dictionary(words.begin(), words.end());\n vector<string> answer;\n for (const string& word : words) {\n const int length = word.length();\n vector<vector<bool>> dp(length, vector<bool>(length, false));\n for (int sub_len = 1; sub_len <= length; ++sub_len) {\n for (int i=0; i<= length - sub_len; i++) {\n int j = i+sub_len-1;\n dp[i][j]=false;\n if(sub_len < length && dictionary.find(word.substr(i, sub_len)) != dictionary.end()){\n dp[i][j]=true;\n continue;\n }\n\n for(int k=i; k<j ;k++){\n dp[i][j] =dp[i][j] || dp[i][k] && dp[k+1][j];\n }\n }\n }\n if (dp[0][length-1]) {\n answer.push_back(word);\n }\n }\n return answer;\n }\n};",
"memory": "144816"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<string,int> mp;\n\n bool solve(int ind, string &s, int n, vector<int> &dp){\n if(ind == n) return true;\n\n if(dp[ind] != -1) return dp[ind];\n\n string temp = \"\";\n\n for(int k=ind;k<n;k++){\n temp += s[k];\n\n if(mp[temp]){\n if(solve(k+1 , s , n , dp)) return dp[ind] = true;\n }\n }\n\n return dp[ind] = false;\n } \n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for(auto x : words){\n mp[x]++;\n }\n\n vector<string> ans;\n\n for(auto x : words){\n vector<int> dp(31 , -1);\n mp.erase(x);\n if(solve(0 , x , x.size() , dp)){\n ans.push_back(x);\n }\n mp[x]++;\n }\n\n return ans;\n }\n};",
"memory": "147958"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class TrieNode {\npublic:\n vector<TrieNode*> children;\n int path = 0;\n int end = 0;\n\n TrieNode(){\n children.assign(26,nullptr);\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n return byBfs(words);\n // return byBottomUp2(words);\n // return byBottomUp1(words);\n // return byTrieNode(words);\n }\n\n vector<string> byTrieNode(vector<string>& words) {\n auto root = new TrieNode();\n\n for(auto w : words){\n auto node = root;\n for(auto c : w){\n c -= 'a';\n if (node->children[c]==nullptr){\n node->children[c] = new TrieNode();\n }\n node = node->children[c];\n node->path++;\n }\n node->end++;\n }\n\n auto check = [&](auto&& check, string s)->bool{\n int n = s.size();\n\n vector<int> dp(n);\n\n for(int i=0; i<n; ++i){\n if (i==0 || dp[i-1]){\n auto node = root;\n\n for(int j=i; j<n; ++j){\n int c = s[j]-'a';\n if (node->children[c]==nullptr){\n break;\n }\n\n node = node->children[c];\n if (node->end>0 && !(i==0 && j==n-1)){ //排除自己的单词\n dp[j] = 1;\n }\n }\n }\n }\n\n return dp[n-1];\n };\n\n vector<string> ans;\n for(auto w : words){\n if (check(check, w)){\n ans.push_back(w);\n }\n }\n\n return ans;\n }\n\n vector<string> byBfs(vector<string>& words) {\n unordered_set<string> dic(words.begin(), words.end());\n \n auto bfs = [&](string& s){\n int n = s.size();\n vector<int> visited(n+1);\n dic.erase(s);\n\n queue<int> q;\n q.push(0);\n\n while(!q.empty()){\n int start = q.front();\n q.pop();\n\n if (start == n){\n return true;\n }\n\n for(int end=start+1; end<=n; ++end){\n if (!visited[end]){\n string str = s.substr(start,end-start);\n if (dic.count(str)){\n visited[end] = 1;\n q.push(end);\n }\n }\n }\n }\n\n dic.insert(s);\n\n return false;\n };\n\n vector<string> ans;\n for(auto w : words){\n if (bfs(w)){\n ans.push_back(w);\n }\n }\n\n return ans;\n }\n\n vector<string> byBottomUp1(vector<string>& words) {\n auto check = [&](int id){\n string s = words[id];\n int n = s.size();\n\n vector<int> dp(n);\n\n for(int i=0; i<n; ++i){\n for(int j=0; j<words.size(); ++j){\n if (j==id){ //排除自己\n continue;\n }\n\n auto& w = words[j]; \n int m = w.size();\n\n if (i<m-1){\n continue;\n }\n\n if (i==m-1||dp[i-m]){\n string str = s.substr(i-m+1,m);\n if (str == w){\n dp[i]= 1;\n break;\n }\n }\n }\n }\n\n return dp[n-1];\n };\n\n\n vector<string> ans;\n\n for(int i=0; i<words.size(); ++i){\n if (check(i)){\n ans.push_back(words[i]);\n }\n }\n\n return ans;\n }\n\n vector<string> byBottomUp2(vector<string>& words) {\n vector<string> ans;\n unordered_set<string> dic(words.begin(), words.end());\n\n auto check = [&](int i){\n string s = words[i];\n int n = s.size();\n\n vector<int> dp(n+1);\n dp[0] = 1;\n dic.erase(s);\n\n for(int i=1; i<=n; ++i){\n for(int j=0; j<i; ++j){\n if (dp[j] && dic.count(s.substr(j, i-1))){\n dp[i] = 1;\n break;\n }\n }\n }\n\n dic.insert(s);\n return dp[n];\n };\n\n for(int i=0; i<words.size(); ++i){\n if (check(i)){\n ans.push_back(words[i]);\n }\n }\n\n return ans;\n }\n};\n",
"memory": "151101"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class TrieNode {\npublic:\n vector<TrieNode*> children;\n int path = 0;\n int end = 0;\n\n TrieNode(){\n children.assign(26,nullptr);\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n return byBfs(words);\n }\n\n vector<string> byTrieNode(vector<string>& words) {\n auto root = new TrieNode();\n\n for(auto w : words){\n auto node = root;\n for(auto c : w){\n c -= 'a';\n if (node->children[c]==nullptr){\n node->children[c] = new TrieNode();\n }\n node = node->children[c];\n node->path++;\n }\n node->end++;\n }\n\n auto check = [&](auto&& check, string s)->bool{\n int n = s.size();\n\n vector<int> dp(n);\n\n for(int i=0; i<n; ++i){\n if (i==0 || dp[i-1]){\n auto node = root;\n\n for(int j=i; j<n; ++j){\n int c = s[j]-'a';\n if (node->children[c]==nullptr){\n break;\n }\n\n node = node->children[c];\n if (node->end>0 && !(i==0 && j==n-1)){ //排除自己的单词\n dp[j] = 1;\n }\n }\n }\n }\n\n return dp[n-1];\n };\n\n vector<string> ans;\n for(auto w : words){\n if (check(check, w)){\n ans.push_back(w);\n }\n }\n\n return ans;\n }\n\n vector<string> byBfs(vector<string>& words) {\n unordered_set<string> dic(words.begin(), words.end());\n \n auto bfs = [&](string& s){\n int n = s.size();\n vector<int> visited(n+1);\n dic.erase(s);\n\n queue<int> q;\n q.push(0);\n\n while(!q.empty()){\n int start = q.front();\n q.pop();\n\n if (start == n){\n return true;\n }\n\n for(int end=start+1; end<=n; ++end){\n if (!visited[end]){\n string str = s.substr(start,end-start);\n if (dic.count(str)){\n visited[end] = 1;\n q.push(end);\n }\n }\n }\n }\n\n dic.insert(s);\n\n return false;\n };\n\n vector<string> ans;\n for(auto w : words){\n if (bfs(w)){\n ans.push_back(w);\n }\n }\n\n return ans;\n }\n\n vector<string> byTopBottom(vector<string>& words) {\n vector<string> ans;\n\n return ans;\n }\n\n vector<string> byBottomUp1(vector<string>& words) {\n vector<string> ans;\n\n return ans;\n }\n\n vector<string> byBottomUp2(vector<string>& words) {\n vector<string> ans;\n\n return ans;\n }\n};\n",
"memory": "151101"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "struct Trie{\n map <char, Trie*> next;\n bool end = false;\n};\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie* root = new Trie;\n for(string word: words){\n Trie* curr = root;\n for(char ch: word){\n if(curr->next[ch]){\n curr = curr->next[ch];\n }\n else{\n curr->next[ch] = new Trie;\n curr = curr->next[ch];\n }\n }\n curr->end = true;\n }\n vector <string> ans;\n for(string word: words){\n vector <pair <bool, int>> dp(word.size()+1, {false, 0});\n dp[word.size()].first = true;\n for(int i=word.size()-1; i>=0; i--){\n Trie* curr = root;\n for(int j=i; j<word.size(); j++){\n curr = curr->next[word[j]];\n if(!curr) break;\n if(curr->end){\n dp[i] = max(dp[i], {dp[j+1].first, dp[j+1].second + 1});\n }\n }\n }\n if(dp[0].second > 1 && dp[0].first) ans.push_back(word);\n }\n return ans;\n }\n};",
"memory": "154243"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n const int p = 31;\n const int mod = 1e9 + 9;\n long long pwr[31];\n long long inv[31];\n long long mexp(int a, int b) {\n long long res = 1;\n while (b>0) {\n if(b&1) res = (res*a)%mod;\n a = (1LL*a*a)%mod;\n b = b>>1;\n }\n return res;\n }\n void precomp() {\n pwr[0] = 1, inv[0]=1;\n for (int i = 1; i < 31; ++i) {\n pwr[i] = (pwr[i - 1] * p) % mod;\n }\n for (int i = 1; i < 31; ++i) {\n inv[i] = mexp(pwr[i], mod - 2);\n }\n }\n void comp(string& s, vector<vector<long long>>& v,\n vector<unordered_set<long long>>& total_hash, int i) {\n int n = s.size();\n vector<long long> hash(n + 1);\n hash[0] = 0;\n for (int i = 1; i <= n; ++i) {\n hash[i] = ((hash[i - 1]) + (s[i - 1] - 'a' + 1) * pwr[i - 1]) % mod;\n // cout<<hash[i]<<endl;\n }\n v.push_back(hash);\n total_hash[n].insert(hash[n]);\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int m = words.size();\n vector<vector<long long>> v;\n vector<unordered_set<long long>> total_hash(31);\n\n precomp();\n for (int i = 0; i < m; ++i) {\n comp(words[i], v, total_hash, i);\n }\n vector<string> ans;\n for (int k = 0; k < m; ++k) {\n int n = words[k].size();\n vector<bool> dp(n + 1, 0);\n dp[0] = 1;\n queue<int> q;\n q.push(0);\n while (!q.empty()) {\n // if(k==2) cout<<1<<endl;\n if(dp[n]==1) break;\n int ind = q.front();\n q.pop();\n for (int j = ind + 1; j <= n&&(j-ind<n); ++j) {\n if(dp[j]==1) continue;\n int sz = j - ind;\n int curr = (v[k][j] - v[k][ind] + mod) % mod;\n if(k==2){\n // cout<<ind<<\" \"<<j<<\" \"<<curr<<endl;\n }\n curr = (curr*inv[ind])%mod;\n if(total_hash[sz].count(curr)){\n dp[j]=1;\n q.push(j);\n }\n }\n }\n if(dp[n]==1){\n ans.push_back(words[k]);\n }\n }\n return ans;\n }\n};",
"memory": "157386"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool check(int start, int end, string &s,vector<vector<int>> &dp,unordered_set<string> &dict){\n if(end == s.size()){\n return start == end;\n }\n if(dp[start][end] != -1)\n return dp[start][end];\n \n // take end\n string temp = string(s.begin()+start, s.begin()+end+1);\n bool ans;\n if(dict.find(temp) != dict.end()){\n ans = check(end+1,end+1,s,dp,dict);\n }\n // dont take end\n dp[start][end] = ans || check(start,end+1,s,dp,dict);\n return dp[start][end];\n\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> pos;\n unordered_set<string> dict;\n vector<string> curPos;\n\n for(auto &word:words)\n dict.insert(word);\n \n for(auto &word:words){\n vector<vector<int>> dp(word.size(),vector<int>(word.size(),-1));\n dict.erase(word);\n curPos.clear();\n if(check(0,0,word,dp,dict))\n pos.push_back(word);\n dict.insert(word);\n }\n return pos;\n }\n};",
"memory": "160528"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool check(int start, int end, string &s, vector<string> &curPos,vector<vector<int>> &dp,unordered_set<string> &dict){\n if(end == s.size()){\n return start == end;\n }\n if(dp[start][end] != -1)\n return dp[start][end];\n \n // take end\n string temp = string(s.begin()+start, s.begin()+end+1);\n // cout << start << ' ' << end << ' ' << temp << endl;\n bool ans;\n if(dict.find(temp) != dict.end()){\n curPos.push_back(temp);\n ans = check(end+1,end+1,s,curPos,dp,dict);\n curPos.pop_back();\n }\n // dont take end\n dp[start][end] = ans || check(start,end+1,s,curPos,dp,dict);\n return dp[start][end];\n\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> pos;\n unordered_set<string> dict;\n vector<string> curPos;\n\n for(auto &word:words)\n dict.insert(word);\n \n for(auto &word:words){\n vector<vector<int>> dp(word.size(),vector<int>(word.size(),-1));\n dict.erase(word);\n curPos.clear();\n if(check(0,0,word,curPos,dp,dict))\n pos.push_back(word);\n dict.insert(word);\n }\n return pos;\n }\n};",
"memory": "163671"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_set<string> s;\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> res;\n for(string& w:words)\n s.insert(w);\n for(string& w:words)\n {\n vector<vector<int>> dp(w.size()+1,vector<int>(w.size()+1,-1));\n if(check(w,0,0,dp))\n res.push_back(w);\n }\n return res;\n }\n bool check(string& word,int ind,int cnt, vector<vector<int>>& dp)\n {\n if(ind==word.size())\n {\n if(cnt>=2) return true;\n return false;\n }\n else if(dp[ind][cnt] != -1) return dp[ind][cnt];\n string t=\"\";\n for(int i=ind;i<word.size();i++)\n {\n t+=word[i];\n if(s.count(t) && check(word,i+1,cnt+1,dp))\n return dp[ind][cnt] = true;\n }\n return dp[ind][cnt] = false;\n }\n};",
"memory": "166813"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<string> ans;\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> st(words.begin(),words.end());\n vector<vector<int>> dp;\n for(int i=0;i<words.size();i++){\n if(!words[i].empty()){\n dp=vector<vector<int>>(words[i].size()+1,vector<int>(words[i].size()+1,-1));\n if(recurse(words[i],0,0,st,dp)){\n ans.push_back(words[i]);\n }\n }\n }\n return ans;\n }\n bool recurse(string &word,int start,int count,unordered_set<string>& st,vector<vector<int>>& dp){\n if(start>=word.size()){\n return count>=2;\n }\n if(dp[start][count]!=-1){\n return dp[start][count];\n }\n string curr=\"\";\n for(int j=start;j<word.size();j++){\n curr+=word[j];\n if(st.count(curr) && curr!=word){\n if(recurse(word,j+1,count+1,st,dp)){\n return dp[j][count]=true;\n }\n }\n }\n return dp[start][count]=false;\n }\n};",
"memory": "169956"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<string> ans;\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_set<string> st(words.begin(),words.end());\n vector<vector<int>> dp;\n for(int i=0;i<words.size();i++){\n if(!words[i].empty()){\n dp=vector<vector<int>>(words[i].size()+1,vector<int>(words[i].size()+1,-1));\n if(recurse(words[i],0,0,st,dp)){\n ans.push_back(words[i]);\n }\n }\n }\n return ans;\n }\n bool recurse(string &word,int start,int count,unordered_set<string>& st,vector<vector<int>>& dp){\n if(start>=word.size()){\n return count>=2;\n }\n if(dp[start][count]!=-1){\n return dp[start][count];\n }\n string curr=\"\";\n for(int j=start;j<word.size();j++){\n curr+=word[j];\n if(st.count(curr) && curr!=word){\n if(recurse(word,j+1,count+1,st,dp)){\n return dp[start][count]=true;\n }\n }\n }\n return dp[start][count]=false;\n }\n};",
"memory": "169956"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_set<string> s;\n bool check(string& word,int index,int cnt,vector<vector<int>>&dp){\n if(index==word.size()){\n if(cnt>=2) return true;\n return false;\n }\n if(dp[index][cnt]!=-1){\n return dp[index][cnt];\n }\n string t=\"\";\n for(int i=index;i<word.size();i++){\n t+=word[i];\n if(s.count(t) && check(word,i+1,cnt+1,dp)) return dp[index][cnt]=true;\n }\n return dp[index][cnt]=false;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> res;\n for(auto w:words)\n s.insert(w);\n for(auto w:words){\n vector<vector<int>>dp(w.size()+1,vector<int>(w.size()+1,-1));\n if(check(w,0,0,dp))\n res.push_back(w);\n }\n return res;\n }\n};",
"memory": "173098"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n set<string> word;\n for(auto i:words) word.insert(i);\n vector<string> ans;\n for(auto x:words){\n cout<<\"curr word:\"<<x<<\"\\n\";\n word.erase(x);\n vector<vector<int>> dp(x.size(),vector<int>(x.size(),-1));\n int n=x.size();\n function<int(int, int)> hmm = [&](int start,int end){\n if(start>end) return 0;\n if(dp[start][end]!=-1) return dp[start][end];\n \n if(word.find(x.substr(start,end-start+1)) != word.end()) return dp[start][end]=1;\n if(start==end) return dp[start][end]=0;\n for(int k=start;k<end;k++){\n if(hmm(start,k)==1 and hmm(k+1,end)==1) return dp[start][end]=1;\n }\n return dp[start][end]=0;\n };\n if(hmm(0,n-1)) ans.push_back(x);\n word.insert(x);\n }\n return ans;\n }\n};",
"memory": "176241"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n bool checkConcatenation(int currIndex, int lastIndex, int sz, string &s, set<string> &st, vector<vector<int>> &dp){\n if(currIndex==sz-1){\n if(lastIndex==0) return 0;\n string str=s.substr(lastIndex,currIndex-lastIndex+1);\n if(st.count(str)) return 1;\n return 0;\n }\n if(dp[currIndex][lastIndex]!=-1) return dp[currIndex][lastIndex];\n string str=s.substr(lastIndex,currIndex-lastIndex+1);\n bool check=checkConcatenation(currIndex+1,lastIndex,sz,s,st,dp);\n if(st.count(str)){\n check|=checkConcatenation(currIndex+1,currIndex+1,sz,s,st,dp);\n }\n return dp[currIndex][lastIndex]=check;\n \n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n=words.size();\n set<string> st;\n for(auto &i:words){\n st.insert(i);\n }\n vector<string> concatenatedWords;\n for(int i=0;i<n;++i){\n string s=words[i];\n vector<vector<int>> dp(s.size()+1,vector<int>(s.size()+1,-1));\n if(checkConcatenation(0,0,s.size(),s,st,dp)){\n concatenatedWords.push_back(s);\n }\n }\n return concatenatedWords;\n }\n};",
"memory": "179383"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int f(string &curr,unordered_set<string> &data,vector<vector<int>> &dp,int ind,int pre,string match)\n {\n if(ind==curr.size())\n {\n if(data.find(match)!=data.end() && match!=curr)\n return 1;\n return 0;\n }\n if(dp[ind][pre]!=-1)\n return dp[ind][pre];\n int a=0,b=0;\n if(data.find(match)!=data.end())\n {\n a=f(curr,data,dp,ind,ind,\"\");\n \n }\n match.push_back(curr[ind]);\n // cout<<match<<endl;\n b=f(curr,data,dp,ind+1,pre,match);\n return dp[ind][pre]=a || b;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n \n\n unordered_set<string> data;\n for(auto i:words)\n data.insert(i);\n\n vector<string> ans;\n for(auto curr:words)\n {\n vector<vector<int>> dp(curr.size()+1,vector<int> (curr.size()+1,-1));\n int a=0;\n a=f(curr,data,dp,0,0,\"\");\n if(a)\n ans.push_back(curr);\n }\n return ans;\n }\n};",
"memory": "182526"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n #define vi vector<int>\n #define vvi vector<vi>\n #define pb push_back\n #define dbg(x) cout << #x << \" : \" << x << endl\n\n int node_cnt = 1;\n int cnt[300005];\n int ends[300005];\n void add(string &s , vvi &trie){\n int x = 0;\n for(int i=0;i<s.size();i++){\n if(trie[x][s[i] - 'a'] == 0){\n trie.pb(vi(26 , 0));\n trie[x][s[i] - 'a'] = node_cnt;\n node_cnt++;\n assert(node_cnt == trie.size());\n } \n x = trie[x][s[i] - 'a'];\n cnt[x]++;\n if(i == s.size() - 1){\n ends[x] = 1;\n }\n }\n }\n\n void erase(string &s , vvi &trie){\n int x = 0;\n for(int i=0;i<s.size();i++){\n assert(trie[x][s[i] - 'a'] != 0);\n\n x = trie[x][s[i] - 'a'];\n cnt[x]--;\n if(i == s.size() - 1){\n ends[x] = 0;\n }\n }\n }\n \n int dp[300005];\n int query(int pos , string &s , vvi &trie){\n if(pos == s.size())return 1;\n if(dp[pos] != -1)return dp[pos];\n\n int x = 0;\n int ok = 0;\n for(int i=pos;i<s.size();i++){\n if(cnt[trie[x][s[i] - 'a']] > 0 && trie[x][s[i] - 'a'] != 0){\n x = trie[x][s[i] - 'a'];\n if(ends[x] == 1){\n // [pos...i] is a word\n dbg(s.substr(pos , i-pos+1));\n if(query(i+1 , s , trie) == 1)ok = 1;\n }\n } \n else{\n break;\n }\n }\n return dp[pos] = ok;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& a) {\n // easy trie question\n // dynamic trie in strings, else TLE/MLE!\n\n cout << \"a : \";\n for(string s : a){\n cout << s << \" \";\n }\n cout << endl;\n\n memset(cnt , 0 , sizeof(cnt));\n memset(ends , 0 , sizeof(ends));\n memset(dp , -1 , sizeof(dp));\n\n vvi trie;\n node_cnt = 1;\n trie.pb(vi(26 , 0));\n\n vector<string> ans;\n int n = a.size();\n for(int i=0;i<n;i++){\n add(a[i] , trie);\n }\n for(int i=0;i<n;i++){\n erase(a[i] , trie);\n for(int j=0;j<=a[i].size();j++)dp[j] = -1;\n if(query(0 , a[i] , trie) == 1)ans.pb(a[i]);\n add(a[i] , trie);\n }\n return ans;\n }\n};",
"memory": "185668"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<string, int> mp;\n bool CheckSubstring(vector<string>& dictionary, string sentence,\n int curIndex, int lastIndex, vector<vector<int>>& dp) {\n if (curIndex == sentence.length() - 1) {\n if(lastIndex==0) return 0;\n bool check = 0;\n int subStringlen = curIndex - lastIndex + 1;\n string subString = sentence.substr(lastIndex, subStringlen);\n if (mp.count(subString)) {\n check = 1;\n }\n return dp[curIndex][lastIndex] = check;\n }\n if (dp[curIndex][lastIndex] != -1)\n return dp[curIndex][lastIndex];\n int subStringlen = curIndex - lastIndex + 1;\n string subString = sentence.substr(lastIndex, subStringlen);\n bool check =\n CheckSubstring(dictionary, sentence, curIndex + 1, lastIndex, dp);\n if (mp.count(subString)) {\n check |= CheckSubstring(dictionary, sentence, curIndex + 1,\n curIndex + 1, dp);\n }\n return dp[curIndex][lastIndex] = check;\n }\n bool DictionaryCheck(vector<string>& dictionary, string sentence) {\n int n = sentence.length();\n vector<vector<int>> dp(n, vector<int>(n, -1));\n bool CheckSentence = CheckSubstring(dictionary, sentence, 0, 0, dp);\n return CheckSentence;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for(auto i:words) mp[i]=1;\n vector<string>ans;\n for(auto i:words){\n bool check=DictionaryCheck(words,i);\n if(check) ans.push_back(i);\n } \n return ans;\n }\n};",
"memory": "188811"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Trie{\npublic:\n int flag;\n unordered_map<char, Trie*>child;\n Trie(){\n flag = -1;\n }\n};\nclass Solution {\npublic:\n static bool vec_sort(string& a, string& b){\n return a.length() < b.length();\n }\n void go(Trie* node, Trie* root, string& s, int spot, vector<string>& ans, int depth){\n if(node->child.count(s[spot]) == 0){\n if(depth != spot)return;\n node->child[s[spot]] = new Trie();\n if(spot == s.length() - 1){\n node->child[s[spot]]->flag = 1;\n }\n else{\n node->child[s[spot]]->flag = 0;\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n else if(node->child[s[spot]]->flag == 1){\n int pre_size = ans.size();\n if(spot == s.length() - 1)ans.push_back(s);\n else{\n go(root, root, s, spot + 1, ans, 0);\n if(pre_size == ans.size()){\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n }\n else{\n if(s.length() - 1 == spot)return;\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n // sort(words.begin(), words.end(), vec_sort);\n Trie* root = new Trie();\n vector<string>ans;\n int len = words.size();\n for(int t=1;t<=30;t++){\n for(int s=0;s<len;s++){\n if(words[s].length() == t)go(root, root, words[s], 0, ans, 0);\n }\n }\n // for(auto& s:words){\n // go(root, root, s, 0, ans, 0);\n // }\n return ans;\n }\n};",
"memory": "201381"
} |
472 | <p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["cat","dog","catdog"]
<strong>Output:</strong> ["catdog"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 30</code></li>
<li><code>words[i]</code> consists of only lowercase English letters.</li>
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
<li><code>1 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Trie{\npublic:\n int flag;\n unordered_map<char, Trie*>child;\n Trie(){\n flag = -1;\n }\n};\nclass Solution {\npublic:\n void go(Trie* node, Trie* root, string& s, int spot, vector<string>& ans, int depth){\n if(node->child.count(s[spot]) == 0){\n if(depth != spot)return;\n node->child[s[spot]] = new Trie();\n Trie* tempnode = node->child[s[spot]];\n if(spot == s.length() - 1){\n tempnode->flag = 1;\n }\n else{\n tempnode->flag = 0;\n go(tempnode, root, s, spot + 1, ans, depth + 1);\n }\n }\n else if(node->child[s[spot]]->flag == 1){\n int pre_size = ans.size();\n if(spot == s.length() - 1)ans.push_back(s);\n else{\n go(root, root, s, spot + 1, ans, 0);\n if(pre_size == ans.size()){\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n }\n else{\n if(s.length() - 1 == spot)return;\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n // sort(words.begin(), words.end(), vec_sort);\n Trie* root = new Trie();\n vector<string>ans;\n int len = words.size();\n for(int t=1;t<=30;t++){\n for(int s=0;s<len;s++){\n if(words[s].length() == t)go(root, root, words[s], 0, ans, 0);\n }\n }\n return ans;\n }\n};",
"memory": "201381"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.