id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n map<int, int> cache;\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if (amount == 0) return 0;\n if (amount < 0 || coins.size() == 0) return -1;\n auto r = coinChangeR(coins, amount);\n return r == INT_MAX ? -1: r;\n }\n int coinChangeR(vector<int>& coins, int amount) {\n if (amount == 0) return 0;\n if (amount < 0 || coins.size() == 0) return INT_MAX;\n if (!cache.count(amount)) {\n int change = INT_MAX;\n for (const auto& c: coins) {\n change = min(change, coinChangeR(coins, amount - c));\n }\n if (change != INT_MAX) {\n cache[amount] = change + 1;\n }\n else {\n cache[amount] = INT_MAX;\n }\n }\n return cache[amount];\n }\n};", "memory": "74350" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<int,int> memo;\n int coiner(vector<int>& coins, int amount,map<int,int>& memo) {\n if(amount==0){\n return 0;\n }\n if(amount<0){\n return -1;\n }\n if(memo.find(amount)!=memo.end()){\n return memo[amount];\n }\n int mini=INT_MAX;\n int t;\n for(int i:coins){\n t=coiner(coins,amount-i,memo);\n if(t==-1){\n continue;\n }\n mini=min(mini,t+1);\n }\n if(mini!=INT_MAX){\n memo[amount]=mini;\n return mini;\n }\n memo[amount]=-1;\n return -1;\n }\n int coinChange(vector<int>& coins, int amount) {\n return coiner(coins,amount,memo);\n }\n};", "memory": "74350" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if (amount == 0) {\n return 0;\n }\n\n // Sort coins in reverse order (largest to smallest)\n std::sort(coins.rbegin(), coins.rend());\n\n // Priority queue to store (count of coins used, remaining amount)\n std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> q;\n\n // Initialize the priority queue with the first set of states\n for (int coin : coins) {\n if (amount >= coin) {\n q.push({1, amount - coin});\n }\n }\n\n // Hash map to track the smallest count to reach a particular total\n std::unordered_map<int, int> seen;\n for (int i = 0; i <= amount; ++i) {\n seen[i] = INT_MAX;\n }\n\n // Process the priority queue\n while (!q.empty()) {\n auto [count, total] = q.top();\n q.pop();\n\n // If total is 0, we found the solution\n if (total == 0) {\n return count;\n }\n\n // For each coin, push the new state if it's more efficient\n for (int coin : coins) {\n int next_total = total - coin;\n if (next_total >= 0) {\n // Proceed if it's more efficient\n if (seen[next_total] > count) {\n seen[next_total] = count;\n q.push({count + 1, next_total});\n }\n }\n }\n }\n\n // If no solution was found, return -1\n return -1;\n }\n};", "memory": "75204" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n vector<int> dp ;\n void nbCoins(vector<int> coins ,int amount){\n if(amount == 0){\n return ;\n }\n int nb = amount+1;\n for( int i =0 ; i<coins.size() ; i++){\n int diff = amount - coins[i];\n if(diff<0){\n continue;\n }\n if(dp[diff] == -2){\n nbCoins(coins,diff);\n }\n if(dp[diff] == -1){\n continue;\n }else{\n nb = min(nb,1+dp[diff]);\n } \n }if(nb == amount+1)\n dp[amount] = -1;\n else\n dp[amount] = nb;\n }\n int coinChange(vector<int>& coins, int amount) {\n for( int i = 0 ;i<=amount;i++){\n dp.push_back(-2);\n }\n dp[0] = 0;\n nbCoins(coins,amount);\n return dp[amount]; \n }\n};", "memory": "75204" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if(amount == 0){return 0;}\n sort(coins.begin(), coins.end());\n int ind = coins.size()-1;\n int minn = 1e9;\n queue<pair<pair<int,int>,int>> q;\n q.push({{amount,0},ind});\n unordered_set<int> st;\n while(!q.empty()){\n auto top = q.front();\n q.pop();\n int amt = top.first.first;\n int cnt = top.first.second;\n int ind = top.second;\n if(amt == 0){return cnt;}\n // if(amt<0){continue;}\n for(int i = ind; i>=0; i--){\n // cout<<\"here \"<<i<<endl;\n if(amt-coins[i]>=0 && st.find(amt-coins[i])==st.end()){\n q.push({{amt-coins[i],cnt+1},i});\n st.insert(amt-coins[i]);\n }\n }\n }\n return -1; \n }\n};", "memory": "76058" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) \n {\n if(amount == 0)\n return 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; // n coins, amt to make\n pq.push({0,amount});\n\n unordered_set<int>vis;\n\n while(!pq.empty())\n {\n int coin = pq.top().first;\n int amt = pq.top().second;\n pq.pop();\n\n if(amt == 0)\n return coin;\n if(vis.find(amt) != vis.end())\n continue;\n vis.insert(amt);\n\n for(int i=0; i<coins.size(); i++)\n {\n if(amt < coins[i])\n continue;\n \n pq.push({coin+1, amt-coins[i]});\n }\n }\n return -1;\n }\n};", "memory": "76058" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using node = pair<int, int>;\n\n int coinChange(vector<int>& coins, int amount) {\n // TLE\n // We can do a BFS\n // Regarding amount as the first node, coins are the edges\n // We make a node to track {current coin count, amount left}\n // In BFS, the first pop() elements from the queue are the \"shortest\" path.\n queue<node> q;\n set<int> amountSeen;\n int coinNum = coins.size();\n sort(coins.begin(), coins.end());\n q.push({amount, 0});\n amountSeen.insert(amount);\n int minCount = amount + 1;\n while(!q.empty()){\n int currAmount = q.front().first;\n int currCoinCount = q.front().second;\n q.pop();\n \n // cout<<currAmount<<endl;\n if (currAmount == 0) return currCoinCount;\n // Traverse from \n for (int i = coinNum - 1; i >= 0; i --) {\n int nextAmount = currAmount - coins[i];\n if (nextAmount >= 0)\n if (!amountSeen.count(nextAmount)){\n // ensure first-visit\n q.push({nextAmount, currCoinCount+1});\n amountSeen.insert(nextAmount);\n }\n }\n }\n return -1;\n }\n};", "memory": "76911" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using node = pair<int, int>;\n\n int coinChange(vector<int>& coins, int amount) {\n // TLE\n // We can do a BFS\n // Regarding amount as the first node, coins are the edges\n // We make a node to track {current coin count, amount left}\n // In BFS, the first pop() elements from the queue are the \"shortest\" path.\n queue<node> q;\n set<int> amountSeen;\n int coinNum = coins.size();\n sort(coins.begin(), coins.end());\n q.push({amount, 0});\n amountSeen.insert(amount);\n int minCount = amount + 1;\n while(!q.empty()){\n int currAmount = q.front().first;\n int currCoinCount = q.front().second;\n q.pop();\n \n // cout<<currAmount<<endl;\n if (currAmount == 0) return currCoinCount;\n // Traverse from \n for (int i = coinNum - 1; i >= 0; i --) {\n int nextAmount = currAmount - coins[i];\n if (nextAmount >= 0)\n if (!amountSeen.count(nextAmount)){\n // ensure first-visit\n q.push({nextAmount, currCoinCount+1});\n amountSeen.insert(nextAmount);\n }\n }\n }\n return -1;\n }\n};", "memory": "76911" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int memo(int i, vector<int>&coins, int amt, vector<vector<int>>&dp){\n if(i==0){\n if(amt%coins[0]==0) return amt/coins[0];\n else return 1e9;\n }\n \n if(dp[i][amt]!=-1) return dp[i][amt];\n\n int notPick = 0 + memo(i-1, coins, amt, dp);\n int pick = INT_MAX;\n if(amt>=coins[i]){\n pick = 1 + memo(i, coins, amt-coins[i], dp);\n }\n \n\n return dp[i][amt] = min(pick, notPick);\n \n }\n\n\n int tab(vector<int>&coins, int amt){\n int n = coins.size();\n vector<vector<int>>dp(n, vector<int>(amt+1,-1));\n \n for(int a = 0 ; a<=amt ; a++){\n if(a%coins[0]==0) dp[0][a] = a/coins[0];\n else dp[0][a] = 1e9;\n }\n\n for(int i=1 ; i<n ; i++){\n for(int a=0 ; a<=amt ; a++){\n int notPick = 0 + dp[i-1][a];\n int pick = 1e9;\n if(a>=coins[i]){\n pick = 1 + dp[i][a-coins[i]];\n }\n \n\n dp[i][a] = min(pick, notPick);\n }\n }\n return dp[n-1][amt];\n }\n\n int spaceOpt(vector<int>&coins, int amt){\n int n = coins.size();\n vector<int>prev(amt+1,0);\n\n for(int i=0 ; i<=amt ; i++){\n if(i%coins[0]==0) prev[i] = i/coins[0];\n else prev[i] = 1e9;\n }\n \n for(int i=1 ; i<n ; i++){\n vector<int>curr(amt+1,0);\n for(int a=0 ; a<=amt ; a++){\n int notPick = 0 + prev[a];\n int pick = 1e9;\n if(a>=coins[i]){\n pick = 1 + curr[a-coins[i]];\n }\n \n\n curr[a] = min(pick, notPick);\n }\n prev = curr;\n }\n return prev[amt];\n\n\n } \n\n\n \n int coinChange(vector<int>& coins, int amount) { \n int n = coins.size();\n vector<vector<int>>dp(n,vector<int>(amount+1,-1));\n // int ans = memo(n-1, coins,amount, dp);\n // int ans = tab(coins, amount);\n int ans = spaceOpt(coins, amount);\n if(ans>=1e9) return -1;\n return ans;\n }\n};", "memory": "77765" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint noofcoins(vector<int>coins,int a,vector<vector<int>>&dp,int i,int n)\n{ vector<int>prev(a+1,0);\nfor(int i=0;i<=a;i++) {if(i%coins[0]==0) prev[i]=i/coins[0];\nelse prev[i]=1e9;\n}\n\nfor(int i=1;i<n;i++){\n vector<int>curr(a+1,0); \nfor(int j=0;j<=a;j++){\nint pick=INT_MAX;\nint npick=prev[j];\nif(coins[i]<=j) pick=1+curr[j-coins[i]];\n curr[j]=min(pick,npick);\n}\nprev=curr;\n}\nreturn prev[a];\n}\n int coinChange(vector<int>& coins, int amount) {\n int n=coins.size();\n vector<vector<int>>dp(n,vector<int>(amount+1,0));\n int ans= noofcoins(coins,amount,dp,n-1,n);\n if(ans>=1e9) return -1;\n return ans;\n }\n};", "memory": "77765" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int f(int i, int target, vector<int>& coins) {\n if(i == 0) {\n if(target % coins[i] == 0) return target / coins[i];\n return 1e9;\n }\n\n if(dp[i][target] != -1) return dp[i][target];\n\n int notpick = f(i-1, target, coins);\n int pick = 1e9;\n if(coins[i] <= target) {\n pick = 1 + f(i, target - coins[i], coins);\n }\n\n return dp[i][target] = min(pick, notpick);\n }\n int coinChange(vector<int>& coins, int amount) {\n if(amount == 0) return 0;\n int n = coins.size();\n vector<vector<int>> dp(n, vector<int>(amount + 1, -1));\n this->dp = dp;\n int ans = f(n -1, amount, coins);\n return ans == 1e9 ? -1: ans;\n }\n};", "memory": "78619" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp(vector<int>& coins, int amount, set<int> &s, map<int, int> &memo){\n if(memo.find(amount) != memo.end()){\n return memo[amount];\n }\n\n if(s.find(amount) != s.end()){\n memo[amount] = 1;\n return 1;\n }else if(amount < coins[0]){\n memo[amount] = -1;\n return -1;\n }\n\n int ans = -1;\n for(int coin : coins){\n int ret = dp(coins, amount-coin, s, memo);\n if(ret != -1){\n if(ans == -1){\n ans = ret;\n } else{\n ans = min(ans, ret);\n }\n }\n }\n if(ans == -1){\n memo[amount] = -1;\n return -1;\n }else{\n memo[amount] = 1+ans;\n return 1+ans;\n }\n }\n int coinChange(vector<int>& coins, int amount) {\n if(amount == 0){\n return 0;\n }\n set<int> s(coins.begin(), coins.end());\n map<int,int> memo;\n sort(coins.begin(), coins.end());\n return dp(coins, amount, s, memo);\n }\n};", "memory": "78619" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define DPSolver ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); \n map<int,int> memo;\n bool dpSolverFlag = false;\n int coinChange(vector<int>& coins, int amount, bool notFirstStep = false ) {\n if(!dpSolverFlag){\n DPSolver;\n dpSolverFlag = true; \n }\n // make it efficient\n auto it = memo.find(amount); \n if(it != memo.end()) \n return it -> second; \n\n // make it work \n // base case \n if(amount == 0)\n return 0; \n else if (amount < 0){\n memo[amount] = INT_MAX; \n return INT_MAX; \n }\n\n int minCoins = INT_MAX; \n for(int c : coins)\n minCoins = min(minCoins, coinChange(coins, amount - c, true)); \n \n if(minCoins == INT_MAX){\n memo[amount] = INT_MAX; \n if(notFirstStep)\n return INT_MAX; \n else \n return -1; \n }\n else {\n memo[amount] = minCoins + 1; \n return minCoins + 1; \n }\n }\n};", "memory": "79473" }
322
<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 fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n unordered_map<int, long long> dp;\n long long recurse(int amount, vector<int>& coins) {\n if(amount < 0) return INT_MAX;\n if(amount == 0) return 0;\n if(dp.find(amount) != dp.end()) return dp[amount];\n long long ans = INT_MAX;\n for(int i=0;i<coins.size();i++) {\n if(amount >= coins[i]) {\n ans = min(ans, 1LL + recurse(amount - coins[i], coins));\n }\n }\n return dp[amount] = ans;\n }\n \n int coinChange(vector<int>& coins, int amount) {\n long long ans = recurse(amount, coins);\n return ans >= INT_MAX ? -1 : ans;\n }\n};", "memory": "79473" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int poorPigs(int total, int minutesToDie, int minutesToTest) {\n if(total==1) return 0;\n int n= (minutesToTest / minutesToDie) +1;\n int p=n;\n int cnt=1;\n while( n<total){\n n=n*p; cnt++;\n }\n return cnt;\n }\n};", "memory": "7200" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\n public:\n int poorPigs(int buckets, int timeDetect, int timeTest) {\n return ceil(log2(buckets)/log2(int(timeTest/timeDetect)+1));\n }\n };\n\n\n\n ", "memory": "7300" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int poorPigs(int buckets, int minutesToDie, int minutesToTest) {\n int trials = minutesToTest / minutesToDie + 1;\n int numPigs = 0;\n for (int testedBuckets = 1; testedBuckets < buckets; testedBuckets *= trials) {\n numPigs++;\n }\n return numPigs;\n }\n};", "memory": "7300" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int poorPigs(int buckets, int minutesToDie, int minutesToTest) {\n return ceil(log2(buckets)/log2(int(minutesToTest/minutesToDie)+1)); \n }\n};", "memory": "7400" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\n public:\n int poorPigs(int buckets, int minutesToDie, int minutesToTest) {\n const int base = minutesToTest / minutesToDie + 1;\n int ans = 0;\n for (int x = 1; x < buckets; x *= base)\n ++ans;\n return ans;\n }\n};", "memory": "7500" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int poorPigs(int buckets, int minutesToDie, int minutesToTest) {\n return ceil(log2(buckets)/log2(int(minutesToTest/minutesToDie)+1));\n }\n};", "memory": "7500" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
2
{ "code": "\nclass Solution {\npublic:\n int poorPigs(int buckets, int minutesToDie, int minutesToTest) {\n // Calculate the number of tests we can conduct within the given time\n int tests = minutesToTest / minutesToDie;\n \n // The number of possible states each pig can be in after the tests\n // is tests + 1 (alive after test 1, alive after test 2, ..., alive after the last test, and dead)\n int states = tests + 1;\n \n // We need to determine the minimum number of pigs required\n // such that the states^pigs >= buckets\n int pigs = 0;\n while (pow(states, pigs) < buckets) {\n pigs++;\n }\n return pigs;\n }\n};\n", "memory": "7600" }
458
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p> <p>You can feed the pigs according to these steps:</p> <ol> <li>Choose some live pigs to feed.</li> <li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.</li> <li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li> <li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li> <li>Repeat this process until you run out of time.</li> </ol> <p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= buckets &lt;= 1000</code></li> <li><code>1 &lt;=&nbsp;minutesToDie &lt;=&nbsp;minutesToTest &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int poorPigs(int buckets, int timeDetect, int timeTest) {\n return ceil(log2(buckets)/log2(int(timeTest/timeDetect)+1));\n }\n};", "memory": "7600" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(const std::string& str)\n\t{\n\t\tconst int len = str.length();\n\t\tfor (int i = len / 2; i >= 1; --i)\n\t\t\tif (len % i == 0 && str.substr(0, len - i) == str.substr(i))\n\t\t\t\treturn true;\n\n\t\treturn false;\n\t}\n};", "memory": "32553" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(const std::string& str)\n\t{\n\t\tconst int len = str.length();\n\t\tfor (int i = len / 2; i >= 1; --i)\n\t\t\tif (len % i == 0 && str.substr(0, len - i) == str.substr(i))\n\t\t\t\treturn true;\n\n\t\treturn false;\n\t}\n};", "memory": "32553" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int size = s.size();\n string r = \"\";\n for(int i = 0; i < size; i++) {\n if(r.size() > 0 && s[i] == r[0] && size % r.size() == 0) {\n int m = size / r.size();\n string ns = \"\";\n while(m > 0) {\n ns += r;\n m--;\n }\n cout << s + \" \" + ns;\n if(ns == s) return true;\n }\n r += s[i];\n }\n \n return false;\n }\n};", "memory": "39026" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n vector<int> sol;\n for(int i=1;i<=s.size()/2;i++){\n if(n%i == 0){\n sol.push_back(i);\n }\n }\n for(int i=0;i<sol.size();i++){\n string ans = s.substr(sol[i]);\n if(s.starts_with(ans)){\n return true;\n }\n }\n return false;\n }\n};", "memory": "39026" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n=s.size();\n for(int i=1;i<=n/2;i++){\n if(n%i==0){\n string x=s.substr(0,i), temp=s;\n while(temp.find(x)!=string::npos){\n temp.erase(temp.find(x), x.size());\n }\n if(temp==\"\") return true;\n\n }\n\n }\n\n return false;\n }\n};", "memory": "45498" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n if (s.size() <= 1) {\n return false;\n }\n for (int len = 2; len < s.size();) {\n if (FoundForThisLength(s, len)) {\n return true;\n }\n\n len++;\n while (s.size() % len != 0) {\n len++;\n }\n }\n return FoundForThisLength(s, 1);\n }\n\nprivate:\n bool FoundForThisLength(string s, size_t length) {\n string sub_str(s, 0, length);\n for (int i = 0; i < s.size(); i += length) {\n if (s.find(sub_str, i) != i) {\n return false;\n }\n }\n return true;\n }\n};", "memory": "45498" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n vector<int> index;\n for(int i=1; i<=s.size()/2; i++) {\n if(s[i] == s[0]) index.push_back(i);\n }\n\n int k = 0,j=INT_MIN;\n for(int i=0; i<index.size(); i++) {\n if(index[i]<j) continue;\n int pattern_length = index[i] - k;\n string sub_string = s.substr(k, pattern_length);\n //cout<<sub_string<<endl;\n j = pattern_length;\n while(j < s.size()) {\n string sub_string1 = s.substr(j, pattern_length);\n if(sub_string == sub_string1) {\n j += pattern_length;\n }else {\n j -= pattern_length;\n break;\n }\n }\n if(j==s.size()) return true;\n }\n return false;\n }\n};\n // extract 'go' from 'iamgoodhere'\n// string s = \"iamgoodhere\";\n// int start = 3, end = 4;\n// cout<<s.substr(start,end-start+1); // go", "memory": "51971" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n \n vector<int> factors;\n int n = s.size();\n for (int i = 1; i <= sqrt(n); i++) {\n if (n % i == 0) {\n if (n / i == i)\n factors.push_back(i);\n else {\n factors.push_back(i);\n factors.push_back(n/i);\n }\n }\n }\n for(auto x: factors) {\n if(x==n) continue;\n string sub = s.substr(0, x), tmp;\n for(int i=0; i<n/x; i++) \n tmp += sub;\n if(!s.compare(tmp)) return true;\n }\n return false;\n\n }\n};", "memory": "51971" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n for(int i = n/2;i>=1;i--){\n if(n%i == 0){\n int t = n/i;\n string a = \"\";\n while(t--){\n a+= (s.substr(0,i));\n }\n if(a == s) return 1;\n }\n }\n return 0;\n }\n};", "memory": "58443" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n=s.size();\n for(int l=n/2;l>=1;l--){\n if(n%l==0){\n int times=n/l;\n string ans=\"\";\n while(times--){\n ans+=s.substr(0,l);\n }\n if(ans==s){\n return true;\n }\n }\n }\n return false;\n }\n};", "memory": "58443" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string str) {\n if(!(str.size() & 1))\n {\n string first = str.substr(0, str.size() / 2);\n if(first + first == str) return true;\n }\n\n int halfSize = str.size() / 2;\n int size = str.size();\n for(int i = 1; i <= halfSize; i++)\n {\n if((size) % i == 0)\n {\n string substring = str.substr(0, i);\n while(substring.size() <= size)\n {\n if(substring == str) return true;\n substring += str.substr(0, i);\n }\n }\n }\n return false;\n }\n};", "memory": "64916" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string str) {\n if(!(str.size() & 1))\n {\n string first = str.substr(0, str.size() / 2);\n if(first + first == str) return true;\n }\n\n int halfSize = str.size() / 2;\n int size = str.size();\n for(int i = 1; i <= halfSize; i++)\n {\n if((size) % i == 0)\n {\n string substring = str.substr(0, i);\n while(substring.size() <= size)\n {\n if(substring == str) return true;\n substring += str.substr(0, i);\n }\n }\n }\n return false;\n }\n};", "memory": "64916" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\nbool repeatedSubstringPattern(std::string s) {\n int s_len = s.size();\n for (int len = 1; len < s_len; len++)\n {\n if ((s_len % len) == 0)\n {\n auto s1 = s.substr(0, s.size() - len);\n auto s2 = s.substr(len);\n if (s1 == s2) return true;\n }\n }\n return false;\n}\n};", "memory": "71388" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(const std::string& str)\n\t{\n\t\tconst int length = str.length();\n\t\tconst int halfLength = length / 2;\n\n\t\tchar first = str[0];\n\t\tint patternLength = 1;\n\t\twhile (patternLength <= halfLength)\n\t\t{\n\t\t\twhile (patternLength <= halfLength && str[patternLength] != first)\n\t\t\t\t++patternLength;\n\n\t\t\tstd::string pattern = str.substr(0, patternLength);\n\t\t\tif (patternLength != length && length % patternLength == 0)\t// is candidate valid\n\t\t\t{\n\t\t\t\tint i = patternLength;\n\t\t\t\twhile (i <= length - patternLength && str.substr(i, patternLength) == pattern)\n\t\t\t\t\ti += patternLength;\n\n\t\t\t\tif (i == length)\n\t\t\t\t\treturn true;\n\t\t\t}\n\t\t\t++patternLength;\n\t\t}\n\n\t\treturn false;\n\t}\n};", "memory": "71388" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "// Ali Eren Ciftci\nclass Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n if(s.length() < 2)\n return false;\n string temp(s);\n for(int i = 1; i <= s.length() / 2; i++) // i substring size ini tutsun\n {\n if(s.length() % i) \n continue;\n temp = temp.substr(i) + temp.substr(0, i);\n if(s == temp)\n return true;\n temp = s; \n }\n return false;\n }\n \n \n};", "memory": "77861" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "// Ali Eren Ciftci\nclass Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n if(s.length() < 2)\n return false;\n string temp = s;\n for(int i = 1; i <= s.length() / 2; i++) // i substring size ini tutsun\n {\n if(s.length() % i) // i substring olmaya aday degilse gec\n continue;\n temp = temp.substr(i) + temp.substr(0, i); // ilk i elemani sona at\n if(s == temp) // esitse bu ilk i eleman true olur\n return true;\n temp = s; // tempi geri eski haline getir. \n }\n return false;\n }\n \n \n};", "memory": "77861" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n\n int n = s.size();\n string temp;\n temp += s[0];\n char comp = s[0];\n\n for(int i = 1; i < n; i++) {\n if(s[i] != comp) temp += s[i];\n else {\n int n1 = temp.size();\n int j = i;\n while(j < n) {\n if(n - j >= n1) {\n if(temp == s.substr(j, n1)) {\n j = j + n1;\n if(j == n) return true;\n }\n else {\n temp = temp + s[i];\n break;\n }\n }\n else return false;\n }\n }\n }\n\n return false;\n \n }\n};", "memory": "84333" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n\n int n = s.size();\n string temp;\n char comp = s[0];\n\n for(int i = 0; i < n; i++) {\n if(temp.size() == 0) temp += s[i];\n else if(s[i] != comp) temp += s[i];\n else {\n int n1 = temp.size();\n int j = i;\n while(j < n) {\n if(n - j >= n1) {\n if(temp == s.substr(j, n1)) {\n j = j + n1;\n if(j == n) return true;\n }\n else {\n temp = temp + s[i];\n break;\n }\n }\n else return false;\n }\n }\n }\n\n return false;\n \n }\n};", "memory": "90806" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n [[nodiscard]]\n auto repeatedSubstringPattern(const std::string_view s) const\n -> bool\n {\n const int sLen = s.length();\n const int halfsLen = sLen / 2;\n std::string sub;\n\n for (int i { 0 }; i < halfsLen; ++i)\n {\n sub += s[i];\n\n if (sLen % (i + 1))\n {\n continue;\n }\n\n std::string candidate;\n\n while (candidate.length() < sLen)\n {\n candidate.append(sub);\n }\n\n if (candidate == s)\n {\n return true;\n }\n }\n\n return false; \n }\n};", "memory": "90806" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n for (int i = 1; i <= n / 2; ++i) {\n if (n % i == 0) {\n string substring = s.substr(0, i);\n string repeated = \"\";\n for (int j = 0; j < n / i; ++j) {\n repeated += substring;\n }\n if (repeated == s) return true;\n }\n }\n return false;\n }\n};", "memory": "97278" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n \n // Check possible lengths for the repeated substring pattern\n for (int len = 1; len <= n / 2; ++len) {\n if (n % len == 0) { // Check if `len` divides `n`\n string sub = s.substr(0, len);\n string repeated = \"\";\n \n // Repeat the substring len times to reconstruct the original string\n for (int i = 0; i < n / len; ++i) {\n repeated += sub;\n }\n \n // If the constructed string matches the original, return true\n if (repeated == s) {\n return true;\n }\n }\n }\n \n // If no valid repeating pattern found, return false\n return false;\n }\n};\n", "memory": "97278" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n string str = \"\";\n for(int i=0; i<n; i++){\n if(n%(i+1)==0) str = s.substr(0,i+1);\n while(str.length()<n){\n str += s.substr(0,i+1);\n if(s==str) return true;\n }\n }\n return false;\n }\n};", "memory": "103751" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n string str = \"\";\n for(int i=0; i<n; i++){\n if(n%(i+1)==0){\n str = s.substr(0,i+1);\n while(str.length()<n){\n str += s.substr(0,i+1);\n if(s==str) return true;\n }\n }\n }\n\n return false;\n }\n};", "memory": "103751" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n string str = \"\";\n for(int i=0; i<n; i++){\n if(n%(i+1)==0) str = s.substr(0,i+1);\n while(str.length()<n){\n str += s.substr(0,i+1);\n if(s==str) return true;\n }\n }\n return false;\n }\n};", "memory": "110223" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(int i=1;i<=s.size()/2;i++){\n int len = i;\n if(s.size()%len == 0){\n unordered_map<string,int> mp;\n for(int j=0;j<s.size();j+=len){\n mp[s.substr(j,len)]++;\n }\n\n if(mp[s.substr(0,len)] == s.size()/len){\n return true;\n }\n } \n }\n\n return false;\n }\n};", "memory": "110223" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n if(s.length()==1) return false;\n char first=s.front();\n char last=s.back();\n int pos=s.find(last,0);\n if(pos==s.length()-1){// the pos found is the last one\n if(last==first){\n for(int i=0;i<=pos;i++){\n if(s[i]!=first) return false;\n }\n return true;\n }\n else return false;\n }\n while(pos!=string::npos){\n int p=pos;\n string sub=s.substr(0,pos+1);\n // cout<<sub<<endl;\n int lenSub=pos+1;\n while(sub==s.substr(pos+1,lenSub)){\n pos=pos+lenSub;\n if(pos==s.length()-1) return true;\n if(pos>=s.length()) break;\n }\n pos=s.find(last,p+1);\n }\n return false;\n }\n};", "memory": "116696" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n int i=1;\n while(i<=n/2){\n if(n%i == 0){\n string repeatedString = s.substr(0, i);\n \n for(int j=1; j<n/i; j++){\n repeatedString += s.substr(0,i);\n }\n \n if(repeatedString == s) return true;\n }\n i++;\n }\n\n return false;\n }\n};", "memory": "123168" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n if (n == 0) {\n return false;\n }\n\n for (int i = 1; i <= n/2; ++i) {\n if (n % i == 0) {\n stringstream pattern;\n for (int j = 0; j < n/i; ++j) {\n pattern << s.substr(0, i);\n }\n if (s == pattern.str()) {\n return true;\n }\n }\n }\n \n return false;\n }\n};", "memory": "149058" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n vector<int> index;\n for(int i=1; i<=s.size()/2; i++) {\n if(s[i] == s[0]) index.push_back(i);\n }\n\n int k = 0;\n for(int i=0; i<index.size(); i++) {\n int pattern_length = index[i] - k;\n string sub_string = s.substr(k, pattern_length);\n //cout<<sub_string<<endl;\n int j = pattern_length;\n while(j < s.size()) {\n string sub_string1 = s.substr(j, pattern_length);\n if(sub_string == sub_string1) {\n j += pattern_length;\n }else\n break;\n }\n if(j==s.size()) return true;\n }\n return false;\n }\n};\n // extract 'go' from 'iamgoodhere'\n// string s = \"iamgoodhere\";\n// int start = 3, end = 4;\n// cout<<s.substr(start,end-start+1); // go", "memory": "155531" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for (int k = 0; k < s.size() / 2; k++) {\n if (s[0] == s[k + 1]) {\n if (check(s, k + 1)) {\n return true;\n }\n }\n }\n return false;\n }\n bool check(string s, int len) {\n if (s.size() % len != 0) {\n return false;\n }\n for (int k = len; k < s.size(); k++) {\n if (s[k % len] != s[k]) {\n return false;\n }\n }\n return true;\n }\n};", "memory": "162003" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n vector<string> f(string s, int length){\n vector<string>ans;\n for(int i=0; i<s.length()-length+1; i+=length){\n ans.push_back(s.substr(i, length));\n }\n return ans;\n }\n \n bool check(vector<string> s){\n set<string>s1;\n for(string t:s){\n s1.insert(t);\n if(s1.size()>1) return false;\n }\n return true;\n }\n \n bool repeatedSubstringPattern(string s) {\n int n=s.length();\n for(int i=1; i<=n/2; i++){\n if(n%i==0){\n if(check(f(s,i))==true) return true;\n }\n }\n \n return false;\n }\n \n};", "memory": "168476" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool check(int size , string s){\n string temp = s.substr(0,size);\n for(int i = size ; i<s.size() ; i+=size){\n string t = s.substr(i,i+size);\n if(t.substr(0,size) != temp){\n return false;\n }\n }\n return true;\n }\n\n bool repeatedSubstringPattern(string s) {\n for(int i = 1 ; i<s.size();i++){\n if(s.size()%i == 0){\n bool ans = check(i,s);\n if(ans){\n return true;\n }\n //cout<<i<<endl;\n }\n }\n return false;\n }\n};", "memory": "174948" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n vector<string> f(string s, int length){\n vector<string>ans;\n for(int i=0; i<s.length()-length+1; i+=length){\n ans.push_back(s.substr(i, length));\n }\n return ans;\n }\n \n bool check(vector<string> s){\n set<string>s1;\n for(string t:s){\n s1.insert(t);\n }\n return s1.size()==1;\n }\n \n bool repeatedSubstringPattern(string s) {\n int n=s.length();\n for(int i=1; i<=n/2; i++){\n if(n%i==0){\n if(check(f(s,i))==true) return true;\n }\n }\n \n return false;\n }\n \n};", "memory": "181421" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n if(n == 1) return false;\n vector<char> tmp;\n char start = s[0], end = s[n - 1];\n //tmp = string(1,start);\n tmp.push_back(s[0]);\n if(start == end)\n {\n int flag = 1;\n for(int i = 0; i < n;i ++)\n {\n if(s[i] != start)\n flag = 0;\n }\n if(flag == 1) return true;\n }\n for(int i = 1; i < n / 2; i++)\n {\n if(s[i] != end) \n {\n tmp.push_back(s[i]);\n }\n else\n {\n string t;\n int len = i - 0 + 1;\n if(n % len != 0) continue;\n t.assign(s.begin(),s.begin()+i+1);\n int q = n/len;\n string f;\n while(q)\n {\n f = f + t;\n q--;\n }\n //string f = string(n/len,t);\n if(f == s) return true;\n }\n }\n return false;\n }\n};", "memory": "187893" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n vector<int> index;\n for(int i=1; i<s.size(); i++) {\n if(s[i] == s[0]) index.push_back(i);\n }\n\n int k = 0;\n for(int i=0; i<index.size(); i++) {\n int pattern_length = index[i] - k;\n string sub_string = s.substr(k, pattern_length);\n //cout<<sub_string<<endl;\n int j = pattern_length;\n while(j < s.size()) {\n string sub_string1 = s.substr(j, pattern_length);\n if(sub_string == sub_string1) {\n j += pattern_length;\n }else\n break;\n }\n if(j==s.size()) return true;\n }\n return false;\n }\n};\n // extract 'go' from 'iamgoodhere'\n// string s = \"iamgoodhere\";\n// int start = 3, end = 4;\n// cout<<s.substr(start,end-start+1); // go", "memory": "194366" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n char c = s[0];\n for(int i = 1; i <= n / 2; ++i){\n if(s[i] == c){\n string str = s.substr(0, i);\n string repeated = \"\";\n for(int j = 0; j < n / i; ++j){\n repeated += str;\n }\n if(repeated == s) return true;\n } \n }\n \n\n return false;\n }\n};", "memory": "200838" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n if (n % 2 == 0){\n if (s.substr(0,n/2) == s.substr(n/2,n)){\n return true;\n }\n }\n for (int i = 1; i <= n / 2; i++) {\n string sub = s.substr(0, i);\n if (n % i == 0) {\n string repeated = \"\";\n for (int j = 0; j < n / i; j++) {\n repeated += sub;\n }\n if (repeated == s) {\n return true;\n }\n }\n }\n return false;\n }\n};\n", "memory": "207311" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string ans;\n for(int i=(s.size()/2);i>0;i--){\n ans = s.substr(0,i);\n if(s.size()%i==0){\n int times=s.size()/i;\n string temp=\"\";\n while(times--){\n temp+=ans;\n }\n if(temp==s) return true;\n }\n }\n return false;\n }\n};", "memory": "213783" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n \n for(int l = n/2; l>=1;l--)\n {\n string newstr = \"\";\n string p = s.substr(0,l);\n if(n%l == 0){\n int times = n/l;\n while(times--)\n {\n newstr += p;\n }\n if(newstr == s)\n {\n return true;\n }\n }\n }\n return false;\n }\n};", "memory": "220256" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n\n int n=s.size();\n for(int l=n/2;l>=1;l--){\n\n string substring=s.substr(0,l);\n if(n%l==0){\n int times=(n/l);\n string newStr=\"\";\n while(times--){\n newStr+=substring;\n }\n\n if(newStr==s) return true;\n }\n\n }\n return false;\n \n }\n};", "memory": "226728" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(string &s,string &curr){\n int n=s.size();\n int m=curr.size();\n if(n%m!=0) return false;\n int times=n/m;\n string temp=\"\";\n while(times--){\n temp+=curr;\n }\n return temp==s;\n }\n bool repeatedSubstringPattern(string s) {\n string curr=\"\";\n for(int i=s.size()/2;i>=1;i--){\n curr=s.substr(0,i);\n if(solve(s,curr)==true) return true;\n }\n return false;\n }\n};", "memory": "233201" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int max_substr_len = s.length() / 2;\n if (max_substr_len < 1) return false;\n\n for (int len = max_substr_len; len >= 1; len--) {\n string sub_str = s.substr(0, len);\n int copy_count = (s.length() / len);\n if (copy_count * len == s.length()) {\n string copy_str = multiAppend(sub_str, copy_count);\n if (copy_str.compare(s) == 0) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n string multiAppend(string a, int count) {\n if (count == 0) {\n return \"\";\n }\n return a + multiAppend(a, count-1);\n }\n};", "memory": "239673" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(const std::string& s) {\n auto begin = s.begin(), end = s.end();\n auto it = s.begin();\n char final_char = s[s.size() - 1];\n\n while (it != end)\n {\n it = std::find(it, end, final_char);\n\n if (it == end - 1) break;\n\n std::string temp_str;\n\n while (temp_str.size() + 1 <= s.size())\n {\n temp_str += std::string(begin, it + 1);\n }\n\n if (temp_str == s)\n return true;\n\n ++it;\n }\n\n return false;\n}\n};", "memory": "246146" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(const std::string& s) {\n auto begin = s.begin(), end = s.end();\n auto it = s.begin();\n char final_char = s[s.size() - 1];\n\n while (it != end)\n {\n it = std::find(it, end, final_char);\n\n if (it == end - 1) break;\n\n std::string temp_str;\n\n while (temp_str.size() + 1 <= s.size())\n {\n temp_str += std::string(begin, it + 1);\n }\n\n if (temp_str == s)\n return true;\n\n ++it;\n }\n\n return false;\n}\n};", "memory": "246146" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int tot = s.length();\n int i = tot/2;\n int j = 2;\n int previous = 0;\n string x(\"\");\n std::ostringstream os;\n os.str(\"\");\n x = s.substr(0, 1);\n std::fill_n(std::ostream_iterator<std::string>(os), tot, x);\n if(os.str() == s && tot > 1) {\n return(true);\n }\n while(i > 0 && i != previous && i*j <= tot) {\n x = s.substr(0, i);\n os.str(\"\");\n while(os.str().length() < tot) {\n std::fill_n(std::ostream_iterator<std::string>(os), 1, x);\n }\n if(os.str() == s) {\n return(true);\n }\n j++;\n previous = i;\n i = tot / j;\n \n }\n return(false);\n }\n};", "memory": "252618" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n \n\n int l = s.length();\n\n string ch = \"\";\n\n for(int i = 0; i < l/2; i++)\n {\n ch += s[i];\n string temp = ch;\n\n int ch_len = ch.length();\n\n if(l % ch_len != 0) continue;\n\n while(ch.length() < l)\n {\n ch += temp;\n }\n\n if(ch == s) return true;\n\n ch = temp;\n }\n\n return false;\n\n \n }\n};", "memory": "259091" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n string st = \"\";\n for(int i = 0;i < s.size()/2; i++){\n string str = s.substr(0,i+1);\n int len = n / (i+1);\n if(n % len == 0)\n for(int k = 0;k < len; k++) st += str;\n if(st == s) return true;\n st = \"\";\n }\n return false;\n }\n};", "memory": "265563" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string t = s;\n string str = t.substr(0,1);\n t.erase(0, 1);\n // bool f = true;\n int i = 0;\n while (!t.empty() && str.length() <= s.length()/2 ) {\n if (t.find(str)==0) {\n i++;\n t.erase(0, str.length());\n cout << \"if \";\n }\n else {\n str = s.substr(0, str.length()+1);\n t = s;\n i = 0;\n cout << \"else \";\n }\n }\n if (i > 0)\n return true;\n else return 0;\n }\n};", "memory": "272036" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n int n_2 = n >> 1;\n\n for (int i = 1; i <= n_2; i++) {\n string sub_s = s.substr(0, i);\n int k = i;\n while (s.compare(k, i, sub_s) == 0) {\n k += i;\n if (k == n)\n return true;\n }\n\n }\n return false;\n }\n};", "memory": "278508" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": " class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n // if(s.size()&1)return 0;\n int n=s.size()/2;\n for(int i=0;i<s.size()/2;i++){\n string h=s.substr(0,i+1);\n int j=0;\n // cout<<h<<\" \";\n // cout<<h<<\" \";\n while(j<s.size()){\n if(s[j]==h[j%(i+1)])j++;\n else break;\n }\n if(j>=s.size() && j%(i+1)==0){return 1;}\n }\n return 0;\n }\n};", "memory": "284981" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n bool pattern(string &s, string &d){\n int n=s.length(); int m=d.length();\n if (n % m != 0) return false;\n for(int i=0; i<n; i=i+m){\n for(int j=0; j<m; j++){\n if(s[i+j]!=d[j]) return false;\n }\n }\n return true;\n }\npublic:\n bool repeatedSubstringPattern(string s) {\n int n=s.length();\n int i=0;\n // for(int i=0; i<n; i++){\n for(int j=1; j<=n/2; j++){\n string d=s.substr(i,j-i);\n if(pattern(s,d)) return true;\n }\n // }\n return false;\n }\n};", "memory": "291453" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": " class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n // if(s.size()&1)return 0;\n int n=s.size()/2;\n for(int i=0;i<s.size()/2;i++){\n string h=s.substr(0,i+1);\n int j=0;\n // cout<<h<<\" \";\n // cout<<h<<\" \";\n while(j<s.size()){\n if(s[j]==h[j%(i+1)])j++;\n else break;\n }\n if(j>=s.size() && j%(i+1)==0){return 1;}\n }\n return 0;\n }\n};", "memory": "297926" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int s_len = s.size();\n \n for ( int t_len = 1; t_len <= s_len/2; t_len++ ) { \n string substr = s.substr(0, t_len);\n if ( s_len % t_len == 0 ) {\n for ( int i = t_len; i + t_len - 1 < s_len; i += t_len ) {\n if ( s.substr(i, t_len) != substr ) {\n break;\n }\n if ( i + t_len == s_len ) {\n return true;\n } \n }\n }\n }\n return false;\n }\n};", "memory": "304398" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string pattern = \"\";\n for (int n = 1; n <= s.size() / 2; n++) {\n pattern = s.substr(0, n);\n if (s.size() % n != 0) {\n continue;\n }\n bool sucses = true;\n for (int i = n; i < s.size(); i += n) {\n if (s.substr(i, n) != pattern) {\n sucses = false;\n break;\n }\n }\n if (sucses) {\n return true;\n }\n }\n return false;\n }\n};", "memory": "310871" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n=s.length(); string a; bool var;\n for (int i=1; i<=n/2;++i){\n if (n%i==0){\n a =s.substr(0,i);\n var = true;\n }\n for (int j=i; j<n;j+=i){\n if (j+i<=n&&s.substr(j,i)!=a) {var=false;\n break;}\n }\n if (var) return true; \n }\n return false;\n }\n};", "memory": "317343" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int lenght = s.size();\n\tint subSize;\n\tstring res;\n\tfor (int i = 1; i <= lenght/2; i++){\n\t\tstring subString = s.substr(0,i);\n\t\tsubSize = subString.size();\n\t\tint repeat = lenght/subSize;\n\t\tres.clear();\n\t\tres.reserve(repeat * subString.size());\t\t\n\t\tfor (int x = 0; x < repeat; x++){\n\t\t\tres += subString;\n\t\t}\n\t\t\n if(res == s){\n \treturn true;\n\t}\n\t\t\n}\nreturn false;\n }\n};", "memory": "323816" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstring a[10001];\n bool repeatedSubstringPattern(string s) {\n int n=s.length(); string a; bool var;\n for (int i=1; i<=n/2;++i){\n if (n%i==0){\n a =s.substr(0,i);\n var = true;\n }\n for (int j=i; j<n;j+=i){\n if (j+i<=n&&s.substr(j,i)!=a) {var=false;\n break;}\n }\n if (var) return true; \n }\n return false;\n }\n};", "memory": "330288" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size();\n for(int l=n/2; l>=1; l--){\n string newString =\"\";\n string pattern = s.substr(0,l);\n int times = n/l;\n while(times--){\n newString += pattern;\n }\n if(newString == s){\n return true;\n }\n }\n return false;\n }\n};", "memory": "336761" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n\n for(int l=n/2;l>=1;l--)\n {\n int times = n/l;\n string pattern = s.substr(0,l);\n string newStr = \"\";\n\n while(times--)\n {\n newStr += pattern;\n } \n\n if(newStr==s)\n {\n return true;\n }\n } \n return false;\n }\n};", "memory": "343233" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n\n for(int l=n/2;l>=1;l--)\n {\n int times = n/l;\n string pattern = s.substr(0,l);\n string newStr = \"\";\n\n while(times--)\n {\n newStr += pattern;\n } \n\n if(newStr==s)\n {\n return true;\n }\n } \n return false;\n }\n\n // Time Complexity = o(n*root(n));\n};", "memory": "343233" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(int i = 0; i < s.size()/2; i++) {\n string sub = s.substr(0,i+1);\n int subSize = sub.size();\n if(s.size()%subSize > 0) continue;\n int num = s.size()/subSize;\n int idx = 0;\n bool matches = true;\n for(int i = 1; i <= num; i++) {\n if(s.substr(idx, subSize) != sub) {\n matches = false;\n break;\n }\n idx += subSize;\n }\n if(matches) return true;\n }\n return false;\n }\n};", "memory": "369123" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(int i = 0; i < s.size()/2; i++) {\n string sub = s.substr(0,i+1);\n int subSize = sub.size();\n if(s.size()%subSize > 0) continue;\n int num = s.size()/subSize;\n int idx = 0;\n bool matches = true;\n for(int i = 1; i <= num; i++) {\n if(s.substr(idx, subSize) != sub) {\n matches = false;\n break;\n }\n idx += subSize;\n }\n if(matches) return true;\n }\n return false;\n }\n};", "memory": "369123" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(int i = 0; i < s.size()/2; i++) {\n string sub = s.substr(0,i+1);\n int subSize = sub.size();\n if(s.size()%subSize > 0) continue;\n int num = s.size()/subSize;\n int idx = 0;\n bool matches = true;\n for(int i = 1; i <= num; i++) {\n if(s.substr(idx, subSize) != sub) {\n matches = false;\n break;\n }\n idx += subSize;\n }\n if(matches) return true;\n }\n return false;\n }\n};", "memory": "375596" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.size()/2;\n string subString;\n for(int i = 0; i<n; i++){\n subString = s.substr(0, i+1);\n \n if(s.size() % subString.size() == 0){\n string temp = subString;\n \n while(subString.size()<s.size()){\n subString += temp;\n \n }\n if(subString == s) return true;\n }\n }\n return false;\n }\n};", "memory": "382068" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string repeat(string str, int count){\n string result = \"\";\n for (int i = 0; i<count; i++){\n result += str;\n }\n return result;\n }\n bool repeatedSubstringPattern(string s) {\n unordered_map<string, int> store = {};\n string substring;\n int num_repeats;\n\n for (int j = 0; j<=s.size() / 2; j++){\n // store[s.substr(0, i)]++;\n substring = s.substr(0, j+1);\n if (s.size() % substring.size() == 0){\n num_repeats = s.size() / substring.size();\n if (num_repeats != 1 && repeat(substring, num_repeats) == s){ \n return true;\n }\n }\n }\n return false;\n\n }\n};", "memory": "388541" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n if (s.length() <= 1) return false; \n int n = s.length();\n for (int i = 1; i <= (n+1)/2; i++) {\n string tmp = s.substr(0, i);\n if (n % i != 0) continue;\n string tmp2 = tmp;\n while (tmp2 != s) {\n tmp2 += tmp;\n if (tmp2.length() > s.length()) break;\n }\n if(tmp2 == s) return true;\n }\n return false;\n }\n \n};", "memory": "388541" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string front = \"\",\n back = \"\",\n pattern = \"\";\n int total = 0,\n n = s.size(),\n curr = 0;\n unordered_map<string,int> mp;\n for(int i=0; i<n; i++){\n if(i>= n-1-i) break;\n front += s[i];\n back = s[n-1-i]+ back;\n if(front== back){\n total = i+1;\n curr = total;\n pattern = front;\n //break;\n }\n }\n if(pattern.size()< 1) return false;\n front = \"\";\n for(int j=pattern.size(); j<n; j++){\n front += s[j];\n if(front.size()== pattern.size()){\n if(front== pattern) front = \"\";\n else return false;\n }\n }\n return true;\n }\n};", "memory": "395013" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nint n;\n string makestring(string str,int times){\n string ans=\"\";\n for (int i = 0; i < times; ++i) {\n ans += str;\n }\n return ans;\n }\n bool repeatedSubstringPattern(string s) {\n n=s.size();\n for(int i=0;i<n/2;i++){\n string str=s.substr(0,i+1);\n if(s.size()%str.size()==0){\n int times=s.size()/str.size();\n string appended=makestring(str,times);\n if(appended==s){\n return true;\n }\n }\n \n } \n return false;\n }\n};", "memory": "395013" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string sub = \"\";\n sub += s[0];\n // 0 1 2 3\n // 4 - 1 = 3\n // 4 -2 = 2\n for (int i = 1; i <= s.size() - sub.size(); i++) {\n // cout << \"i: \" << i << endl;\n // cout << \"sub: \" << sub << endl;\n // cout << \"s.substr: \" << s.substr(i, sub.size()) << endl;\n if (sub == s.substr(i, sub.size())) {\n if (i == s.size() - sub.size())\n return true;\n\n i += sub.size() - 1;\n } else {\n sub = s.substr(0, sub.size() + 1);\n i = sub.size() - 1;\n }\n }\n return false;\n }\n};", "memory": "401486" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(int size=1;size<=s.size()/2;size++){\n string temp=s.substr(0, size);\n bool f=true;\n for(int i=size;i<=s.size()-size;i+=size){\n if(temp!=s.substr(i, size)){\n f=false;\n break;\n }\n }\n if(f) return !(s.size()%size);\n }\n return false;\n }\n};", "memory": "401486" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n \tint size = s.length();\n\tif (size == 1)\n\t\treturn false;\n\tif (size == 2)\n\t\treturn(s[0] == s[1]);\n\tif (size == 3)\n\t\treturn(s[0] == s[1] && s[1] == s[2]);\n\tvector<string>container;\n\tfor (int i = 0, j = size - 1; i < j; i++, j--) {\n\t\tstring left = s.substr(0, i + 1);\n\t\tstring right = s.substr(j, size - j);\n\t\tif (left == right)\n\t\t\tcontainer.push_back(left);\n\t}\n\tif (container.empty())\n\t\treturn false;\n\tint cSize = container.size();\n\tint result = false;\n\tfor (int i = 0; i < cSize; i++) {\n\t\tstring current = container[i];\n\t\tstring temp = \"\";\n\t\tint currentSize = current.length();\n\t\tint time = size / currentSize;\n\t\tfor (int j = 0; j < time; j++) {\n\t\t\ttemp += current;\n\t\t}\n\t\tif (temp == s) {\n\t\t\tresult = true;\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn result; \n }\n};", "memory": "407958" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n bool f=false;\n int len=s.size(),siz;\n unordered_map<string,int> mp;\n string n=\"\";\n for(int i=0;i<s.size()-1;i++){\n n+=s[i];\n //cout<<n<<endl;\n siz=n.size();\n int l=len/siz;\n string j=n;\n for(int i=0;i<l-1;i++){\n j+=n;\n }\n if(j==s) f=true;\n }\n return f;\n }\n};", "memory": "407958" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isDiv(string s, string div){\n int n = s.length(), m = div.length();\n while(s.size()){\n string part = s.substr(0,m);\n if(part != div)\n return false;\n s = s.substr(m);\n }\n return true;\n }\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n string div;\n for(int i=0;i<n-1;i++){\n div += s[i];\n if(n%(i+1)) continue;\n if(isDiv(s, div))\n return true;\n }\n return false;\n }\n};", "memory": "414431" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n std::list<std::string> substrings;\n std::string substring = \"\";\n for (char c : s) {\n substring.push_back(c);\n substrings.push_back(substring);\n if (substring.size() * 2 > s.size()) {\n break;\n }\n }\n substring = \"\";\n size_t point = 0;\n size_t repeat_count = 0;\n for (std::string ss : substrings) {\n repeat_count = 0;\n for (size_t i = 0; i < s.size(); i++) {\n if (ss[point] != s[i]) {\n point = 0;\n repeat_count = 0;\n break;\n }\n point++;\n if (point == ss.size()) {\n point = 0;\n repeat_count++;\n }\n }\n if (repeat_count > 1 && point == 0) {\n return true;\n }\n }\n return false;\n }\n};", "memory": "414431" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n string str;\n string str2;\n string dup=s;\n int count=0;\n\n for(int i=0;i<(s.length()/2);i++){\n str+=s[i];\n dup=s;\n count=0;\n int prev=-1;\n if(s.length()%(i+1)==0){\n for(int j=0;j<(s.length()/str.length());j++){\n int ind = dup.find(str);\n if(ind != string::npos){\n count++;\n dup = dup.substr(ind+str.length());\n prev=ind;\n }\n if(ind>=1&&ind != prev+str.length()){\n break;\n }\n }\n if(count*str.length()==s.length()){\n return true;\n }\n }\n }\n return false;\n }\n};", "memory": "420903" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n for(auto i = 0; i < s.size()-1; i++){\n if(s.size() % (i+1) == 0){\n string sub = s.substr(0, i+1); \n string compare = sub;\n while(compare.size() < s.size()) compare = compare + s.substr(0, i+1);\n if (compare == s) return true;\n }\n }\n return false;\n }\n};", "memory": "420903" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int n = s.length();\n for(int i=0;i<n/2;i++){\n string str = s.substr(0,i+1);\n int x = n/(i+1);\n string s1 = \"\";\n while(x--){\n s1+=str;\n }\n if(s1==s) return true;\n }\n\n return false;\n }\n};", "memory": "427376" }
459
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;ab&quot; twice. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aba&quot; <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcabcabcabc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> It is the substring &quot;abc&quot; four times or the substring &quot;abcabc&quot; twice. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool repeatedSubstringPattern(string s) {\n int len=s.size();\n for(int i=0;i<len/2;i++)\n {\n string str=s.substr(0,i+1);\n int kitne_bar_append_karu=len/(i+1);\n string result;\n while(kitne_bar_append_karu--)\n {\n result+=str;\n }\n // cout<<result<<endl;\n if(result==s)\n {\n return true;\n }\n }\n return false;\n }\n};", "memory": "427376" }