id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trie{\npublic:\n int flag;\n unordered_map<char, Trie*>child;\n // vector<Trie*>child;\n Trie(){\n flag = -1;\n }\n};\nclass Solution {\npublic:\n static bool vec_sort(string& a, string& b){\n return a.length() < b.length();\n }\n void go(Trie* node, Trie* root, string& s, int spot, vector<string>& ans, int depth){\n // int num = s[spot] - 'a';\n // Trie* tempnode = node->child[num];\n // if(tempnode->flag == -1){\n if(node->child.count(s[spot]) == 0){\n if(depth != spot)return;\n // for(int t=0;t<26;t++){\n // Trie* temppush = new Trie();\n // tempnode->child.push_back(temppush);\n // }\n node->child[s[spot]] = new Trie();\n if(spot == s.length() - 1){\n node->child[s[spot]]->flag = 1;\n // tempnode->flag = 1;\n }\n else{\n node->child[s[spot]]->flag = 0;\n // tempnode->flag = 0;\n // go(tempnode, root, s, spot + 1, ans, depth + 1);\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n // else if(tempnode->flag == 1){\n else if(node->child[s[spot]]->flag == 1){\n int pre_size = ans.size();\n if(spot == s.length() - 1)ans.push_back(s);\n else{\n go(root, root, s, spot + 1, ans, 0);\n if(pre_size == ans.size()){\n // go(tempnode, root, s, spot + 1, ans, depth + 1);\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n }\n else{\n if(s.length() - 1 == spot)return;\n // go(tempnode, root, s, spot + 1, ans, depth + 1);\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n sort(words.begin(), words.end(), vec_sort);\n Trie* root = new Trie();\n // for(int t=0;t<26;t++){\n // Trie* temppush = new Trie();\n // root->child.push_back(temppush);\n // }\n vector<string>ans;\n for(auto& s:words){\n // cout<<\"\\\"\"<<s<<\"\\\"\"<<\",\";\n go(root, root, s, 0, ans, 0);\n }\n return ans;\n }\n};", "memory": "204523" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trie{\npublic:\n int flag;\n unordered_map<char, Trie*>child;\n Trie(){\n flag = -1;\n }\n};\nclass Solution {\npublic:\n void go(Trie* node, Trie* root, string& s, int spot, vector<string>& ans, int depth){\n if(node->child.count(s[spot]) == 0){\n if(depth != spot)return;\n node->child[s[spot]] = new Trie();\n if(spot == s.length() - 1){\n node->child[s[spot]]->flag = 1;\n }\n else{\n node->child[s[spot]]->flag = 0;\n go(node->child[s[spot]], root, s, spot + 1, ans, depth + 1);\n }\n }\n else{\n Trie* tempnode = node->child[s[spot]];\n if(tempnode->flag == 1){\n int pre_size = ans.size();\n if(spot == s.length() - 1)ans.push_back(s);\n else{\n go(root, root, s, spot + 1, ans, 0);\n if(pre_size == ans.size()){\n go(tempnode, root, s, spot + 1, ans, depth + 1);\n }\n }\n }\n else{\n if(s.length() - 1 == spot)return;\n go(tempnode, root, s, spot + 1, ans, depth + 1);\n }\n }\n \n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n // sort(words.begin(), words.end(), vec_sort);\n Trie* root = new Trie();\n vector<string>ans;\n int len = words.size();\n for(int t=1;t<=30;t++){\n for(int s=0;s<len;s++){\n if(words[s].length() == t)go(root, root, words[s], 0, ans, 0);\n }\n }\n return ans;\n }\n};", "memory": "204523" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct node {\n\n bool is_end;\n\n unordered_map<char, node*> mp;\n\n node(): is_end(false) {}\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& arr) {\n \n vector<string> res;\n \n node* root = new node();\n\n sort( arr.begin(), arr.end(), []( const string & ls, const string & rs)->bool {\n return ls.length() < rs.length();\n });\n\n function<void( node*, int, string &)> insert;\n\n function<void( node*, int, string &, vector<bool> &)> match;\n\n insert = [&]( node* nodeptr, int pos, string & s)->void {\n\n bool & is_end = nodeptr->is_end;\n unordered_map<char,node*> & mp = nodeptr->mp;\n\n if(pos>=s.length()) {\n is_end = true;\n return;\n } else {\n if(mp.find(s[pos])==mp.end()) {\n mp[s[pos]] = new node();\n }\n insert( mp[s[pos]], pos+1, s);\n }\n };\n\n match = [&]( node* nodeptr, int pos, string & s, vector<bool> & v)->void {\n if(pos>=s.length()) {\n return;\n } else {\n unordered_map<char,node*> & mp = nodeptr->mp;\n if(mp.find(s[pos])==mp.end()) {\n return;\n }\n nodeptr = mp[s[pos]];\n bool & is_end = nodeptr->is_end;\n if(is_end) {\n v[pos] = true;\n }\n match( nodeptr, pos+1, s, v);\n }\n };\n\n for( auto & s: arr) {\n int m = s.length();\n vector<bool> v( m, false);\n for( int i=0; i<m; i++) {\n if((i==0) || (v[i-1])) {\n match( root, i, s, v);\n }\n }\n insert( root, 0, s);\n if(v.back()) {\n res.push_back(s);\n }\n }\n\n return res;\n }\n};", "memory": "207666" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class TrieNode {\npublic:\n bool isEnd;\n unordered_map<char, TrieNode*> children;\n \n TrieNode() : isEnd(false) {}\n};\n\nclass Trie {\npublic:\n TrieNode* root;\n \n Trie() {\n root = new TrieNode();\n }\n \n void insert(const string& word) {\n TrieNode* node = root;\n for (char c : word) {\n if (!node->children.count(c)) {\n node->children[c] = new TrieNode();\n }\n node = node->children[c];\n }\n node->isEnd = true;\n }\n \n bool search(const string& word, int start, int count, unordered_map<int, bool>& memo) {\n if (start == word.size()) {\n return count > 1;\n }\n if (memo.count(start)) return memo[start];\n \n TrieNode* node = root;\n for (int i = start; i < word.size(); ++i) {\n if (!node->children.count(word[i])) {\n break;\n }\n node = node->children[word[i]];\n if (node->isEnd && search(word, i + 1, count + 1, memo)) {\n return memo[start] = true;\n }\n }\n return memo[start] = false;\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie trie;\n vector<string> result;\n unordered_map<int, bool> memo; // Memoization map\n \n for (const string& word : words) {\n if (!word.empty()) {\n trie.insert(word);\n }\n }\n \n for (const string& word : words) {\n if (!word.empty()) {\n memo.clear(); // Clear memoization for each new word\n if (trie.search(word, 0, 0, memo)) {\n result.push_back(word);\n }\n }\n }\n \n return result;\n }\n};\n", "memory": "210808" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct TrieNode{\n TrieNode(bool word = false)\n {\n isWord = word;\n }\n\n unordered_map<char, TrieNode*> children;\n bool isWord;\n};\n\nclass Trie{\n private:\n TrieNode* root;\n\n void addWord(string word)\n {\n TrieNode* node = root;\n for (int i = 0; i < word.length(); i ++)\n {\n if (node->children.find(word[i]) == node->children.end()) node->children[word[i]] = new TrieNode();\n node = node->children[word[i]];\n }\n\n node->isWord = true;\n }\n\n public:\n Trie(vector<string>& words)\n {\n root = new TrieNode();\n for (auto &word: words)\n {\n addWord(word);\n }\n }\n\n bool isConcatenated(string& curWord, int s, int count, int n, vector<int>& dp)\n {\n if (s == n) return count > 1;\n if (dp[s] != -1) return dp[s];\n TrieNode* node = root;\n for (int i = s; i < n; i ++)\n {\n if (node->children.find(curWord[i]) == node->children.end()) break;\n node = node->children[curWord[i]];\n if (node->isWord){\n bool isConcat = isConcatenated(curWord, i+1, count+1, n, dp);\n if (isConcat) {\n return dp[s] = 1;\n } \n }\n }\n\n return dp[s] = 0;\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie trie(words);\n vector<string> result;\n for (auto& word: words)\n {\n vector<int> dp(word.length(), -1);\n if (trie.isConcatenated(word, 0, 0, word.length(), dp)) result.push_back(word);\n }\n\n return result;\n \n }\n\n\n};", "memory": "213951" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class TrieNode{\n public:\n unordered_map<char ,TrieNode*> mp;\n bool end;\n TrieNode (){\n end=false;\n }\n };\n\n class Trie{\n public:\n TrieNode * trie;\n int cnt;\n Trie(){\n trie=new TrieNode();\n cnt=0;\n }\n void insert(string s){\n TrieNode *trieNode=trie;\n for(int i=0;i<(int)s.size();i++){\n if(trieNode->mp.find(s[i])==trieNode->mp.end()){\n trieNode->mp[s[i]]=new TrieNode();\n cnt++;\n // cout<<s[i]<<\" \";\n }\n trieNode=trieNode->mp[s[i]];\n }\n //cout<<s[(int)s.size()-1]<<endl;\n trieNode->end=true;\n }\n };\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie* trie= new Trie();\n for(int i=0;i<(int)words.size();i++){\n trie->insert(words[i]);\n }\n //cout<<trie->cnt<<endl;\n vector<int>dp;\n function<int (int ,int ,string )> helper=[&](int i,int c,string s){\n if(i>=(int)s.size())return c>=2?1:0;\n if(dp[i]!=-1)return dp[i];\n TrieNode *tempTrie=trie->trie;\n for(int j=i;j<(int)s.size();j++){\n //cout<<j<<\" \"<<s[j]<<\" \";\n if(tempTrie->mp.find(s[j])==tempTrie->mp.end()){\n return dp[i]=0;\n }\n tempTrie=tempTrie->mp[s[j]];\n if(tempTrie->end==true){\n //cout<<s[j]<<\" came here \"<<endl;\n dp[i]=helper(j+1,c+1,s)?1:0;\n if(dp[i])return dp[i];\n }\n }\n // cout<<endl;\n return dp[i]=0;\n };\n vector<string>res;\n for(int i=0;i<(int)words.size();i++){\n dp=vector<int>(words[i].size(),-1);\n if(helper(0,0,words[i]))res.push_back(words[i]);\n }\n return res;\n }\n};", "memory": "217093" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class TrieNode{\n public:\n unordered_map<char ,TrieNode*> mp;\n bool end;\n TrieNode (){\n end=false;\n }\n };\n\n class Trie{\n public:\n TrieNode * trie;\n int cnt;\n Trie(){\n trie=new TrieNode();\n cnt=0;\n }\n void insert(string s){\n TrieNode *trieNode=trie;\n for(int i=0;i<(int)s.size();i++){\n if(trieNode->mp.find(s[i])==trieNode->mp.end()){\n trieNode->mp[s[i]]=new TrieNode();\n cnt++;\n // cout<<s[i]<<\" \";\n }\n trieNode=trieNode->mp[s[i]];\n }\n //cout<<s[(int)s.size()-1]<<endl;\n trieNode->end=true;\n }\n };\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie* trie= new Trie();\n for(int i=0;i<(int)words.size();i++){\n trie->insert(words[i]);\n }\n //cout<<trie->cnt<<endl;\n vector<int>dp;\n function<int (int ,int ,string )> helper=[&](int i,int c,string s){\n if(i>=(int)s.size())return c>=2?1:0;\n if(dp[i]!=-1)return dp[i];\n TrieNode *tempTrie=trie->trie;\n for(int j=i;j<(int)s.size();j++){\n //cout<<j<<\" \"<<s[j]<<\" \";\n if(tempTrie->mp.find(s[j])==tempTrie->mp.end()){\n return dp[i]=0;\n }\n tempTrie=tempTrie->mp[s[j]];\n if(tempTrie->end==true){\n //cout<<s[j]<<\" came here \"<<endl;\n dp[i]=helper(j+1,c+1,s)?1:0;\n if(dp[i])return dp[i];\n }\n }\n // cout<<endl;\n return dp[i]=0;\n };\n vector<string>res;\n for(int i=0;i<(int)words.size();i++){\n dp=vector<int>(words[i].size(),-1);\n if(helper(0,0,words[i]))res.push_back(words[i]);\n }\n return res;\n }\n};", "memory": "217093" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool tell(string& curwrd,set<string>& st,int indx,string tillnow,map<pair<string,int>,bool>& mp)\n {\n int n=curwrd.size();\n if(indx==n)\n {\n if(st.find(tillnow)!=st.end()||tillnow==\"\")\n return true;\n return false;\n }\n if(mp.find({tillnow,indx})!=mp.end())\n return mp[{tillnow,indx}]; \n string inswrd=tillnow+curwrd[indx];\n bool ans=false;\n if(st.find(inswrd)!=st.end())\n ans|=tell(curwrd,st,indx+1,\"\",mp);\n ans|=tell(curwrd,st,indx+1,inswrd,mp);\n return mp[{tillnow,indx}]=ans;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n set<string> st;\n for(string str:words)\n st.insert(str);\n vector<string> ans;\n for(string str:words)\n {\n st.erase(str);\n map<pair<string,int>,bool> mp;\n if(tell(str,st,0,\"\",mp))\n ans.push_back(str);\n st.insert(str);\n }\n return ans; \n }\n};", "memory": "220236" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<string, bool> memo;\n\n bool concatWord(const string& w, const unordered_set<string>& dict) {\n auto it = memo.find(w);\n if (it != memo.end())\n return memo[w];\n\n for (int i = 1; i < w.size(); ++i) {\n string prefix = w.substr(0, i);\n string suffix = w.substr(i, w.size() - i);\n if (dict.count(prefix) == 0)\n continue;\n\n if (dict.count(suffix) > 0) {\n memo[w] = true;\n return true;\n }\n\n if (concatWord(suffix, dict)) {\n memo[w] = true;\n return true;\n }\n }\n\n memo[w] = false;\n return false;\n }\n\n // 24.08.2024\n vector<string> findAllConcatenatedWordsInADict1(vector<string>& words) {\n unordered_set<string> dict;\n\n for (auto w: words)\n dict.insert(w);\n\n vector<string> answer;\n\n for (auto w: words) {\n if (concatWord(w, dict))\n answer.push_back(w);\n }\n\n return answer;\n }\n\n struct TreeNode {\n unordered_map<char, TreeNode*> nodes;\n bool endOfWord = false;\n } root;\n\n\n void addWord(TreeNode* root, const string& word) {\n TreeNode* node = root;\n for (char c: word) {\n if (node->nodes.find(c) == node->nodes.end())\n node->nodes[c] = new TreeNode();\n \n node = node->nodes[c];\n }\n\n node->endOfWord = true;\n }\n\n void createTrie(vector<string>& words) {\n for (auto& w: words) {\n addWord(&root, w);\n }\n }\n\n unordered_map<string, int> memo2;\n\n int searchConCat(const string& word, int pos) {\n string wrd = word.substr(pos);\n if (memo2.find(wrd) != memo2.end())\n return memo2[wrd];\n\n if (pos == word.size())\n return 0;\n\n int found = 0;\n TreeNode* node = &root;\n for (int i = pos; i < word.size(); ++i) {\n if (node->nodes.find(word[i]) == node->nodes.end())\n break;\n\n char c = word[i];\n node = node->nodes[c];\n if (node->endOfWord) {\n int prefix_found = searchConCat(word, i + 1);\n if (prefix_found >= 0) {\n found = 1 + prefix_found;\n break;\n }\n }\n }\n\n memo2[wrd] = (found == 0) ? -1 : found;\n \n return memo2[wrd];\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n createTrie(words);\n\n vector<string> answer;\n\n for (auto w: words) {\n if (searchConCat(w, 0) > 1)\n answer.push_back(w);\n }\n\n return answer;\n }\n\n};", "memory": "223378" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct Node{\n Node* ch[26];\n bool end;\n};\nclass Trie{\npublic:\n Node* root;\n Trie(){\n root= new Node();\n }\n void insert(string s){\n Node* temp= root;\n for(auto c: s){\n int t= c-'a';\n if(temp->ch[t]==NULL) temp->ch[t]= new Node();\n temp= temp->ch[t];\n }\n temp->end=true;\n }\n\n bool search(Node* temp, int idx, int cnt, string &word){\n if(temp==NULL) return false;\n if(idx==word.size()) return cnt>=1 && temp->end;\n \n if(temp->end){\n if(search(root, idx, cnt+1, word)) return true;\n }\n return search(temp->ch[word[idx]-'a'], idx+1, cnt, word);\n }\n};\n\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie trie;\n for(auto &s: words) reverse(s.begin(), s.end());\n \n for(auto w: words) {\n trie.insert(w);\n }\n vector<string> ans;\n for(auto w: words){\n if(trie.search(trie.root, 0, 0, w)) {\n reverse(w.begin(), w.end());\n ans.push_back(w);\n }\n }\n \n return ans;\n }\n};", "memory": "226521" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n struct TrieNode {\n TrieNode* children[26];\n bool isEnd;\n TrieNode() : isEnd(false) {\n for (int i = 0; i < 26; i++) {\n children[i] = nullptr;\n }\n }\n };\n\n TrieNode* root;\n\n void insert(const string& word) {\n TrieNode* node = root;\n for (char c : word) {\n int index = c - 'a';\n if (!node->children[index]) {\n node->children[index] = new TrieNode();\n }\n node = node->children[index];\n }\n node->isEnd = true;\n }\n\n bool isConcatenated(const string& word, int start, int count) {\n if (start == word.length() && count > 1) return true;\n \n TrieNode* node = root;\n for (int i = start; i < word.length(); i++) {\n int index = word[i] - 'a';\n if (!node->children[index]) return false;\n node = node->children[index];\n if (node->isEnd) {\n if (isConcatenated(word, i + 1, count + 1)) return true;\n }\n }\n return false;\n }\n\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n root = new TrieNode();\n vector<string> result;\n\n // Sort words by length\n sort(words.begin(), words.end(), [](const string& a, const string& b) {\n return a.length() < b.length();\n });\n\n for (const string& word : words) {\n if (word.empty()) continue;\n if (isConcatenated(word, 0, 0)) {\n result.push_back(word);\n } else {\n insert(word);\n }\n }\n\n return result;\n }\n};", "memory": "226521" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trie {\n Trie *arr[26];\n bool end = false;\n\n public:\n Trie() {\n for (int i = 0; i < 26; i++)\n arr[i] = nullptr;\n }\n\n void insert(const string &s, Trie *t) {\n for (const char &c : s) {\n const int k = c - 'a';\n if (t->arr[k] == nullptr)\n t->arr[k] = new Trie();\n\n t = t->arr[k];\n }\n\n t->end = true;\n }\n\n bool altFind(const string &s, Trie *t, const int pos) {\n if (pos == s.size())\n return true;\n\n Trie *tt = t;\n for (int i = pos; i < s.size(); i++) {\n const int k = s[i] - 'a';\n\n if (t->arr[k] == nullptr)\n return false;\n\n t = t->arr[k];\n\n if (t->end == true) {\n if (altFind(s, tt, i + 1))\n return true;\n }\n }\n\n return t->end == true;\n }\n\n void addEnd(const string &s, Trie *t) {\n for (const char &c : s)\n t = t->arr[c - 'a'];\n\n t->end = true;\n }\n\n void removeEnd(const string &s, Trie *t) {\n for (const char &c : s)\n t = t->arr[c - 'a'];\n\n t->end = false;\n }\n};\n\nclass Solution {\npublic:\n vector<string>\n findAllConcatenatedWordsInADict(const vector<string>& words) {\n vector<string> results;\n Trie* t = new Trie;\n for (const string& s : words)\n t->insert(s, t);\n\n for (const string& s : words) {\n if(s.size() > 13 && s[0] == s[1] && s[s.size()-1] == 'z') continue;\n\n t->removeEnd(s, t);\n if (t->altFind(s, t, 0))\n results.push_back(s);\n t->addEnd(s, t);\n }\n\n return results;\n }\n};", "memory": "229663" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trie {\n Trie *arr[26];\n bool end = false;\n\n public:\n Trie() {\n for (int i = 0; i < 26; i++)\n arr[i] = nullptr;\n }\n\n void insert(const string &s, Trie *t) {\n for (const char &c : s) {\n const int k = c - 'a';\n if (t->arr[k] == nullptr)\n t->arr[k] = new Trie();\n\n t = t->arr[k];\n }\n\n t->end = true;\n }\n\n bool altFind(const string &s, Trie *t, const int pos) {\n if (pos == s.size())\n return true;\n\n Trie *tt = t;\n for (int i = pos; i < s.size(); i++) {\n const int k = s[i] - 'a';\n\n if (t->arr[k] == nullptr)\n return false;\n\n t = t->arr[k];\n\n if (t->end == true) {\n if (altFind(s, tt, i + 1))\n return true;\n }\n }\n\n return t->end == true;\n }\n\n void addEnd(const string &s, Trie *t) {\n for (const char &c : s)\n t = t->arr[c - 'a'];\n\n t->end = true;\n }\n\n void removeEnd(const string &s, Trie *t) {\n for (const char &c : s)\n t = t->arr[c - 'a'];\n\n t->end = false;\n }\n};\n\nclass Solution {\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n vector<string>\n findAllConcatenatedWordsInADict(const vector<string>& words) {\n vector<string> results;\n Trie* t = new Trie;\n for (const string& s : words)\n t->insert(s, t);\n\n for (const string& s : words) {\n if(s.size() > 17 && s[0] == s[1] && s[s.size()-1] == 'z') continue;\n\n t->removeEnd(s, t);\n if (t->altFind(s, t, 0))\n results.push_back(s);\n t->addEnd(s, t);\n }\n\n return results;\n }\n};", "memory": "229663" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct Node{\n bool end;\n Node *nxt[26];\n Node(){\n end = false;\n memset(nxt, 0, sizeof nxt);\n }\n }*root;\n void insert(string &s){\n Node *cur = root;\n for(int i =0; i < s.size(); i++){\n if(!cur->nxt[s[i]-'a']) cur->nxt[s[i]-'a'] = new Node();\n cur = cur->nxt[s[i]-'a'];\n }\n cur->end = true;\n }\n\n bool search(string &s){\n Node *cur = root;\n for(int i =0; i < s.size(); i++){\n if(!cur->nxt[s[i]-'a'])return false;\n cur = cur->nxt[s[i]-'a'];\n }\n if(cur->end)return true;\n else return false;\n }\n\n\n vector<string> ans;\n unordered_map<int, int> mp;\n int dp[10005][35];\n void solve(int idx, int cnt, string &s, int idxOfArray){\n if(idx >= s.size()){\n if(cnt > 1 && mp.find(idxOfArray) == mp.end()) {\n ans.push_back(s);\n mp[idxOfArray]++;\n }\n return;\n }\n if(dp[idxOfArray][idx]) return;\n for(int i = idx; i < s.size(); i++){\n string tmp = s.substr(idx, (i - idx) + 1);\n if(search(tmp)){\n dp[idxOfArray][i] = 1;\n solve(i+1,cnt+1, s, idxOfArray);\n }\n }\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n memset(dp,0,sizeof dp);\n root = new Node();\n for(int i =0; i < words.size(); i++){\n insert(words[i]);\n }\n for(int i =0; i < words.size(); i++){\n solve(0,0,words[i], i);\n }\n return ans;\n }\n};", "memory": "232806" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trie {\n public:\n Trie() noexcept = default;\n\n void add(const string& s) {\n Trie* current = this;\n for(const auto c : s) {\n auto& ptr = current->get(c);\n if(!ptr) {\n ptr = std::make_unique<Trie>();\n }\n current = ptr.get();\n }\n current->end(true);\n }\n\n constexpr const Trie* get(const char c) const noexcept {\n return m_letters[c-'a'].get();\n }\n\n constexpr bool end() const noexcept {\n return m_end;\n }\n\n private:\n constexpr std::unique_ptr<Trie>& get(const char c) noexcept {\n return m_letters[c-'a'];\n }\n constexpr void end(const bool e) noexcept {\n m_end = e;\n }\n\n bool m_end = false;\n std::array<std::unique_ptr<Trie>, 26> m_letters;\n};\nclass Solution {\npublic:\n constexpr bool search(const Trie* node, const Trie* root, const string& word, const int index) const noexcept {\n auto t = node->get(word[index]);\n for(auto i = index+1; i < word.size(); ++i) {\n if(t == nullptr) {\n return false;\n }\n else if(t->end() && search(root, root, word, i)) {\n return true;\n }\n t = t->get(word[i]);\n }\n return t != nullptr && t->end();\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) const {\n std::sort(words.begin(), words.end(), [](const auto& a, const auto& b){\n return (a.size() < b.size()) || (a.size()==b.size() && a < b);\n });\n\n vector<string> res;\n Trie trie{};\n for(auto&& w : words) {\n if(search(&trie, &trie, w, 0)) {\n res.emplace_back(std::move(w));\n }\n trie.add(w);\n }\n return res;\n }\n};", "memory": "232806" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct Trie\n{\n std::array<Trie*, 26> _childs;\n bool isWord = false;\n\n Trie * emplace(string & w, int i)\n {\n if(i == w.size())\n {\n isWord = true;\n return this;\n }\n else\n {\n int j = w[i]-'a';\n if(!_childs[j])\n {\n _childs[j] = new Trie();\n }\n\n return _childs[j]->emplace(w, i+1);\n }\n }\n\n bool has(string & w, int i)\n {\n if(i == w.size())\n {\n return isWord;\n }\n else\n {\n int j = w[i]-'a';\n return _childs[j] ? _childs[j]->has(w, i+1) : false;\n }\n }\n};\n\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n //std::unordered_set<string> set;\n Trie set;\n for(auto & w : words)\n {\n set.emplace(w, 0);\n }\n\n vector<string> ans;\n for(auto & w : words)\n {\n std::vector<bool> visted(w.size(), false);\n if(isConcatenated(w, 0, set, visted))\n {\n ans.emplace_back(w);\n }\n }\n return ans;\n }\n\n bool isConcatenated(string & w, int i, Trie & set, std::vector<bool> & visted)\n {\n bool ret = (i == w.size());\n string key;\n for(int j=i; j<w.size(); ++j)\n {\n key += w[j];\n if(!visted[j] && set.has(key,0) && key.size() < w.size())\n { \n visted[j] = true;\n ret |= isConcatenated(w, j+1, set, visted);\n }\n\n if(ret)\n {\n break;\n }\n }\n\n return ret;\n }\n};\n\n// class Solution {\n// bool dfs(const string& word, int length, vector<bool>& visited,\n// const unordered_set<string>& dictionary) {\n// if (length == word.length()) \n// {\n// return true;\n// }\n \n// if (visited[length]) \n// {\n// return false;\n// }\n\n// visited[length] = true;\n// for (int i = word.length() - (length == 0 ? 1 : 0); i > length; --i) \n// {\n// if (dictionary.count(word.substr(length, i - length)) &&\n// dfs(word, i, visited, dictionary)) \n// {\n// return true;\n// }\n// }\n// return false;\n// }\n\n// public:\n// vector<string> findAllConcatenatedWordsInADict(vector<string>& words) \n// {\n// unordered_set<string> dictionary(words.begin(), words.end());\n// vector<string> answer;\n// for (const string& word : words) \n// {\n// vector<bool> visited(word.length()); // ?\n// if (dfs(word, 0, visited, dictionary)) \n// {\n// answer.push_back(word);\n// }\n// }\n// return answer;\n// }\n// };", "memory": "235948" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "\nclass Solution {\n public:\n class Tire {\n struct TireNode {\n bool has = false;\n unique_ptr<TireNode> child[26] = {};\n };\n\n unique_ptr<TireNode> root;\n\n public:\n Tire() : root(std::make_unique<TireNode>()) {}\n\n unordered_map<string, bool> memo;\n\n bool IsCat(string_view word) {\n if (word.empty()) {\n return true;\n }\n\n if (memo.contains(string(word))) {\n return memo[string(word)];\n }\n\n auto* curr = root.get();\n bool ans = false;\n for (int i = 0; i < word.size(); ++i) {\n if (ans) {\n break;\n }\n\n auto c = word[i];\n\n if (auto& next = curr->child[c - 'a']; next == nullptr) {\n return ans;\n }\n\n curr = curr->child[c - 'a'].get();\n\n if (curr->has) {\n ans |= IsCat(word.substr(i + 1));\n }\n }\n\n return memo[std::string(word)] = ans;\n }\n\n void InsertWord(string_view word) {\n if (word.empty()) {\n return;\n }\n\n auto* curr = root.get();\n\n for (int i = 0; i < word.size(); ++i) {\n auto c = word[i];\n\n if (auto& next = curr->child[c - 'a']; next == nullptr) {\n next = std::make_unique<TireNode>();\n }\n\n curr = curr->child[c - 'a'].get();\n }\n\n curr->has = true;\n }\n };\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n ranges::sort(words, [](auto& a, auto& b) { return a.size() < b.size(); });\n\n vector<string> ans;\n Tire t;\n for (auto& w : words) {\n if (t.IsCat(w)) {\n ans.push_back(w);\n } else {\n t.InsertWord(w);\n }\n }\n return ans;\n }\n};\n", "memory": "235948" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string findkey(string &s, int k){\n string key = \"\";\n string tmp = to_string(k);\n key+=tmp;\n key.push_back('#');\n for(char x : s){\n key.push_back(x);\n }\n return key;\n }\n bool solve(string &s, int i, string &curr, unordered_map<string,int> &umap, vector<string> &temp, unordered_map<string,bool> &dp){\n if(i==s.length()){\n if(curr.length()!=0){\n if(umap.find(curr)!=umap.end()){\n temp.push_back(curr);\n }else{\n return false;\n }\n }\n if(temp.size()<=1) return false;\n string tmp=\"\";\n for(string x : temp){\n for(char y : x){\n tmp.push_back(y);\n }\n }\n return umap.find(tmp)!=umap.end();\n }\n string key = findkey(curr,i);\n if(dp.find(key)!=dp.end()) return dp[key];\n bool ans = false;\n curr.push_back(s[i]);\n if(umap.find(curr)!=umap.end()){\n string x = curr;\n curr = \"\";\n temp.push_back(x);\n ans|=solve(s,i+1,curr,umap,temp,dp);\n curr=x;\n temp.pop_back();\n }\n ans|=solve(s,i+1,curr,umap,temp,dp);\n return dp[key]=ans;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n unordered_map<string,int> umap;\n for(string x : words){\n umap[x]=1;\n }\n vector<string> ans;\n for(string x : words){\n vector<string> temp;\n string curr = \"\";\n unordered_map<string,bool> dp;\n bool tmp = solve(x,0,curr,umap,temp,dp);\n if(tmp){\n ans.push_back(x);\n }\n }\n return ans;\n \n }\n};", "memory": "239091" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct Node{\n Node* child[26];\n bool eow;\n\n Node(){\n for(int i = 0; i < 26; i++){\n child[i] = NULL;\n }\n eow = false;\n }\n};\n\nclass Solution {\npublic:\n Node* root;\n\n Solution() {\n root = new Node();\n }\n\n void insert(string s) {\n Node* node = root;\n for (int i = 0; i < s.size(); i++) {\n int idx = s[i] - 'a';\n if (node->child[idx] == NULL) {\n node->child[idx] = new Node();\n }\n node = node->child[idx];\n }\n node->eow = true;\n }\n\n bool solve(string s, int start, vector<int>& dp, int count) {\n Node* node = root;\n\n if (start == s.size()) return count >= 2; // Word formed by at least two smaller words\n\n if (dp[start] != -1) return dp[start];\n\n for (int i = start; i < s.size(); i++) {\n int idx = s[i] - 'a';\n\n if (node->child[idx] == NULL) {\n return dp[start] = false;\n }\n\n node = node->child[idx];\n\n if (node->eow) { // A valid subword is found\n if (solve(s, i + 1, dp, count + 1)) {\n return dp[start] = true; // Return true if rest of the word can be split\n }\n }\n }\n\n return dp[start] = false; // No valid concatenation found\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for (auto word : words) {\n insert(word); // Insert all words into the Trie\n }\n\n vector<string> ans;\n\n for (auto word : words) {\n vector<int> dp(word.size(), -1); // Memoization array for each word\n if (!word.empty() && solve(word, 0, dp, 0)) {\n ans.push_back(word); // If the word can be concatenated, add to result\n }\n }\n\n return ans;\n }\n};\n", "memory": "239091" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "typedef long long ll;\nconst int MOD1 = 1e9 + 7;\nconst int MOD2 = 1e9 + 9;\nconst int st1 = 137;\nconst int st2 = 2137;\nclass Solution {\npublic:\n pair<int, int> hash(string &s, int a, int b){\n int hash1=0, hash2=0;\n for(int i=a;i<=b;i++){\n hash1=((ll)hash1*st1+s[i])%MOD1;\n hash2=((ll)hash2*st2+s[i])%MOD2;\n }\n return {hash1, hash2};\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n set<pair<int, int>> dic;\n for(auto &v : words){\n dic.insert(hash(v, 0, (int)v.size()-1));\n }\n for(auto &v : words){\n pair<int, int> pm=hash(v, 0, (int)v.size()-1);\n dic.erase(pm);\n int n=(int)v.size();\n vector<bool> dp(n, 0);\n vector<vector<pair<int, int>>> calc(n, vector<pair<int, int>>(n, {0, 0}));\n for(int i=0;i<n;i++){\n for(int j=i;j<n;j++){\n calc[i][j]=hash(v, i, j);\n }\n }\n for(int i=0;i<n;i++){\n if(dic.find(calc[0][i])!=dic.end()){\n dp[i]=true;\n continue;\n }\n for(int j=0;j<i;j++){\n if(dp[j]&&dic.find(calc[j+1][i])!=dic.end()){\n dp[i]=true;\n break;\n }\n }\n }\n if(dp[n-1]) ans.push_back(v);\n dic.insert(pm);\n }\n return ans;\n }\n};", "memory": "242233" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n\n map<string,int> wordMap;\n \n struct TrieNode{\n TrieNode* children[26];\n bool isLeaf;\n\n TrieNode(){\n for(int i = 0; i < 26 ; i++){\n children[i] = NULL;\n }\n isLeaf = false;\n }\n };\n\n void insert(TrieNode* root, string word){\n for(int i = 0; i < word.size();i++){\n if(root->children[word[i]-'a'] == NULL){\n root->children[word[i]-'a'] = new TrieNode();\n }\n root = root->children[word[i]-'a'];\n }\n root->isLeaf = true;\n }\n\n bool isWord(TrieNode* root, string word){\n for(int i = 0; i < word.size();i++){\n if(root->children[word[i]-'a'] == NULL) return false;\n root = root->children[word[i]-'a'];\n }\n return root->isLeaf;\n }\n\n int dp[10005];\n\n int checkConcatenation(int l , int r, TrieNode* &root ,string word){\n if(l>=r) return 0;\n int countWords = INT_MIN;\n\n if(dp[l] != -1) return dp[l];\n for(int i = l ; i<=r;i++){\n string temp = word.substr(l,i-l+1);\n if(isWord(root,temp)){\n // cout<<temp<<endl; \n int value = checkConcatenation(i+1,r,root,word);\n if(value != INT_MIN)\n countWords = max(countWords , 1 + value);\n }\n }\n return dp[l] = countWords;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n\n int n = words.size();\n vector<string> ans;\n TrieNode* root = new TrieNode();\n for(int i = 0; i < n ;i++){\n insert(root,words[i]);\n }\n for(int i = 0 ; i < n ; i++){\n memset(dp,-1,sizeof(dp));\n int wordCount = checkConcatenation(0, words[i].size() , root , words[i]);\n if(wordCount >=2) ans.push_back(words[i]);\n }\n\n return ans;\n \n }\n};", "memory": "242233" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class TrieNode{\n public:\n TrieNode* node[26];\n bool ends;\n TrieNode(){\n ends = 0;\n for(int i = 0; i < 26; i++){\n node[i] = nullptr;\n }\n }\n\n};\nclass Trie{\n public:\n TrieNode* root;\n Trie(){\n root = new TrieNode();\n }\n void insert(string& s){\n int n = s.size();\n TrieNode* ptr = root;\n for(int i = 0; i < n; i++){\n int ch = s[i] - 'a';\n if(ptr->node[ch] == nullptr){\n ptr->node[ch] = new TrieNode();\n }\n ptr = ptr->node[ch];\n }\n ptr->ends = 1;\n }\n};\n\nclass Solution {\npublic:\n bool rec(int i, int j, vector<string>& words, TrieNode* root, map<string, int>& mp) {\n bool ans = 0;\n if (j >= words[i].size()) {\n return 1;\n }\n string s = words[i].substr(j);\n // cout << i << \" \" << j << \" \" << s << endl;\n int n = words[i].size();\n if (mp.find(s) != mp.end()) {\n // cout << \"memo \" << s << endl;\n return mp[s];\n }\n\n TrieNode* ptr = root; // Don't modify the original root reference\n for (int k = j; k < n; k++) {\n int ch = words[i][k] - 'a';\n if (ptr->node[ch] == nullptr) {\n break;\n }\n ptr = ptr->node[ch];\n if (ptr->ends) {\n ans |= rec(i, k + 1, words, root, mp); // Pass the original root\n }\n }\n\n return mp[s] = ans;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n = words.size();\n if (n <= 2) {\n return {};\n }\n\n // Sort words by length\n sort(words.begin(), words.end(), [&](const string& a, const string& b) {\n return a.size() < b.size();\n });\n // for(auto x : words){\n // cout << x << \" \";\n // }\n // cout << endl;\n // Remove duplicates\n words.erase(unique(words.begin(), words.end()), words.end());\n\n map<string, int> mp;\n Trie tree;\n\n // Insert first two words into Trie\n tree.insert(words[0]);\n tree.insert(words[1]);\n mp[words[0]] = 1;\n mp[words[1]] = 1;\n vector<string> ans;\n for (int i = 2; i < n; i++) {\n if (rec(i, 0, words, tree.root, mp)) {\n ans.push_back(words[i]);\n tree.insert(words[i]);\n } else {\n mp[words[i]] = 1;\n tree.insert(words[i]); // Insert current word into Trie\n }\n }\n return ans;\n }\n};\n", "memory": "245376" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class trie{\n public:\n trie *children[26];\n bool end;\n\n \n trie(){\n this->end=false;\n for(int i=0;i<26;i++) this->children[i]=NULL;\n }\n void insert(string &key){\n trie *root=this;\n for(auto i:key){\n if(root->children[i-'a']==NULL){\n root->children[i-'a']=new trie();\n }\n root=root->children[i-'a'];\n }\n root->end=true;\n }\n\n \n};\n\nclass Solution {\npublic:\n map<string,pair<int,bool>> res;\n pair<int,bool> search(trie *root,string &key){\n trie *currNode=root;\n for(int i=0;i<key.size();i++){\n if(currNode->end==true){\n string x=key.substr(i);\n if(res.count(x)){\n if(res[x].second) return res[key]={2,true};\n }else{\n pair<int,bool> check=search(root,x);\n res[x]=check;\n if(check.second) return res[key]={2,true};\n }\n }\n if(currNode->children[key[i]-'a']==NULL) return {0,false};\n currNode=currNode->children[key[i]-'a'];\n }\n if(currNode->end==true) return res[key]={1,true};\n return res[key]={0,false};\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n trie *root=new trie();\n for(auto i:words){\n root->insert(i);\n }\n vector<string> v;\n for(auto i:words){\n pair<int,bool> check=search(root,i);\n if(check.second && check.first>1) v.push_back(i);\n }\n return v;\n }\n};", "memory": "245376" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Node {\npublic:\n bool isEnd = false;\n Node *next[26] = {nullptr};\n};\n\nclass Trie {\npublic:\n Node *head;\n\n Trie() {\n head = new Node();\n }\n\n void insert(string word) {\n Node *tmp = head;\n for (auto c: word) {\n c -= 'a';\n if (tmp->next[c]) {\n tmp = tmp->next[c];\n } else {\n tmp->next[c] = new Node();\n tmp = tmp->next[c];\n }\n }\n tmp->isEnd = true;\n }\n\n bool search(string word) {\n Node *tmp = head;\n for (auto c: word) {\n c -= 'a';\n if (tmp->next[c]) {\n tmp = tmp->next[c];\n } else {\n return false;\n }\n }\n return tmp->isEnd;\n }\n};\n\nclass Solution {\npublic:\n Trie trie;\n int isConcatenated(int index, string word, int pieces, vector<int>& dp) {\n if (index == word.size()) return pieces >= 2;\n else if (dp[index] != -1) return dp[index];\n\n string curr = \"\";\n dp[index] = 0;\n for (int i=index; i<word.size(); i++) {\n curr += word[i];\n if (trie.search(curr)) {\n dp[index] |= isConcatenated(i+1, word, pieces + 1, dp);\n }\n }\n return dp[index];\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n for (auto word: words) {\n trie.insert(word);\n }\n\n vector<string> ans;\n for (auto word: words) {\n vector<int> dp(word.size(), -1);\n if (isConcatenated(0, word, 0, dp)) {\n ans.push_back(word);\n }\n }\n return ans;\n }\n};", "memory": "248518" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct node{\n node* links[26];\n bool flag=false;\n\n bool contains(char ch){\n return links[ch-'a']!=NULL;\n }\n\n void put(char ch,node* temp){\n links[ch-'a']=temp;\n }\n\n node* get(char ch){\n return links[ch-'a'];\n }\n\n void setflag(){\n this->flag=true;\n }\n\n bool retflag(){\n return this->flag;\n }\n\n};\n\nclass Trie{\n private: node* root;\n public:\n Trie(){\n root=new node();\n }\n\n void insert(string word){\n node* temp=root;\n for(int i=0;i<word.size();i++){\n if(!temp->contains(word[i])){\n temp->put(word[i],new node());\n }\n temp=temp->get(word[i]);\n }\n temp->setflag();\n }\n\n bool func(int ind,int cnt,string word,map<pair<int,int>,int>& dp){\n if(ind>=word.size()) return cnt>1;\n if(dp.find({ind,cnt})!=dp.end()) return dp[{ind,cnt}];\n node* temp=root;\n for(int i=ind;i<word.size();i++){\n if(!temp->contains(word[i])) return dp[{ind,cnt}]=false;\n temp=temp->get(word[i]);\n if(temp->retflag()){\n if(func(i+1,cnt+1,word,dp)) return dp[{ind,cnt}]=true;\n }\n }\n return dp[{ind,cnt}]=false;\n }\n\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie trie;\n for(auto value: words){\n trie.insert(value);\n }\n vector<string> ans;\n \n for(auto value: words){\n map<pair<int,int>,int> dp;\n if(trie.func(0,0,value,dp)) ans.push_back(value);\n }\n return ans;\n }\n};", "memory": "248518" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "const int INF = 1e9;\n\nstruct TrieNode {\n TrieNode* next[26];\n bool terminal = false;\n\n TrieNode() {\n for (auto& v : next)\n v = nullptr;\n }\n};\n\nstruct Trie {\n TrieNode* root;\n\n Trie() : root(new TrieNode) {}\n\n void insert(string s) {\n TrieNode* v = root;\n \n for (char ch : s) {\n if (!v->next[ch-'a'])\n v->next[ch-'a'] = new TrieNode;\n \n v = v->next[ch-'a'];\n }\n v->terminal = true;\n }\n\n unordered_map<string, int> m;\n bool search(string s) {\n TrieNode* v = root;\n\n auto dfs = [&](auto&& self, string t) {\n if (m.find(t) != m.end())\n return m[t];\n\n if (t.empty())\n return 0;\n\n int count = -INF;\n TrieNode* v = root;\n\n for (int i = 0; i < (int)t.size(); ++i) {\n if (!v->next[t[i]-'a'])\n break;\n\n v = v->next[t[i]-'a'];\n\n if (v->terminal)\n count = max(count, 1+self(self, t.substr(i+1, t.size())));\n }\n\n return m[t] = count;\n };\n\n return dfs(dfs, s) >= 2;\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Trie trie;\n vector<string> ans;\n\n sort(words.begin(), words.end(), [](auto i, auto j) {\n return (i.length() < j.length()) || (i.length() == j.length() && i < j);\n });\n\n for (auto word : words) {\n trie.insert(word);\n if (trie.search(word))\n ans.push_back(word);\n }\n return ans;\n }\n};", "memory": "251661" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string substring(string s,int i,int j){\n string temp;\n for(int k=i;k<j;k++)\n temp.push_back(s[k]);\n return temp;\n }\n bool solve(int i,int j,string &s,vector<vector<int>>&dp,map<string,int> &m){\n if(i==s.size()){\n string tt = substring(s,j,i);\n if(m[tt])\n return 1;\n return 0;\n }\n if(dp[i][j]!=-1)\n return dp[i][j];\n\n string tt = substring(s,j,i+1);\n int ans = 0;\n if(m[tt])\n ans|=solve(i+1,i+1,s,dp,m);\n \n ans|=solve(i+1,j,s,dp,m);\n return dp[i][j]=ans;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n map<string,int> m;\n for(auto &b:words){\n m[b]++;\n }\n vector<string> ans;\n for(auto b:words){\n m[b]--;\n vector<vector<int>>dp(b.size(),vector<int>(b.size(),-1));\n if(solve(0,0,b,dp,m))\n ans.push_back(b);\n m[b]++;\n }\n return ans;\n }\n};", "memory": "251661" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool func(int i, string& s, string dum, map<string, int>& m, int flag, map<pair<int, string>, int>& dp) {\n if (i == s.length()) {\n return dum.empty() && flag >= 2;\n }\n\n if (dp.find({i, dum}) != dp.end()) return dp[{i, dum}];\n \n dum += s[i];\n bool take = false, no = false;\n\n if (m[dum]) {\n take = func(i + 1, s, \"\", m, flag + 1, dp);\n }\n no = func(i + 1, s, dum, m, flag, dp);\n\n return dp[{i, dum}] = take || no;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n map<string, int> m;\n int n = words.size();\n \n for (int i = 0; i < n; i++) {\n m[words[i]]++;\n }\n\n vector<string> ans;\n\n for (int i = 0; i < n; i++) {\n map<pair<int, string>, int> dp;\n if (func(0, words[i], \"\", m, 0, dp)) {\n ans.push_back(words[i]);\n }\n }\n\n return ans;\n }\n};\n", "memory": "254803" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class TrieNode {\npublic:\n vector<TrieNode*> children;\n int path = 0;\n int end = 0;\n\n TrieNode(){\n children.assign(26,nullptr);\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n auto root = new TrieNode();\n\n for(auto w : words){\n auto node = root;\n for(auto c : w){\n c -= 'a';\n if (node->children[c]==nullptr){\n node->children[c] = new TrieNode();\n }\n node = node->children[c];\n node->path++;\n }\n node->end++;\n }\n\n auto check = [&](auto&& check, string s)->bool{\n int n = s.size();\n\n vector<int> dp(n);\n\n for(int i=0; i<n; ++i){\n if (i==0 || dp[i-1]){\n auto node = root;\n\n for(int j=i; j<n; ++j){\n int c = s[j]-'a';\n if (node->children[c]==nullptr){\n break;\n }\n\n node = node->children[c];\n if (node->end>0 && !(i==0 && j==n-1)){\n dp[j] = 1;\n }\n }\n }\n }\n\n return dp[n-1];\n };\n\n vector<string> ans;\n for(auto w : words){\n if (check(check, w)){\n ans.push_back(w);\n }\n }\n\n return ans;\n }\n\n // vector<string> fun2(vector<string>& words) {\n\n // }\n\n vector<string> fun1(vector<string>& words) {\n unordered_set<string> dictionary(words.begin(), words.end());\n vector<string> answer;\n for (const string& word : words) {\n const int length = word.length();\n vector<bool> dp(length + 1);\n dp[0] = true;\n for (int i = 1; i <= length; ++i) {\n for (int j = (i == length ? 1 : 0); !dp[i] && j < i; ++j) {\n dp[i] = dp[j] && dictionary.count(word.substr(j, i - j));\n }\n }\n if (dp[length]) {\n answer.push_back(word);\n }\n }\n return answer;\n }\n};\n", "memory": "267373" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trienode{\n public:\n bool end=false;\n Trienode* *next =new Trienode* [26];\n Trienode(){\n for(int i=0 ; i<26 ;i++){\n this->next[i]=NULL;\n }\n }\n};\nclass Trie{\n public:\n Trienode * root=new Trienode;\n void insert(Trienode * root,string word){\n if(!root->next[word[0]-'a']){\n root->next[word[0]-'a']=new Trienode;\n }\n if(word.size()==1){\n root->next[word[0]-'a']->end=true;\n }else{\n insert(root->next[word[0]-'a'],word.substr(1));\n }\n }\n bool search(Trienode * root,string word){\n if(!root->next[word[0]-'a']){\n return false;\n }else{\n if(word.size()==1){\n return root->next[word[0]-'a']->end;\n }else{\n return search(root->next[word[0]-'a'],word.substr(1));\n }\n }\n return false;\n }\n void add(string a){\n insert(root,a);\n }\n bool find(string a){\n return search(root,a);\n }\n};\n\n\nclass Solution {\npublic:\n bool helper(string word,Trie * tri,Trie * combo,vector<int> & dp , int i){\n int n=word.size();\n if(i==n) return 1;\n if(dp[i]!=-1) return dp[i];\n if(i!=0 && combo->find(word.substr(i))) return dp[i]=1;\n for(int j=i;j<n;j++){\n if((tri->find(word.substr(i,j-i+1)) ) && ((i==0 && j==n-1) ? 0:1)){\n bool tmp=helper(word,tri,combo,dp,j+1);\n if(tmp){\n combo->add(word);\n return dp[i]=1;\n }\n }\n }\n return dp[i]=0;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> ans;\n Trie * tri=new Trie;\n for(auto it:words){\n tri->add(it);\n }\n Trie * combo= new Trie;// taki baad me same subword toh use without solving can say\n for(auto it:words){\n vector<int> dp(it.size(),-1);\n if(helper(it,tri,combo,dp,0)){\n ans.push_back(it);\n }\n }\n return ans;\n }\n};", "memory": "267373" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct TrieNode {\n bool isWord = false;\n TrieNode* children[26];\n unordered_map<string, bool> cache;\n TrieNode() {\n for (int i = 0; i < 26; i++) {\n children[i] = nullptr;\n }\n }\n void insert(string& s) {\n TrieNode* head = this;\n for (auto it = s.begin(); it < s.end(); it++) {\n char c = *it;\n if (head->children[c - 'a'] == nullptr) {\n head->children[c - 'a'] = new TrieNode();\n }\n head = head->children[c - 'a'];\n }\n head->isWord = true;\n }\n\n bool solve(string& s, bool oneOrMore) {\n TrieNode* head = this;\n auto found = cache.find(s);\n if (oneOrMore && found != cache.end()) {\n return found->second;\n }\n for (auto it = s.begin(); it < s.end(); it++) {\n char c = *it;\n if (head->children[c - 'a'] == nullptr) {\n if (oneOrMore) {\n cache.insert({s, false});\n }\n return false;\n } else if (head->children[c - 'a']->isWord) {\n string sub(it + 1, s.end());\n if (solve(sub, true)) {\n if (oneOrMore) {\n cache.insert({s, true});\n }\n return true;\n }\n }\n head = head->children[c - 'a'];\n }\n if (oneOrMore) {\n cache.insert({s, head->isWord});\n }\n return oneOrMore && head->isWord;\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n TrieNode t;\n for (auto it : words) {\n t.insert(it);\n }\n vector<string> ret;\n\n for (auto it : words) {\n if (t.solve(it, false)) {\n ret.push_back(it);\n }\n }\n return ret;\n }\n};", "memory": "270516" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct TrieNode {\n bool isWord = false;\n TrieNode* children[26];\n unordered_map<string, bool> cache;\n TrieNode() {\n for (int i = 0; i < 26; i++) {\n children[i] = nullptr;\n }\n }\n void insert(string& s) {\n TrieNode* head = this;\n for (auto it = s.begin(); it < s.end(); it++) {\n char c = *it;\n if (head->children[c - 'a'] == nullptr) {\n head->children[c - 'a'] = new TrieNode();\n }\n head = head->children[c - 'a'];\n }\n head->isWord = true;\n }\n\n bool solve(string& s, bool oneOrMore) {\n TrieNode* head = this;\n auto found = cache.find(s);\n if (oneOrMore && found != cache.end()) {\n return found->second;\n }\n for (auto it = s.begin(); it < s.end(); it++) {\n char c = *it;\n if (head->children[c - 'a'] == nullptr) {\n if (oneOrMore) {\n cache.insert({s, false});\n }\n return false;\n } else if (head->children[c - 'a']->isWord) {\n string sub(it + 1, s.end());\n if (solve(sub, true)) {\n if (oneOrMore) {\n cache.insert({s, true});\n }\n return true;\n }\n }\n head = head->children[c - 'a'];\n }\n if (oneOrMore) {\n cache.insert({s, head->isWord});\n }\n return oneOrMore && head->isWord;\n }\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n TrieNode t;\n for (auto it : words) {\n t.insert(it);\n }\n vector<string> ret;\n for (auto it : words) {\n if (t.solve(it, false)) {\n ret.push_back(it);\n }\n }\n return ret;\n }\n};", "memory": "270516" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class TrieNode {\npublic:\n char c;\n vector<TrieNode*> children;\n bool end;\n\n // Constructor\n TrieNode(char k) : c(k), children(26, nullptr), end(false) {}\n};\n\nclass Solution {\npublic :\n TrieNode*root;\n Solution(){\n root = new TrieNode('N');\n }\n void insert(TrieNode*r,string &word,int i){\n int n = word.size();\n if(i == n){\n r->end = 1;\n return;\n }\n int ind = word[i] - 'a';\n if(!r->children[ind]){\n r->children[ind] = new TrieNode(word[i]);\n }\n insert(r->children[ind],word,i+1);\n }\n int concatinate(string &word,vector<int>&dp,int i){\n if(dp[i] != -1)return dp[i];\n int n = word.size();\n TrieNode*now = root;\n for(int j = i;j < n;j++){\n int ind = word[j] - 'a';\n now = now->children[ind];\n if(!now)return dp[i] = 0;\n if(now -> end){\n int p = concatinate(word,dp,j+1);\n if(p >= 1)return dp[i] = 1;\n }\n }\n return dp[i] = 0;\n }\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n Solution s;\n for(string &i : words)s.insert(s.root,i,0);\n vector<string>ans;\n for(string &i : words){\n int n = i.size();\n vector<int>dp(n+1,-1);\n dp[i.size()] = 1;\n TrieNode*now = s.root;\n for(int j = 0;j < n-1;j++){\n int ind = i[j] - 'a';\n now = now->children[ind];\n if(now->end){\n if(s.concatinate(i,dp,j+1) == 1){ans.emplace_back(i);break;}\n }\n }\n }\n return ans;\n }\n};", "memory": "273658" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trie{\n public:\n vector<Trie*> children = vector<Trie*>(26, nullptr);\n bool flag = 0;\n};\n\nclass Solution {\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n vector<string> res;\n Trie* root = new Trie();\n for(auto& el: words)\n {\n Trie* ptr=root;\n for(int i=0; i<el.size(); i++)\n {\n if(ptr->children[el[i]-'a']==nullptr)\n {\n Trie* t = new Trie();\n ptr->children[el[i]-'a']=t;\n ptr=t;\n }\n else\n {\n ptr=ptr->children[el[i]-'a'];\n }\n }\n ptr->flag=1;\n }\n\n for(auto& el: words)\n {\n // cout <<endl;\n vector<Trie*> dfs={root};\n vector<int> id={0};\n int p=0;\n while(!dfs.empty())\n {\n if(p==32) break;\n p++;\n int cnt=id[id.size()-1];\n // cout << el.size()<<\": \"<<cnt<<endl;\n \n if(cnt==el.size())\n {\n // cout <<\"ok\";\n if((dfs[dfs.size()-1]==root) && dfs.size()>2)\n {\n res.push_back(el);\n break;\n }\n dfs.pop_back();\n id.pop_back();\n continue;\n }\n if(dfs[dfs.size()-1]->children[el[id[id.size()-1]]-'a']!=nullptr)\n {\n dfs[dfs.size()-1]=dfs[dfs.size()-1]->children[el[cnt]-'a'];\n id[id.size()-1]++;\n if(dfs[dfs.size()-1]->flag==1)\n {\n dfs.push_back(root);\n id.push_back(id[id.size()-1]);\n }\n }\n else\n {\n dfs.pop_back();\n id.pop_back();\n }\n }\n // if(p==31)\n // {\n // cout << el.size() <<\": \"<<id[id.size()-1]<<endl;\n // }\n // cout << endl;\n }\n return res;\n }\n};", "memory": "276801" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n struct Node{\n char ch;\n int isWord;\n vector<Node*> next;\n Node(char c){\n ch = c;\n isWord = 0;\n next.resize(26);\n for(int i=0;i<26;i++){\n next[i] = nullptr;\n }\n }\n };\n\n vector<vector<int>> dp;\n\n bool isConcat(int word_num,int pos,Node* &master,vector<string>& words){\n cout<<word_num<<\" \" << pos <<endl;\n if(pos >= words[word_num].size()){\n return true;\n }\n if(dp[word_num][pos] != -1){\n return dp[word_num][pos];\n }\n bool possible = false;\n Node* curr = master;\n for(int i=pos;i<words[word_num].size();i++){\n if(curr->next[words[word_num][i]-'a'] == nullptr){\n break;\n }\n curr = curr->next[words[word_num][i]-'a'];\n cout << curr->ch << endl;\n if(curr->isWord == 1){\n possible |= isConcat(word_num,i+1,master,words);\n if(possible && (pos != 0 || (i != words[word_num].size()-1))){\n dp[word_num][pos] = 1;\n return true;\n }\n possible = false;\n }\n if(i+1 != words[word_num].size() && curr->next[words[word_num][i+1] - 'a'] == nullptr){\n dp[word_num][pos] = 0;\n return false;\n }\n }\n\n dp[word_num][pos] = (possible == true ? 1 : 0);\n return possible;\n }\n\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n int n = words.size();\n dp.assign(n+1, vector<int>(32,-1));\n Node* master = new Node('a');\n for(int i=0;i<n;i++){\n Node* curr = master;\n for(int j=0;j<words[i].size();j++){\n if(curr->next[words[i][j]-'a'] == nullptr){\n curr->next[words[i][j]-'a'] = new Node(words[i][j]);\n }\n curr = curr->next[words[i][j]-'a'];\n }\n curr->isWord = 1;\n }\n vector<string> answer;\n for(int i=0;i<n;i++){\n if(isConcat(i,0,master,words)){\n answer.push_back(words[i]);\n }\n }\n return answer;\n }\n};", "memory": "276801" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Trie{\npublic:\n Trie* children[26];\n bool whether_end;\n Trie():whether_end(false){\n fill(begin(children), end(children), nullptr);\n }\n};\nclass Solution {\nprivate:\n Trie * t;\n vector<string>res;\n void add_word(string & word){\n Trie * cur=t;\n for(char c:word){\n if(cur->children[c-'a']==nullptr){\n cur->children[c-'a']=new Trie();\n }\n cur=cur->children[c-'a'];\n }\n cur->whether_end=true;\n }\n void search_word2(Trie * old_trie, Trie * new_trie, string old_s, string new_s){\n if(old_trie==nullptr||new_trie==nullptr){\n return ;\n }\n if(old_trie->whether_end&&new_trie->whether_end){\n // cout<<old_trie->s<<\" \"<<new_trie->s<<endl;\n res.push_back(old_s);\n old_trie->whether_end=false;\n }\n if(new_trie->whether_end){\n search_word2(old_trie, t, old_s, new_s);\n }\n for(int i=0; i<26; i++){\n this->search_word2(old_trie->children[i], new_trie->children[i], \n old_s+(char)(i+'a'), new_s+(char)(i+'a'));\n }\n }\n void search_word(Trie * current, string s){\n if(current==nullptr){\n return ;\n }\n if(current->whether_end){\n search_word2(current, t, s, \"\");\n }\n for(int i=0; i<26; i++){\n search_word(current->children[i], s+(char)(i+'a'));\n }\n }\npublic:\n vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {\n this->t=new Trie();\n for(string &word:words){\n add_word(word);\n }\n search_word(t, \"\");\n return res;\n }\n};", "memory": "279943" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct TrieNode {\n int chars[32] = {};\n bool contains = false;\n };\n\n std::vector<std::string> findAllConcatenatedWordsInADict(std::vector<std::string>& words) {\n std::vector<TrieNode> nodes(1);\n for (auto const& word : words) {\n auto node = nodes.begin();\n std::string_view part = word;\n while (!part.empty()) {\n int idx = node->chars[part.front() - 'a'];\n if (idx > 0)\n node = nodes.begin() + idx;\n else {\n node->chars[part.front() - 'a'] = static_cast<int>(nodes.size());\n nodes.push_back(TrieNode{});\n node = nodes.end() - 1;\n }\n part.remove_prefix(1);\n }\n node->contains = true;\n }\n\n std::vector<std::string> ans;\n for (auto & word : words) {\n std::deque<std::pair<std::pair<int, bool>, std::string_view>> queue;\n queue.emplace_back(std::pair<int, bool>{0, false}, word);\n bool look[31] = {};\n while (!queue.empty()) {\n auto [pair, part] = queue.front();\n auto [idx, compound] = pair;\n queue.pop_front();\n while (!part.empty()) {\n auto it = nodes[idx].chars[part.front() - 'a'];\n if (it == 0)\n break;\n part.remove_prefix(1);\n int pos = static_cast<int>(word.size() - part.size());\n if (nodes[it].contains && !part.empty() && !look[pos]) {\n queue.emplace_back(std::pair<int, bool>{0, true}, part);\n look[pos] = true;\n }\n idx = it;\n }\n if (part.empty() && nodes[idx].contains && compound) {\n ans.emplace_back(std::move(word));\n break;\n }\n }\n }\n return ans;\n }\n};", "memory": "283086" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct TrieNode {\n int chars[32] = {};\n bool contains = false;\n };\n\n std::vector<std::string> findAllConcatenatedWordsInADict(std::vector<std::string>& words) {\n std::vector<TrieNode> nodes(1);\n for (auto const& word : words) {\n auto node = nodes.begin();\n std::string_view part = word;\n while (!part.empty()) {\n int idx = node->chars[part.front() - 'a'];\n if (idx > 0)\n node = nodes.begin() + idx;\n else {\n node->chars[part.front() - 'a'] = static_cast<int>(nodes.size());\n nodes.push_back(TrieNode{});\n node = nodes.end() - 1;\n }\n part.remove_prefix(1);\n }\n node->contains = true;\n }\n\n std::vector<std::string> ans;\n for (auto & word : words) {\n std::deque<std::pair<std::pair<int, bool>, std::string_view>> queue;\n queue.emplace_back(std::pair<int, bool>{0, false}, word);\n bool look[31] = {};\n while (!queue.empty()) {\n auto [pair, part] = queue.front();\n auto [idx, compound] = pair;\n queue.pop_front();\n while (!part.empty()) {\n auto it = nodes[idx].chars[part.front() - 'a'];\n if (it == 0)\n break;\n part.remove_prefix(1);\n int pos = static_cast<int>(word.size() - part.size());\n if (nodes[it].contains && !part.empty() && !look[pos]) {\n queue.emplace_back(std::pair<int, bool>{0, true}, part);\n look[pos] = true;\n }\n idx = it;\n }\n if (part.empty() && nodes[idx].contains && compound) {\n ans.emplace_back(std::move(word));\n break;\n }\n }\n }\n return ans;\n }\n};", "memory": "286228" }
472
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p> <p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;] <strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;] <strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; &quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; &quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;] <strong>Output:</strong> [&quot;catdog&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 30</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li>All the strings of <code>words</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct TrieNode {\n int chars[31] = {};\n bool contains = false;\n };\n\n std::vector<std::string> findAllConcatenatedWordsInADict(std::vector<std::string>& words) {\n std::vector<TrieNode> nodes(1);\n for (auto const& word : words) {\n auto node = nodes.begin();\n std::string_view part = word;\n while (!part.empty()) {\n int idx = node->chars[part.front() - 'a'];\n if (idx > 0)\n node = nodes.begin() + idx;\n else {\n node->chars[part.front() - 'a'] = static_cast<int>(nodes.size());\n nodes.push_back(TrieNode{});\n node = nodes.end() - 1;\n }\n part.remove_prefix(1);\n }\n node->contains = true;\n }\n\n std::vector<std::string> ans;\n for (auto & word : words) {\n std::deque<std::pair<std::pair<int, bool>, std::string_view>> queue;\n queue.emplace_back(std::pair<int, bool>{0, false}, word);\n bool look[31] = {};\n while (!queue.empty()) {\n auto [pair, part] = queue.front();\n auto [idx, compound] = pair;\n queue.pop_front();\n while (!part.empty()) {\n auto it = nodes[idx].chars[part.front() - 'a'];\n if (it == 0)\n break;\n part.remove_prefix(1);\n int pos = static_cast<int>(word.size() - part.size());\n if (nodes[it].contains && !part.empty() && !look[pos]) {\n queue.emplace_back(std::pair<int, bool>{0, true}, part);\n look[pos] = true;\n }\n idx = it;\n }\n if (part.empty() && nodes[idx].contains && compound) {\n ans.emplace_back(std::move(word));\n break;\n }\n }\n }\n return ans;\n }\n};", "memory": "286228" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool backtrack(vector<int>& matchsticks, vector<int>&ans, int index, int side){\n if(index==matchsticks.size()) return true;\n for(int i=0;i<4;i++){\n if(ans[i]+matchsticks[index]<=side){\n ans[i]+=matchsticks[index];\n if(backtrack(matchsticks, ans, index+1, side)) return true;\n ans[i]-=matchsticks[index];\n }\n }\n return false;\n }\n bool makesquare(vector<int>& matchsticks) {\n int peri=0;\n for(auto matchstick:matchsticks){\n peri+=matchstick;\n }\n if(peri%4!=0) return false;\n int side=peri/4;\n vector<int>ans(4,0);\n sort(matchsticks.begin(), matchsticks.end(), greater<int>());\n return backtrack(matchsticks, ans, 0, side);\n }\n};", "memory": "12583" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool solve(vector<int>& matchsticks, int idx, int n, vector<int>& sides) {\n if (idx == n) return true;\n bool ans = false;\n int a = matchsticks[idx];\n for(int i = 0; i < 4; i++) {\n if (sides[i] >= a) {\n sides[i] -= a;\n ans = ans || solve(matchsticks, idx + 1, n, sides);\n sides[i] += a;\n }\n }\n return ans;\n }\n bool makesquare(vector<int>& matchsticks) {\n int m = 0, n = matchsticks.size();\n if (n < 4) return false;\n for(int a: matchsticks) m += a;\n if (m % 4 != 0) return false;\n sort(matchsticks.begin(), matchsticks.end(), greater<>());\n int l = m / 4;\n vector<int> sides(4, l);\n return solve(matchsticks, 0, n, sides);\n }\n};", "memory": "12583" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<int>& matchsticks, int target, int start, int cur_mask, unordered_set<int>& valid) {\n if (target == 0) {\n valid.insert(cur_mask);\n if (cur_mask == 0)\n cout << target << \" \" << start << \" \" << cur_mask << endl;\n return;\n }\n if (start >= matchsticks.size() || matchsticks[start] > target)\n return;\n if (start == 0 || matchsticks[start] != matchsticks[start - 1] || (cur_mask | (1 << (start - 1))) != 0) {\n helper(matchsticks, target - matchsticks[start], start + 1, (cur_mask | (1 << start)), valid);\n }\n helper(matchsticks, target, start + 1, cur_mask, valid);\n }\n\n bool makesquare(vector<int>& matchsticks) {\n int sum = 0, len = matchsticks.size();\n sort(matchsticks.begin(), matchsticks.end());\n for (int m : matchsticks)\n sum += m;\n if (sum % 4 != 0) {\n return false;\n }\n unordered_set<int> valid;\n helper(matchsticks, sum / 4, 0, 0, valid);\n if (valid.size() < 4)\n return false;\n unordered_set<int> valid_2;\n for (int edge1 : valid) {\n for (int edge2 : valid) {\n if ((edge1 & edge2) == 0) {\n valid_2.insert(edge1 | edge2);\n }\n }\n }\n for (int edge1 : valid_2) {\n for (int edge2 : valid_2) {\n if ((edge1 & edge2) == 0) {\n return true;\n }\n }\n }\n return false;\n }\n};", "memory": "19648" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& v) {\n \n int tot = accumulate(begin(v) , end(v) , 0);\n \n if(tot%4 or v.size() < 4)return 0;\n \n vector<int>poss;\n \n int n = v.size();\n \n vector<int>ids(n);\n iota(begin(ids) , end(ids) , 0);\n \n function<int(int , vector<int> , int)>isPoss = [&](int target , vector<int>ids , int mode)-> int {\n \n int m = ids.size();\n for(int i = 0 ; i < (1<<m) ; ++i){\n int sum = 0;\n for(int j = 0 ; j < m ; ++j){\n if(i&(1<<j))sum += v[mode ? ids[j] : n - 1 - ids[j]];\n }\n \n if(sum == target){\n if(mode) return 1;\n else poss.push_back(i);\n }\n }\n \n return 0;\n };\n \n isPoss(tot/2 , ids , 0);\n \n for(int&mask : poss){\n \n vector<int>s1 , s2;\n \n for(int i = 0 ; i < n ; ++i){\n if(mask&(1<<i)) s1.push_back(n-1-i);\n else s2.push_back(n-1-i);\n }\n \n int ok1 = isPoss(tot/4 , s1 , 1);\n int ok2 = isPoss(tot/4 , s2 , 1);\n \n if(ok1 and ok2) return 1;\n }\n \n return 0;\n }\n};", "memory": "19648" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool helper(int sum, int bitmask, int tar, int cnt, vector<int>& matchsticks, vector<int>& dp) {\n if (cnt == 4) {\n return true;\n }\n if (dp[bitmask] != -1) {\n return dp[bitmask];\n }\n if (sum == tar) {\n return dp[bitmask] = helper(0, bitmask, tar, cnt + 1, matchsticks, dp);\n }\n \n bool ans = false;\n for (int i = 0; i < matchsticks.size(); i++) {\n if ((bitmask & (1 << i)) == 0 && sum + matchsticks[i] <= tar) {\n int temp = bitmask;\n temp |= (1 << i);\n ans = ans| helper(sum + matchsticks[i], temp, tar, cnt, matchsticks, dp);\n \n }\n }\n \n return dp[bitmask] = ans;\n }\n\n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n int maxi = *max_element(matchsticks.begin(), matchsticks.end());\n if (sum % 4 != 0) {\n return false;\n }\n if (sum / 4 < maxi) {\n return false;\n }\n\n int n = matchsticks.size();\n vector<int> dp(1 << n, -1);\n return helper(0, 0, sum / 4, 0, matchsticks, dp);\n }\n};\n", "memory": "21414" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(std::vector<int> &matchsticks, int n, int k, int curSum, int mask, int target, std::vector<int> &dpTable) {\n if (mask == (1 << n) - 1) return true;\n\n if (dpTable[mask] != -1) return dpTable[mask];\n\n bool result = false;\n for (int next = 0; next < n; ++next) {\n if (mask & (1 << next)) continue;\n\n if (matchsticks[next] + curSum == target) {\n result |= solve(matchsticks, n, k - 1, 0, mask | (1 << next), target, dpTable);\n }\n else if (matchsticks[next] + curSum < target) {\n result |= solve(matchsticks, n, k, matchsticks[next] + curSum, mask | (1 << next), target, dpTable);\n }\n }\n return dpTable[mask] = result;\n }\n bool makesquare(vector<int>& matchsticks) {\n int sum = std::accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if (sum % 4 != 0) return false;\n\n int target = sum / 4;\n auto maxElement = *std::max_element(matchsticks.begin(), matchsticks.end());\n if (maxElement > target) return false;\n\n int n = static_cast<int>(matchsticks.size());\n std::vector<int> dpTable(1 << n, -1);\n return solve(matchsticks, n, 4, 0, 0, target, dpTable);\n }\n};", "memory": "21414" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& nums) {\n if (nums.empty() || nums.size() < 4) return false;\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if (sum % 4 != 0) return false;\n int n = nums.size(), all = (1 << n) - 1, target = sum / 4;\n vector<int> masks, validHalf(1 << n, false);\n for (int i = 0; i <= all; ++i) {\n int curSum = 0;\n for (int j = 0; j <= 15; ++j) {\n if ((i >> j) & 1) curSum += nums[j];\n }\n if (curSum == target) {\n for (int mask : masks) {\n if ((mask & i) != 0) continue;\n int half = mask | i;\n validHalf[half] = true;\n if (validHalf[all ^ half]) return true;\n }\n masks.push_back(i);\n }\n }\n return false;\n }\n};", "memory": "23180" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nbool makesquare(vector<int>& nums) {\nif (nums.empty() || nums.size() < 4) return false;\nint sum = accumulate(nums.begin(), nums.end(), 0);\nif (sum % 4 != 0) return false;\nint n = nums.size(), all = (1 << n) - 1, target = sum / 4;\nvector<int> masks, validHalf(1 << n, false);\nfor (int i = 0; i <= all; ++i) {\nint curSum = 0;\nfor (int j = 0; j <= 15; ++j) {\nif ((i >> j) & 1) curSum += nums[j];\n}\nif (curSum == target) {\nfor (int mask : masks) {\nif ((mask & i) != 0) continue;\nint half = mask | i;\nvalidHalf[half] = true;\nif (validHalf[all ^ half]) return true;\n}\nmasks.push_back(i);\n}\n}\nreturn false;\n}\n};", "memory": "23180" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> dp;\n bool rec(int submask , vector<int> & matchsticks , int req){\n int n = matchsticks.size();\n if(submask == (1 << n) - 1){\n return true;\n }\n if(dp[submask] != -1){\n return dp[submask];\n }\n for(int i = 0 ; i < (1 << n) ; i++){\n if((submask & (i)) != submask){\n continue;\n }\n else{\n int sum = 0;\n for(int j = 0 ; j < n ; j++){\n if(((1 << j) & submask) == 0){\n if(((1 << j) & i) != 0){\n sum += matchsticks[j];\n }\n }\n }\n if(sum == req){\n dp[submask] = rec(i , matchsticks , req);\n if(dp[submask] == 1){\n return true;\n }\n }\n } \n }\n dp[submask] = 0;\n return false;\n }\n bool makesquare(vector<int>& matchsticks) {\n \n int n = matchsticks.size();\n int sum = 0;\n for(int i = 0 ; i < n ; i++){\n sum += matchsticks[i];\n }\n if(sum % 4 != 0){\n return false;\n }\n int req = sum/4;\n int num = (1 << 15) + 1;\n dp = vector<int>(num , -1);\n bool ans = rec(0 , matchsticks , req);\n return ans;\n }\n};", "memory": "24946" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "// class Solution {\n// public:\n// bool helper(vector<int>& matchsticks , vector<int> &sides , int index , int val){\n// if(index==matchsticks.size()){\n// return true;\n// }\n// for(int i=0 ; i<4 ; i++){\n// if(sides[i] + matchsticks[index] <= val){\n// sides[i]+=matchsticks[index];\n// if(helper(matchsticks , sides , index+1 , val)){\n// return true;\n// }\n// sides[i]-=matchsticks[index];\n// }\n// }\n// return false;\n// }\n// bool makesquare(vector<int>& matchsticks) {\n// int sum=0;\n// for(auto el : matchsticks){\n// sum+=el;\n// }\n// int val=0;\n// if(sum%4!=0){\n// return false;\n// }\n// val=sum/4;\n// vector<int> sides(4 , 0);\n// return helper(matchsticks , sides , 0 , val);\n// }\n// };\n// class Solution {\n// public:\n// bool dp(vector<int>& matchsticks, vector<int>& sides, int index, int target) {\n// if (index == matchsticks.size()) {\n// // Check if all sides are equal to the target length\n// return sides[0] == target && sides[1] == target && sides[2] == target && sides[3] == target;\n// }\n\n// // Try to place the current matchstick in any of the 4 sides\n// for (int i = 0; i < 4; ++i) {\n// if (sides[i] + matchsticks[index] <= target) {\n// sides[i] += matchsticks[index];\n\n// if (dp(matchsticks, sides, index + 1, target)) {\n// return true;\n// }\n\n// sides[i] -= matchsticks[index]; // backtrack\n// }\n\n// // If one side is 0 and we can't place the matchstick, there's no point in trying other sides\n// if (sides[i] == 0) break;\n// }\n\n// return false;\n// }\n\n// bool makesquare(vector<int>& matchsticks) {\n// int sum = 0;\n// for (int el : matchsticks) {\n// sum += el;\n// }\n\n// // If the total sum is not divisible by 4, it's impossible to form a square\n// if (sum % 4 != 0) {\n// return false;\n// }\n\n// int target = sum / 4;\n// vector<int> sides(4, 0);\n \n// // Sort the matchsticks in descending order to optimize backtracking\n// sort(matchsticks.rbegin(), matchsticks.rend());\n\n// // Start the backtracking process from the first matchstick\n// return dp(matchsticks, sides, 0, target);\n// }\n// };\nclass Solution {\npublic:\n unordered_map<int, bool> memo;\n \n bool dp(vector<int>& matchsticks, int usedMask, int currentSum, int target, int sidesFormed) {\n // If 3 sides are formed, the last side must also be the same, so return true\n if (sidesFormed == 3) return true;\n \n // Check if the current state has been computed before\n if (memo.find(usedMask) != memo.end()) {\n return memo[usedMask];\n }\n \n // Try placing each unused matchstick\n for (int i = 0; i < matchsticks.size(); ++i) {\n if (usedMask & (1 << i)) continue; // If matchstick i is already used, skip it\n\n // If placing this matchstick doesn't exceed the side length\n if (currentSum + matchsticks[i] <= target) {\n // Try placing matchstick[i] into the current side\n if (dp(matchsticks, usedMask | (1 << i), (currentSum + matchsticks[i]) % target, target, sidesFormed + ((currentSum + matchsticks[i]) == target))) {\n return memo[usedMask] = true;\n }\n }\n }\n\n return memo[usedMask] = false;\n }\n \n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n \n // If the total length is not divisible by 4, we can't form a square\n if (sum % 4 != 0) {\n return false;\n }\n \n int target = sum / 4;\n memo.clear();\n \n // Sort matchsticks in descending order to attempt to place larger sticks first\n sort(matchsticks.rbegin(), matchsticks.rend());\n \n // Start recursive backtracking with memoization\n return dp(matchsticks, 0, 0, target, 0);\n }\n};\n", "memory": "24946" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "\n// //tle\n// class Solution {\n// public:\n// bool f(int i,vector<int> &d, int side, vector<int>& stick){\n// if(i==stick.size() && (d[0] == d[1] && d[1] == d[2] && d[2] == d[3])) return true;\n// if(i==stick.size()) return false;\n\n// for(int j=0;j<4;j++){ \n// if(d[j]+stick[i]>side)continue;\n// d[j]+=stick[i];\n// if (f(i+1,d,side,stick)) return true;\n// d[j]-=stick[i];\n// }\n \n// return false;\n// }\n\n// bool makesquare(vector<int>& stick) {\n// int sum=0;\n// for(auto it: stick) sum+=it;\n// if(sum%4 !=0) return false;\n// int side=sum/4;\n// vector<int>d(4,0);\n \n// return f(0,d,side,stick);\n// }\n// };\n\n\n//\nclass Solution {\npublic:\n unordered_map<int, bool> memo;\n\n bool f(int mask, int sides, int currSum, int sideLength, vector<int>& stick) {\n if (sides == 0) return true; // All sides are filled\n if (currSum == sideLength) return f(mask, sides - 1, 0, sideLength, stick); // Start a new side\n\n if (memo.find(mask) != memo.end()) return memo[mask];\n\n for (int i = 0; i < stick.size(); i++) {\n if (!(mask & (1 << i)) && currSum + stick[i] <= sideLength) {\n if (f(mask | (1 << i), sides, currSum + stick[i], sideLength, stick)) {\n return memo[mask] = true;\n }\n }\n }\n\n return memo[mask] = false;\n }\n\n bool makesquare(vector<int>& stick) {\n int totalLength = accumulate(stick.begin(), stick.end(), 0);\n if (totalLength % 4 != 0) return false;\n\n int sideLength = totalLength / 4;\n sort(stick.rbegin(), stick.rend());\n\n return f(0, 4, 0, sideLength, stick);\n }\n};\n", "memory": "26713" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<int, bool> memo; \n int target; \n \n bool dp(vector<int>& nums, int mask, int currentSum, int sides) {\n if (sides == 4) return true; \n if (memo.count(mask)) return memo[mask]; \n \n for (int i = 0; i < nums.size(); ++i) {\n if (!(mask & (1 << i))) { \n int newSum = currentSum + nums[i];\n if (newSum <= target) { \n if (dp(nums, mask | (1 << i), newSum % target, sides + (newSum == target))) {\n return memo[mask] = true;\n }\n }\n }\n }\n \n return memo[mask] = false;\n }\n \n bool makesquare(vector<int>& matchsticks) {\n int sum = 0;\n for (int num : matchsticks) sum += num;\n if (sum % 4 != 0) return false; \n \n target = sum / 4; \n memo.clear(); \n \n return dp(matchsticks, 0, 0, 0); \n }\n};\n", "memory": "26713" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int n = matchsticks.size();\n int sum = 0;\n for (int m : matchsticks) sum += m;\n if (sum % 4) return false;\n int target = sum / 4;\n unordered_set<int> memo;\n function<bool(int, int, int)> dfs = [&] (int cur, int vis, int edge) {\n if (memo.contains(vis)) return false;\n if (edge == target) {\n if (vis == (1<<n) - 1) return true;\n return dfs(0, vis, 0);\n }\n for (int i = cur; i < n; i++) {\n if (vis & (1 << i) || matchsticks[i] + edge > target) continue;\n if (dfs(i + 1, vis | (1 << i), edge + matchsticks[i])) return true;\n }\n memo.insert(vis);\n return false;\n };\n return dfs(0, 0, 0);\n }\n};", "memory": "28479" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if(sum % 4) return false;\n int side = sum / 4;\n\n \n vector<unordered_set<unsigned long>> memo(4);\n\n function<bool(bitset<15>, int)> dfs =\n [&dfs, matchsticks, side, &memo](bitset<15> used, int completed) -> bool {\n // If we have built all four sides and used all matchsticks, it is possible\n // Otherwise, no\n if(completed == 4) return used.count() == matchsticks.size();\n\n if(memo[completed].count(used.to_ulong())) return false;\n\n int built_perimeter = 0;\n for(int i = 0; i < 15; i++) if(used[i]) built_perimeter += matchsticks[i];\n\n // Compute how much we have left for this side\n int this_side = built_perimeter - completed * side;\n int remainder = side - this_side;\n\n // Try each\n for(int i = 0; i < matchsticks.size(); i++) {\n if(used[i]) continue;\n int matchstick = matchsticks[i];\n if(matchstick > remainder) continue;\n\n used[i] = true;\n if(dfs(used, matchstick == remainder ? completed + 1 : completed)) return true;\n used[i] = false;\n }\n\n memo[completed].insert(used.to_ulong());\n return false;\n };\n\n bitset<15> used;\n return dfs(used, 0);\n }\n};", "memory": "28479" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\n map<pair<int,int>,bool> dp;\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int n=matchsticks.size();\n if(n<4) return 0;\n\n int sum=0;\n for(int i=0;i<n;i++)\n {\n sum+=matchsticks[i];\n }\n\n if(sum%4) return 0;\n\n return f(matchsticks,(1<<15)-1,0,sum/4);\n }\n\n bool f(vector<int> &a, int masks, int side, int t)\n {\n int sum=0;\n int n=a.size();\n\n for(int i=n-1;i>=0;i--)\n {\n if(((1<<i)&masks)==0) sum+=a[n-1-i];\n }\n\n if(sum && sum%t==0) side++;\n\n if(side==3) return 1;\n\n if(dp.find({masks,side})!=dp.end()) return dp[{masks,side}];\n \n int rem=t-(sum)%t;\n\n for(int i=n-1;i>=0;i--)\n {\n if(a[n-1-i]<=rem && (masks&(1<<i)))\n {\n if(f(a,masks^(1<<i),side,t))\n {\n return dp[{masks,side}]=1;\n }\n }\n }\n\n return dp[{masks,side}]=0;\n }\n};", "memory": "30245" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\n map<pair<int,int>,bool> dp;\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int n=matchsticks.size();\n if(n<4) return 0;\n\n int sum=0;\n for(int i=0;i<n;i++)\n {\n sum+=matchsticks[i];\n }\n\n if(sum%4) return 0;\n\n return f(matchsticks,(1<<15)-1,0,sum/4);\n }\n\n bool f(vector<int> &a, int masks, int side, int t)\n {\n int sum=0;\n int n=a.size();\n\n for(int i=n-1;i>=0;i--)\n {\n if(((1<<i)&masks)==0) sum+=a[n-1-i];\n }\n\n if(sum && sum%t==0) side++;\n\n if(side==3) return 1;\n\n if(dp.find({masks,side})!=dp.end()) return dp[{masks,side}];\n \n int rem=t*(sum/t+1)-sum;\n\n for(int i=n-1;i>=0;i--)\n {\n if(a[n-1-i]<=rem && (masks&(1<<i)))\n {\n if(f(a,masks^(1<<i),side,t))\n {\n return dp[{masks,side}]=1;\n }\n }\n }\n\n return dp[{masks,side}]=0;\n }\n};", "memory": "30245" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int side;\n vector<int> dp;\n bool solve(vector<int> &a, int k, int curr, int mask, int i){\n if(k == 1) return true;\n if(i>=a.size()) return false;\n\n if(dp[mask] != -1) return false;\n\n if(curr == side) return dp[mask] = solve(a, k-1, 0, mask, 0);\n \n for(int j = i; j<a.size(); j++){\n if(!(mask&(1<<j)) && a[j]+curr<=side && solve(a, k, a[j]+curr, mask | (1<<j), j+1)) return dp[mask] = true;\n }\n return dp[mask] = false;\n }\n\n bool makesquare(vector<int>& a) {\n int sum = accumulate(a.begin(), a.end(), 0);\n side = sum/4;\n if(sum%4 != 0) return false;\n dp.resize(1<<(a.size()+1), -1);\n return solve(a, 4, 0, 0, 0);\n }\n};", "memory": "32011" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int n=matchsticks.size();\n int sum=0;\n for(auto it:matchsticks){\n sum+=it;\n }\n if(sum%4!=0)return false;\n int target=sum/4;\n vector<pair<int,int>>dp(1<<n,{1e9,1e9});\n dp[0]={1,0};\n for(int mask=1;mask<(1<<n);mask++){\n for(int j=0;j<n;j++){\n if(mask&(1<<j)){\n int pre=mask^(1<<j);\n if(dp[pre].second+matchsticks[j]<=target){\n dp[mask]=min(dp[mask],{dp[pre].first,dp[pre].second+matchsticks[j]});\n }\n else{\n dp[mask]=min(dp[mask],{dp[pre].first+1,matchsticks[j]});\n }\n }\n }\n }\n if(dp[(1<<n)-1].first==4 && dp[(1<<n)-1].second==target)return true;\n return false;\n }\n};", "memory": "32011" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matchsticks) {\n long totalLength = 0;\n for(int &len : matchsticks){\n totalLength += len;\n }\n if(totalLength%4 != 0) return false;\n\n int sideLength = totalLength/4;\n long state = 0;\n unordered_set<long> combos;\n unordered_set<long> visited;\n createAllPossibleCombos(matchsticks, sideLength, state, combos, visited);\n\n bool ans = false;\n long targetState = (1 << matchsticks.size()) - 1;\n vector<unordered_set<long>> visitedCombo(4);\n checkPossibility(combos, visitedCombo, 0, targetState, 0, ans);\n return ans;\n }\n\n void checkPossibility(\n unordered_set<long> &combos, \n vector<unordered_set<long>> &visited,\n long state, \n long &targetState, \n int level, \n bool &ans\n ){\n\n //cout<<\"level: \"<<level<<\" state: \"<<state<<\" target: \"<<targetState<<endl;\n if(level > 3){\n if(level == 4 && state == targetState) ans = true;\n return;\n }\n\n if(visited[level].find(state) != visited[level].end()) return;\n visited[level].insert(state);\n\n auto itr = combos.begin();\n while(itr != combos.end()){\n if((state^(*itr)) > state){\n checkPossibility(combos, visited, state | (*itr), targetState, level+1, ans);\n if(ans) break;\n }\n\n itr++;\n }\n }\n\n void createAllPossibleCombos(\n vector<int> &matchsticks, \n int target, \n long state, \n unordered_set<long> &combos,\n unordered_set<long> &visited\n ){\n if(visited.find(state) != visited.end()) return;\n visited.insert(state);\n\n if(target == 0){\n // cout<<\"inserting \"<<state<<endl;\n combos.insert(state);\n return;\n }\n if(target < 0) return;\n\n for(int i=0;i<matchsticks.size();i++){\n if(target < matchsticks[i]) continue;\n createAllPossibleCombos(matchsticks, target - matchsticks[i], state | (1<<i), combos, visited);\n }\n }\n};", "memory": "33778" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matchsticks) {\n long totalLength = 0;\n for(int &len : matchsticks){\n totalLength += len;\n }\n if(totalLength%4 != 0) return false;\n\n int sideLength = totalLength/4;\n long state = 0;\n unordered_set<long> combos;\n unordered_set<long> visited;\n createAllPossibleCombos(matchsticks, sideLength, state, combos, visited);\n\n bool ans = false;\n long targetState = (1 << matchsticks.size()) - 1;\n vector<unordered_set<long>> visitedCombo(4);\n checkPossibility(combos, visitedCombo, 0, targetState, 0, ans);\n return ans;\n }\n\n void checkPossibility(\n unordered_set<long> &combos, \n vector<unordered_set<long>> &visited,\n long state, \n long &targetState, \n int level, \n bool &ans\n ){\n\n //cout<<\"level: \"<<level<<\" state: \"<<state<<\" target: \"<<targetState<<endl;\n if(level > 3){\n if(level == 4 && state == targetState) ans = true;\n return;\n }\n\n if(visited[level].find(state) != visited[level].end()) return;\n visited[level].insert(state);\n\n auto itr = combos.begin();\n while(itr != combos.end()){\n if((state^(*itr)) > state){\n //cout<<\"adding \"<<(*itr)<<\" \"<<(state | (*itr))<<endl;\n checkPossibility(combos, visited, state | (*itr), targetState, level+1, ans);\n if(ans) break;\n }\n\n itr++;\n }\n }\n\n void createAllPossibleCombos(\n vector<int> &matchsticks, \n int target, \n long state, \n unordered_set<long> &combos,\n unordered_set<long> &visited\n ){\n if(visited.find(state) != visited.end()) return;\n visited.insert(state);\n\n if(target == 0){\n // cout<<\"inserting \"<<state<<endl;\n combos.insert(state);\n return;\n }\n if(target < 0) return;\n\n for(int i=0;i<matchsticks.size();i++){\n if(target < matchsticks[i]) continue;\n createAllPossibleCombos(matchsticks, target - matchsticks[i], state | (1<<i), combos, visited);\n }\n }\n};", "memory": "33778" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<string, int> mp;\n int sl = 0;\n\npublic:\n bool makesquare(vector<int>& m) {\n int totalLength = accumulate(m.begin(), m.end(), 0);\n if (totalLength % 4 != 0) return false;\n sl = totalLength / 4;\n sort(m.rbegin(), m.rend());\n vector<int> ans(4, 0);\n return f(0, m, ans);\n }\n\n bool f(int i, vector<int>& m, vector<int> ans) {\n if (i == m.size()) {\n return ans[0] == sl && ans[1] == sl &&\n ans[2] == sl && ans[3] == sl;\n }\n\n string state = getState(ans);\n if (mp.find(state) != mp.end()) {\n return mp[state];\n }\n\n for (int j = 0; j < 4; j++) {\n if (ans[j] + m[i] <= sl) {\n ans[j] += m[i];\n if (f(i + 1, m, ans)) {\n return mp[state] = true;\n }\n ans[j] -= m[i];\n }\n if (ans[j] == 0) break;\n }\n\n return mp[state] =false;\n }\n\n string getState(vector<int>& sides) {\n return to_string(sides[0]) + \"-\" + to_string(sides[1]) + \"-\" +\n to_string(sides[2]) + \"-\" + to_string(sides[3]);\n }\n};", "memory": "35544" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // bool solve(int i, int n, int sum, int target, int k, vector<int>&v, vector<int>&vis) {\n // if (k == 0) return true;\n // if (sum == target) return solve(0, n, 0, target, k - 1, v, vis);\n\n // for (int j = i; j < n; ++j) {\n // if (vis[j] && sum + v[j] <= target ) {\n // vis[i] = 0;\n // if (solve(i + 1, n, sum + v[j], target, k, v, vis)) {\n // return true;\n // }\n // vis[i] = 1;\n // if (sum == 0) {\n // return false;\n // }\n // }\n // }\n // return false;\n // }\n\n bool solve(int start,int sum,int target,int n,int k,vector<int>nums,vector<int>&vis){\n if(k==0) return true;\n\n if(sum==target){\n return solve(0,0,target,n,k-1,nums,vis);\n }\n\n for(int i=start;i<n;++i){\n if(vis[i] && sum+nums[i]<=target){\n vis[i]=0;\n if(solve(i+1,sum+nums[i],target,n,k,nums,vis)){\n return true;\n }\n vis[i]=1;\n if(sum==0){\n return false;\n }\n }\n }\n\n return false;\n }\n\n bool canPartitionKSubsets(vector<int>& nums, int k) {\n int n=nums.size();\n int sum=0;\n for(int i:nums) sum+=i;\n if(sum%k!=0){\n return false;\n }\n for(int i:nums) {\n if(i>sum/k) return false;\n }\n int val=sum/k;\n vector<int>vis(n,1);\n bool ans=solve(0,0,val,n,k,nums,vis);\n return ans;\n }\n\n bool makesquare(vector<int>& matchsticks) {\n return canPartitionKSubsets(matchsticks,4);\n // int side = 0;\n // for (int i : matchsticks) side += i;\n \t// for (int i : matchsticks) if (i > side / 4) return false ;\n // if (side % 4 != 0) return false;\n // vector<int>vis(matchsticks.size(), 1);\n // return solve(0, matchsticks.size(), 0, side / 4, 4, matchsticks, vis);\n }\n\n};", "memory": "35544" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matc) {\n sort(matc.begin(), matc.end(), greater<int>()); \n int summ = 0;\n for (int i = 0; i < matc.size(); i++) {\n summ += matc[i];\n }\n if (summ % 4 != 0)\n return false;\n\n int sle = summ / 4;\n vector<vector<vector<int>>> dp(matc.size() + 1);\n dp[0].push_back({0, 0, 0, 0});\n\n for (int i = 0; i < matc.size(); i++) {\n set<vector<int>> see;\n for (int j = 0; j < dp[i].size(); j++) { \n for (int k = 0; k < 4; k++) {\n vector<int> r(dp[i][j]);\n r[k] += matc[i];\n if (r[k] > sle)\n continue; \n sort(r.begin(), r.end());\n see.insert(r);\n }\n }\n dp[i + 1].assign(see.begin(), see.end());\n }\n // for(int i=0;i<dp[matc.size()].size();i++){\n // for(int j=0;j<4;j++){\n // cout<<dp[matc.size()][i][j]<<\", \";\n // }\n // }\n // cout<<endl;\n for (int i = 0; i < dp[matc.size()].size(); i++) {\n bool valid = true;\n for (int j = 0; j < 4; j++) {\n if (dp[matc.size()][i][j] != sle) {\n valid = false;\n break;\n }\n }\n if (valid)\n return true;\n }\n return false;\n }\n};", "memory": "37310" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(vector<int> nums,vector<bool>& visited,int currsum,int idx,int subsetsum,int k){\n if(k==0)return true;\n if(currsum>subsetsum)return false;\n if(currsum==subsetsum){\n return solve(nums,visited,0,0,subsetsum,k-1);\n }\n for(int i=idx;i<nums.size();i++){\n if(visited[i])continue;\n visited[i] = true;\n if(solve(nums,visited,currsum+nums[i],i+1,subsetsum,k))return true;\n visited[i] = false;\n if(currsum==0)break;\n }\n return false;\n }\n bool makesquare(vector<int>& nums) {\n int k=4;\n int n = nums.size();\n if(k > n)return false;\n int sum=0;\n for(auto n : nums)\n sum += n;\n if (nums.size() < k || sum % k) return false;\n int subsetsum = sum/k;\n vector<bool> v(n,false);\n sort(nums.begin(), nums.end(), greater<int>());\n if(nums[0]>subsetsum)\n return false;\n\n return solve(nums,v,0,0,subsetsum,k);\n }\n \n};", "memory": "37310" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<string, bool> map; \n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if (sum % 4 != 0) return false;\n int target = sum / 4; \n //vector<bool> used(matchsticks.size(), false);\n string used(matchsticks.size(), '0'); \n return backtrack(matchsticks, 0, 0, used, target, 0); \n }\n\n\n bool backtrack(vector<int> &matchsticks, int curSum, \n int edges, string &used, int target, int num_used) {\n if (edges == 4) return true;\n\n if (map.find(used) != map.end()) return map[used]; \n\n if (curSum > target) return false; \n\n if (curSum == target) {\n return backtrack(matchsticks, 0, edges+1, used, target, num_used); \n } \n\n if (curSum < target) {\n for (int i = 0; i < used.size(); i++) {\n if (used[i] == '0') {\n curSum += matchsticks[i]; \n used[i] = '1'; \n if (backtrack(matchsticks, curSum, edges, used, target, num_used+1)) { \n \n return map[used] = true; \n\n }\n \n used[i] = '0'; \n curSum -= matchsticks[i]; \n }\n }\n }\n\n return map[used] = false; \n }\n};", "memory": "39076" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool helper(int index, int curr_k, int k, int currSum, int targetSum, string &picked, vector<int> &matchsticks, unordered_map<string, bool> &dp) {\n if(curr_k == k - 1) {\n return true;\n }\n if(currSum > targetSum) {\n return false;\n }\n if(dp.find(picked) != dp.end()) {\n return dp[picked];\n }\n if(currSum == targetSum) {\n return dp[picked] = helper(0, curr_k+1, k, 0, targetSum, picked, matchsticks, dp);\n }\n\n for(int i=index;i<matchsticks.size();i++) {\n if(picked[i] == '0') {\n picked[i] = '1';\n if(helper(i+1, curr_k, k, currSum + matchsticks[i], targetSum, picked, matchsticks, dp)) {\n return dp[picked] = true;\n }\n picked[i] = '0';\n \n }\n }\n\n return dp[picked] = false;\n }\n\n bool makesquare(vector<int>& matchsticks) {\n int totalSum = 0;\n for(int i=0;i<matchsticks.size();i++) {\n totalSum += matchsticks[i];\n }\n if(totalSum % 4 != 0) {\n return false;\n }\n int targetSum = totalSum/4;\n string picked(matchsticks.size(), '0');\n unordered_map<string, bool> dp;\n sort(matchsticks.begin(), matchsticks.end(), greater<int>());\n return helper(0, 0, 4, 0, targetSum, picked, matchsticks, dp);\n }\n};", "memory": "39076" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "const int SIDES = 4;\nconst int MAX_LENGTH = 15;\nint SIZE;\nint SIDE_SIZE;\n\n\nclass Solution {\npublic:\n bool dfs(const std::vector<int>& xs, std::bitset<MAX_LENGTH> mask, int side, int c, std::unordered_map<int, bool>& memo)\n { \n\n if(c == 4)\n return true;\n\n if(side > SIDE_SIZE)\n return false;\n \n if(auto i = memo.find((int)mask.to_ulong()); i != memo.end())\n return (*i).second; \n\n bool ans = false;\n for(int i = 0; i < SIZE && !mask.all() && !ans; i++)\n {\n if(mask.test(i)) continue;\n mask.set(i);\n if(side + xs[i] == SIDE_SIZE)\n ans |= memo[(int)mask.to_ulong()] = dfs(xs, mask, 0, c + 1, memo);\n ans |= memo[(int)mask.to_ulong()] = dfs(xs, mask, side + xs[i], c, memo);\n mask.set(i, false);\n }\n return ans;\n }\n bool makesquare(vector<int>& matchsticks) {\n SIZE = matchsticks.size();\n int perimeter = std::reduce(matchsticks.begin(), matchsticks.end());\n if(SIZE < 4 || (perimeter % SIDES))\n return false;\n SIDE_SIZE = perimeter / SIDES;\n std::sort(matchsticks.begin(), matchsticks.end(), std::greater<int>());\n std::bitset<MAX_LENGTH> mask;\n std::unordered_map<int, bool> memo;\n return dfs(matchsticks, mask, 0, 0, memo);\n }\n};", "memory": "40843" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(vector<int> &arr,int n,int k,vector<int> ref,\n int ind,int s){\n //base case:\n if(ind==n){\n for(int i=1;i<=k;i++){\n if(ref[i]!=s){\n return false;\n }\n }\n return true;\n }\n //assign matchsticks to the sides:\n bool ans=false;\n for(int j=1;j<=k;j++){\n int given=0;\n // if(ref[j]==0){\n // given=1;\n // }\n if(ref[j]+arr[ind]>s){\n continue;\n }\n // if(ref[j]==0){\n // given=1;\n // }\n //given ind-th stick to jth side:\n ref[j]=ref[j]+arr[ind];\n bool temp=solve(arr,n,k,ref,ind+1,s);\n if(temp==true){\n return true;\n }\n //ans=ans|temp;\n //backtrack:\n ref[j]=ref[j]-arr[ind];\n if(ref[j]==0){\n break;\n }\n }\n return ans;\n }\n bool makesquare(vector<int>& arr) {\n int n=arr.size();\n int k=4;\n int sum=0;\n for(int i=0;i<n;i++){\n sum=sum+arr[i];\n }\n if(n<k||sum%k>0){\n return false;\n }\n int s=sum/k;\n if(*max_element(arr.begin(),arr.end())>s){\n return false;\n }\n sort(arr.begin(),arr.end(),greater<int>());\n //int s=sum/k;\n vector<int> ref(k+1,0);\n int ind=0;\n bool ans=solve(arr,n,k,ref,ind,s);\n return ans;\n }\n};", "memory": "40843" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef vector<int> I;\n int target;\n\n bool bt(I m, I s, int idx) {\n if (idx == m.size()) {\n for (int i : s) {\n if (i != target) {\n return false;\n }\n }\n return true;\n }\n for (int i = 0 ; i < 4; i++) {\n if (i > 0 && s[i] == s[i-1]) {\n continue;\n }\n if (s[i] + m[idx] > target) {\n continue;\n }\n s[i] += m[idx];\n if (bt(m,s,idx + 1)) {\n return true;\n }\n s[i] -= m[idx];\n }\n return false;\n }\n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if (sum % 4 != 0) {\n return false;\n }\n target = sum / 4;\n I sides(4, 0);\n sort(matchsticks.begin(), matchsticks.end(), greater<int>());\n return bt(matchsticks, sides, 0);\n }\n};", "memory": "42609" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef vector<int> I;\n int target;\n\n bool bt(I m, I s, int idx) {\n if (idx == m.size()) {\n for (int i : s) {\n if (i != target) {\n return false;\n }\n }\n return true;\n }\n for (int i = 0 ; i < 4; i++) {\n if (i > 0 && s[i] == s[i-1]) {\n continue;\n }\n if (s[i] + m[idx] > target) {\n continue;\n }\n s[i] += m[idx];\n if (bt(m,s,idx + 1)) {\n return true;\n }\n s[i] -= m[idx];\n }\n return false;\n }\n bool makesquare(vector<int>& matchsticks) {\n ios_base :: sync_with_stdio(0);\n cin.tie(NULL);\n cout.tie(NULL);\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if (sum % 4 != 0) {\n return false;\n }\n target = sum / 4;\n I sides(4, 0);\n sort(matchsticks.begin(), matchsticks.end(), greater<int>());\n return bt(matchsticks, sides, 0);\n }\n};", "memory": "44375" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string getState(int s1,int s2,int s3,int s4){\n vector<int> arr = {s1,s2,s3,s4};\n sort(arr.begin(),arr.end());\n string state=\"\";\n for(int i:arr){\n state+=to_string(i)+\":\";\n }\n return state;\n }\n bool dfs(int i,int s1,int s2,int s3,int s4,vector<int>& matchsticks,vector<map<string,bool>>& dp){\n if(i==matchsticks.size()) return s1==0 && s2==0 && s3==0 && s4==0;\n\n string state = getState(s1,s2,s3,s4);\n\n if(dp[i].find(state)!=dp[i].end()) return dp[i][state];\n\n bool ans = false;\n\n int v = matchsticks[i];\n if(s1-v>=0) ans = ans or dfs(i+1,s1-v,s2,s3,s4,matchsticks,dp);\n if(s2-v>=0) ans = ans or dfs(i+1,s1,s2-v,s3,s4,matchsticks,dp);\n if(s3-v>=0) ans = ans or dfs(i+1,s1,s2,s3-v,s4,matchsticks,dp);\n if(s4-v>=0) ans = ans or dfs(i+1,s1,s2,s3,s4-v,matchsticks,dp);\n\n\n\n return dp[i][state]=ans;\n }\n bool makesquare(vector<int>& matchsticks) {\n long long int s = 0;\n for(int i:matchsticks){\n s+=i;\n }\n if(s%4!=0) return false;\n sort(matchsticks.rbegin(),matchsticks.rend());\n vector<map<string,bool>> dp(matchsticks.size());\n return dfs(0,s/4,s/4,s/4,s/4,matchsticks,dp); \n }\n};", "memory": "46141" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& A) {\n long sideSum = accumulate(A.begin(), A.end(), 0L);\n int n = A.size();\n \n // Check if the total length can be evenly divided into 4 sides\n if (sideSum % 4 != 0 || *max_element(A.begin(), A.end()) > sideSum / 4) \n return false;\n \n sideSum /= 4;\n sort(A.begin(), A.end(), greater<>()); // Sort in descending order\n \n // Array to store the current sum of each side\n long sides[4] = {};\n \n function<bool(int)> dfs = [&](int index) {\n if (index == n) return true; // All matchsticks used\n \n unordered_set<long> visited; // Track visited states to avoid redundant checks\n \n for (int i = 0; i < 4; ++i) {\n // Skip if adding the current matchstick exceeds the side length\n // or if the current side state has been visited\n if (A[index] + sides[i] > sideSum || visited.count(sides[i])) \n continue;\n \n visited.insert(sides[i]);\n sides[i] += A[index]; // Add matchstick to the current side\n \n if (dfs(index + 1)) return true; // Recurse to place the next matchstick\n \n sides[i] -= A[index]; // Backtrack if placement fails\n }\n \n return false; // No valid configuration found\n };\n \n return dfs(0); // Start recursion with the first matchstick\n }\n};\n", "memory": "47908" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int solve(int cur,int idx,int mask,vector<int>&arr,int n,int l){\n if(idx==4) return true;\n if(dp[idx][mask]!=-1) return dp[idx][mask];\n bool ans = false;\n for(int i = 0; i < n; i++){\n if((mask&(1<<i))==0){\n if(cur+arr[i]==l){\n bool chk = solve(0,idx+1,mask|(1<<i),arr,n,l);\n if(chk==true){\n ans = true;\n break;\n }\n }else if(cur+arr[i] < l){\n bool chk = solve(cur+arr[i],idx,mask|(1<<i),arr,n,l);\n if(chk==true){\n ans = true;\n break;\n }\n }\n }\n }\n\n return dp[idx][mask] = ans;\n }\n bool makesquare(vector<int>& arr) {\n int n = arr.size();\n long long sum = 0;\n for(int i:arr) sum+=i;\n if(sum%4!=0) return false;\n int l = sum/4;\n for(int i:arr){\n if(i>l) return false;\n }\n dp.resize(4,vector<int>(1<<n,-1));\n bool ans = solve(0,0,0,arr,n,l);\n\n return ans;\n }\n};", "memory": "49674" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\n vector<vector<int>> dp;\n bool check(int side, vector<int>& m, int mask, int curr, int count) {\n if (count == 4) {\n return true;\n }\n\n if (dp[count][mask] != -1) {\n return dp[count][mask];\n }\n\n bool ans = false;\n for (int i = 0; i < m.size(); i++) {\n int cmask = 1 << i;\n if ((cmask & mask) == 0) {\n if (curr + m[i] == side) {\n ans = ans || check(side, m, cmask | mask, 0, count + 1);\n } else if (curr + m[i] < side) {\n ans =\n ans || check(side, m, cmask | mask, curr + m[i], count);\n }\n }\n }\n\n return dp[count][mask] = ans;\n }\n\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int perm = 0;\n unordered_map<int, int> mp;\n\n for (const auto matchStick : matchsticks) {\n mp[matchStick]++;\n perm += matchStick;\n }\n\n if (perm % 4 > 0) {\n return false;\n }\n\n int side = perm / 4;\n\n dp = vector<vector<int>>(4, vector<int>(1 << matchsticks.size(), -1));\n\n return check(side, matchsticks, 0, 0, 0);\n }\n};", "memory": "58505" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int n = matchsticks.size();\n int tot = 0;\n vector<int>sum(1<<n);\n vector<int>dp(1<<n,-69);\n dp[0] = 0;\n for(int nxt: matchsticks){\n tot+=nxt;\n }\n if(tot%4 != 0){\n return false;\n }\n tot/=4;\n for(int i = 1; i<(1<<n); i++){\n for(int j = 0; j<n; j++){\n if(i&(1<<j)){\n sum[i]+=matchsticks[j];\n }\n }\n }\n for(int i = 1; i<(1<<n); i++){\n for(int j = i; j>0; j = (j-1)&i){\n if(sum[j] == tot)dp[i] = max(dp[i],dp[i^j]+1);\n }\n }\n return dp[(1<<n)-1] == 4;\n }\n};", "memory": "60271" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool func(int ind, vector<int>& side, vector<int> matchsticks){\n if(ind==matchsticks.size()){\n if(side[1]==side[2] && side[2]==side[3] && side[3]==side[4] && side[4]==sum/4) return true;\n return false;\n }\n \n if(side[1]>sum/4 || side[2]>sum/4 || side[3]>sum/4 || side[4]>sum/4 ) return false;\n for(int i=1; i<=4 ;i++)\n {\n if(side[i]+matchsticks[ind]>sum/4) continue;\n int j = i;\n while (--j >= 1)\n if (side[i] == side[j]) \n break;\n if (j != 0) continue;\n side[i]+=matchsticks[ind];\n if(func(ind+1, side,matchsticks)) return true;\n side[i]-=matchsticks[ind];\n }\n\n\n return false;\n }\n int sum = 0;\n bool makesquare(vector<int>& matchsticks) {\n vector<int> sides(5,0);\n for(auto it: matchsticks) sum+=it;\n sort(matchsticks.begin(), matchsticks.end(), [](int a, int b){\n return a>b;\n });\n return func(0, sides, matchsticks);\n }\n};", "memory": "62038" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n bool isPossible(vector<int> matchsticks, int target,vector<int> &res, int idx){\n \n if(idx == matchsticks.size()){\n return (res[0] == res[1] && res[1] == res[2] && res[2] == res[3]);\n }\n \n for(int i=0;i<4;i++){\n \n if(res[i] + matchsticks[idx] > target){\n continue;\n }\n \n int j = i-1;\n \n while(j>=0){\n \n if(res[i] == res[j]) break;\n \n j--;\n \n }\n \n if(j != -1)\n continue;\n \n \n res[i] += matchsticks[idx];\n \n if(isPossible(matchsticks,target,res,idx+1)) return true;\n \n res[i] -= matchsticks[idx];\n \n }\n return false;\n }\n \n \n \n bool makesquare(vector<int>& matchsticks) {\n \n //Edge Case\n if(matchsticks.size() == 0)\n return 0;\n \n int sum = accumulate(matchsticks.begin(),matchsticks.end(),0);\n \n int target = sum/4;\n \n vector<int> sides(4,0);\n \n sort(matchsticks.begin(),matchsticks.end(),greater<int>());\n \n return isPossible(matchsticks,target,sides,0);\n \n }\n};", "memory": "63804" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n bool dfs(vector<int> matchsticks, int target, vector<int> &sides, int idx){\n \n // base case\n if(idx == matchsticks.size()){\n if(sides[0] == sides[1] && sides[1] == sides[2] && sides[2] == sides[3]){\n return true;\n }\n return false;\n }\n \n for(int i = 0; i < 4; i++){\n if (sides[i] + matchsticks[idx] > target){ //optimization - 1\n continue;\n }\n int j = i - 1; //optimization - 3\n while (j>=0){\n if(sides[j] == sides[i]){\n break;\n }\n \n j -= 1;\n }\n \n if(j != -1){\n continue;\n }\n \n \n sides[i] += matchsticks[idx];\n \n if( dfs(matchsticks, target, sides, idx+1) ){\n return true;\n }\n sides[i] -= matchsticks[idx];\n }\n \n return false;\n \n \n }\n \n \n bool makesquare(vector<int>& matchsticks) {\n \n if(matchsticks.size() == 0){\n return false;\n }\n \n int sum = 0;\n for(int i = 0; i < matchsticks.size() ; i++){\n sum += matchsticks[i];\n }\n \n int target = sum / 4;\n vector<int> sides(4,0);\n sort(matchsticks.begin(), matchsticks.end(), greater<int>()); // optimization 2\n return dfs(matchsticks, target, sides, 0);\n \n \n }\n}; ", "memory": "65570" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\n using ll = int;\n using Key = tuple<ll, ll, ll, ll>; \n\n template<int i>\n Key update(Key k, int val){\n get<i>(k) += val;\n return k;\n }\n\n set<Key> genSet(int a, const vector<int>& sticks){\n set<Key> s;\n s.insert({0,0,0,0});\n for(auto stick : sticks){\n set<Key> temp;\n for(auto k : s){\n if(get<0>(k) + stick <= a){\n temp.insert(update<0>(k, stick));\n }\n\n if(get<1>(k) + stick <= a){\n temp.insert(update<1>(k, stick));\n }\n\n if(get<2>(k) + stick <= a){\n temp.insert(update<2>(k, stick));\n }\n\n if(get<3>(k) + stick <= a){\n temp.insert(update<3>(k, stick));\n }\n \n }\n s = move(temp);\n }\n return s;\n };\npublic:\n bool makesquare(vector<int>& matchsticks) {\n sort(matchsticks.begin(), matchsticks.end());\n ll S = 0;\n for(auto i : matchsticks)\n S += i;\n if(S % 4)\n return false;\n ll a = S / 4;\n int n = matchsticks.size();\n auto s1 = genSet(a, {matchsticks.begin(), matchsticks.begin() + n / 2});\n auto s2 = genSet(a, {matchsticks.begin() + n / 2, matchsticks.end()});\n cout << s1.size() << \" \" << s2.size() << endl;\n assert(s1.size() < (1 << n) && s2.size() < (1 << n));\n \n for(const auto& k1 : s1){\n if(s2.contains({a - get<0>(k1), a - get<1>(k1), a - get<2>(k1), a - get<3>(k1)})){\n return true;\n }\n }\n return false;\n }\n};", "memory": "67336" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int len = 0;\n int sum = 0;\n sort(matchsticks.begin(), matchsticks.end(), greater<int>());\n for(int& stick: matchsticks)\n {\n sum += stick;\n }\n len = sum / 4;\n if (len * 4 != sum)\n {\n return false;\n }\n for(int stick: matchsticks)\n {\n if (stick > len)\n {\n return false;\n }\n }\n // cout << \"yyyyyyyy\" << endl;\n vector<int> edges(4, 0);\n unordered_set<string> vis;\n function<string(vector<int>)> hash = [&](vector<int> egs) -> string {\n sort(egs.begin(), egs.end());\n return to_string(egs[0]) + \"_\" + to_string(egs[1]) + \"_\" + to_string(egs[2]) + \"_\" + to_string(egs[3]);\n };\n function<bool(int)> dfs = [&](int pos) -> bool {\n for(int i = 0; i < 4; i++)\n {\n if (edges[i] > len)\n {\n return false;\n }\n }\n if (pos == matchsticks.size())\n {\n for(int i = 0; i < 3; i++)\n {\n if (edges[i] != len)\n {\n return false;\n }\n }\n return true;\n }\n for(int i = 0; i < 4; i++)\n {\n edges[i] += matchsticks[pos];\n auto h = hash(edges);\n // cout << h << endl;\n if (vis.find(h) != vis.end())\n {\n edges[i] -= matchsticks[pos];\n // return false;\n continue;\n }\n if (dfs(pos + 1))\n {\n return true;\n }\n vis.insert(h);\n edges[i] -= matchsticks[pos];\n }\n return false;\n };\n return dfs(0);\n }\n};", "memory": "69103" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int target;\n bool rec(int s, int mask, vector<int>& matchsticks, int sum, vector<vector<int>>& dp) {\n if (s == 4 and (mask == (1 << matchsticks.size()) - 1)) return true;\n if (sum > target) return false;\n if (sum == target) return rec(s+1, mask, matchsticks, 0, dp);\n\n if (dp[s][mask] != -1) return dp[s][mask];\n\n for (int i=0; i<matchsticks.size(); i++) {\n if (!(mask & (1 << i))) {\n if (rec(s, mask | (1 << i), matchsticks, sum + matchsticks[i], dp)) {\n return dp[s][mask] = true;\n }\n }\n }\n\n return dp[s][mask] = false;\n }\n bool makesquare(vector<int>& matchsticks) {\n int n = matchsticks.size();\n int total = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n\n if (total % 4 != 0) return false;\n\n target = total / 4;\n\n sort(matchsticks.rbegin(), matchsticks.rend());\n\n vector<vector<int>> dp(5, vector<int>(1 << n, -1));\n return rec(0, 0, matchsticks, 0, dp);\n }\n};", "memory": "70869" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int lim;\n bool makesquare(vector<int>ms,vector<int>&dir,int n,int i){\n \n if(i == n){\n return true;\n }\n\n for(int j=0;j<4;j++){\n if(ms[i]+dir[j] <= lim){\n dir[j]+=ms[i];\n if(makesquare(ms,dir,n,i+1)) return true;\n dir[j]-=ms[i];\n }\n if(dir[j]==0) break;\n }\n return false;\n \n }\n bool makesquare(vector<int>& ms) {\n int sum=0;\n int tot = accumulate(ms.begin(),ms.end(),sum);\n if(tot%4 != 0) return false;\n lim = tot/4;\n vector<int>dir(4,0);\n\n sort(ms.rbegin(),ms.rend());;\n return makesquare(ms,dir,ms.size(),0);\n }\n};", "memory": "72635" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "\n\n\n\n\nclass Solution {\npublic:\n bool Try(vector<int> matchsticks,int value,vector<int>& canh,int pos){\n if(pos==matchsticks.size()){\n return canh[0]==value&&canh[1]==value&&canh[2]==value&&canh[3]==value;\n }\n for(int i=0;i<4;++i){\n if(canh[i]+matchsticks[pos]>value) continue;\n canh[i]+=matchsticks[pos];\n if(Try(matchsticks,value,canh,pos+1)){\n return true;\n }\n canh[i]-=matchsticks[pos];\n if(canh[i]==0) break;\n }\n return false;\n }\n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if(sum%4!=0){\n return false;\n }\n sort(matchsticks.rbegin(),matchsticks.rend());\n int value=sum/4;\n for(int i=0;i<matchsticks.size();i++){\n if(matchsticks[i]>value){\n return false;\n }\n }\n vector<int> canh(4,0);\n return Try(matchsticks,value,canh,0);\n }\n};\n\n\n\n\n", "memory": "72635" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int target;\n unordered_map<int, unordered_map<int, bool>> dp;\n\n bool makesquare(vector<int>& matchsticks) {\n n = matchsticks.size();\n if (n < 4) return false;\n\n int sum = std::accumulate(begin(matchsticks), end(matchsticks), 0);\n if (sum % 4 != 0) return false;\n target = sum / 4;\n\n return check(matchsticks, (1<<n) - 1, 0, 0, 4);\n }\n\n bool check(vector<int>& matchsticks, int mask, int idx, int sum, int k) {\n if (k == 0) return true;\n if (sum == target) return check(matchsticks, mask, 0, 0, k - 1);\n \n if (dp.contains(mask) && dp[mask].contains(k)) return dp[mask][k];\n\n for (int i = 0; i < matchsticks.size(); i++) {\n if (!(mask & (1<<(n - i - 1))) || sum + matchsticks[i] > target) continue;\n mask ^= (1<<(n - i - 1));\n if (check(matchsticks, mask, i + 1, sum + matchsticks[i], k)) return dp[mask][k] = true;\n mask ^= (1<<(n - i - 1));\n }\n return dp[mask][k] = false;\n }\n};", "memory": "74401" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool rec(int i,vector<int> &nums,int k,vector<int> &v,int s)\n {\n int n=nums.size();\n if(i==n)\n {\n for(int j=0;j<k;j++)\n if(v[j]!=v[0]) return false;\n return true;\n }\n bool ans=false;\n unordered_map<int,int> M;\n for(int j=0;j<k;j++)\n {\n if(M[v[j]]) continue;\n M[v[j]]=1;\n if(v[j]+nums[i]>s/k) continue;\n v[j]+=nums[i];\n ans|=rec(i+1,nums,k,v,s);\n v[j]-=nums[i];\n if(ans) return ans;\n }\n return ans;\n }\n\n bool makesquare(vector<int>& nums) {\n int k=4;\n vector<int> v(k,0);\n int s=0;\n if(nums.size()<k) return false;\n for(auto i:nums) s+=i;\n if(s%k!=0) return false;\n sort(nums.begin(),nums.end());\n reverse(nums.begin(),nums.end());\n for(auto i:nums)\n if(i>s/k) return false;\n return rec(0,nums,k,v,s);\n }\n};", "memory": "76168" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool solve(vector<int> nums, int index, int target, vector<int> sides){\n if(index == nums.size()) return true;\n for(int j = 0; j < 4; j++){\n if(sides[j] + nums[index] <= target){\n sides[j] += nums[index];\n if(solve(nums, index + 1, target, sides)) return true;\n sides[j] -= nums[index];\n }\t\n if(sides[j] == 0) return false;\t\n }\t\n return false;\t\n }\n\n bool makesquare(vector<int>& matchsticks) {\n int sum = 0;\n for(int i = 0; i < matchsticks.size(); i++) sum += matchsticks[i];\n if(sum % 4 != 0) return false;\n sort(matchsticks.begin(), matchsticks.end(), greater<int>());\n vector<int> sides(4, 0);\n return solve(matchsticks, 0, sum / 4, sides); \n }\n};", "memory": "77934" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool solve(vector<int> nums, int index, int target, vector<int> sides, int n){\n if(index == n) return true;\n for(int j = 0; j < 4; j++){\n if(sides[j] + nums[index] <= target){\n sides[j] += nums[index];\n if(solve(nums, index + 1, target, sides, n)) return true;\n sides[j] -= nums[index];\n }\t\n if(sides[j] == 0) return false;\t\n }\t\n return false;\t\n }\n\n bool makesquare(vector<int>& matchsticks) {\n int sum = 0;\n for(int i = 0; i < matchsticks.size(); i++) sum += matchsticks[i];\n if(sum % 4 != 0) return false;\n sort(matchsticks.begin(), matchsticks.end(), greater<int>());\n vector<int> sides(4, 0);\n return solve(matchsticks, 0, sum / 4, sides, matchsticks.size()); \n }\n};", "memory": "77934" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool backtrack(vector<int> arr,vector<int> m,int idx,int target){\n if(idx==-1) return true;\n \n for(int i=0;i<4;i++){\n \n if(arr[i]+m[idx]<=target){\n arr[i]+=m[idx];\n if(backtrack(arr,m,idx-1,target)) return true;\n arr[i]-=m[idx];\n }\n if(arr[i]==0) break;\n }\n return false;\n \n }\n bool makesquare(vector<int>& matchsticks) {\n int sum=0;\n sum=accumulate(matchsticks.begin(),matchsticks.end(),sum);\n sort(matchsticks.begin(),matchsticks.end());\n if(matchsticks.size()<4 || sum%4) return false;\n vector<int> arr(4,0);\n return backtrack(arr,matchsticks,matchsticks.size()-1,sum/4);\n\n }\n};", "memory": "79700" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool solve(int bits,int sum, int num,vector<int>& a,int req,unordered_map<int,int>& dp) {\n int n = a.size();\n if(bits==0) {\n //cout<<sum<<\" \"<<num<<\"\\n\";\n return sum==req and num==4;\n }\n if(dp.find(bits)!=dp.end()) {\n return dp[bits];\n }\n bool ans = false;\n for(int i=0;i<n;i++) {\n if(((bits>>i) & 1) == 1 and sum-a[i]>=0) {\n ans |= solve(bits^(1<<i),sum-a[i]>0?sum-a[i]:req,sum-a[i]>0?num:num+1,a,req,dp);\n }\n }\n return dp[bits] = ans;\n }\n\n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(),matchsticks.end(),0);\n if(sum%4!=0) {\n return false;\n }\n cout<<sum<<\"\\n\";\n int n = matchsticks.size();\n int bits = (1<<n)-1;\n cout<<bits<<\"\\n\";\n unordered_map<int,int> dp;\n return solve(bits,sum/4,0,matchsticks,sum/4,dp);\n }\n};", "memory": "81466" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int n = 0;\n int side = 0;\n set<vector<int> > dp[20];\n\n bool isSquarePossible(vector<int> m, int x, int a, int b, int c, int d) {\n if (x==n) {\n if (a==side && b==side && c==side && d==side)\n return true;\n return false;\n }\n\n if (a>side || b>side || c>side || d>side)\n return false;\n \n // choose each side one by one\n vector<int> v = {a+m[x],b,c,d};\n sort(v.begin(),v.end());\n if (dp[x+1].find(v) == dp[x+1].end() && isSquarePossible(m,x+1,a+m[x],b,c,d))\n return true;\n \n v = {a,b+m[x],c,d};\n sort(v.begin(),v.end());\n if (dp[x+1].find(v) == dp[x+1].end() && isSquarePossible(m, x+1,a,b+m[x],c,d))\n return true;\n\n v = {a,b,c+m[x],d};\n sort(v.begin(),v.end());\n if (dp[x+1].find(v) == dp[x+1].end() && isSquarePossible(m, x+1,a,b,c+m[x],d))\n return true;\n\n v = {a,b,c,d+m[x]};\n sort(v.begin(),v.end());\n if (dp[x+1].find(v) == dp[x+1].end() && isSquarePossible(m, x+1,a,b,c,d+m[x]))\n return true;\n \n v = {a,b,c,d};\n sort(v.begin(),v.end());\n dp[x].insert(v);\n return false;\n }\n\n bool makesquare(vector<int>& m) {\n n=m.size();\n for(auto u:m) {\n side += u;\n }\n if (side%4 !=0)\n return false;\n side/=4;\n sort(m.begin(),m.end());\n reverse(m.begin(),m.end());\n\n return isSquarePossible(m,0, 0,0,0,0);\n }\n};", "memory": "83233" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct SetHash {\n std::size_t operator()(const std::set<int>& s) const {\n std::size_t hash = 0;\n for (const int& i : s) {\n hash ^= std::hash<int>{}(i) + 0x9e3779b9 + (hash << 6) + (hash >> 2);\n }\n return hash;\n }\n };\n\n bool recur(int i, vector<int>& matchsticks, int a, int b, int c, int d, map<set<int>, bool>& dp) {\n if (a < 0 || b < 0 || c < 0 || d < 0) return false;\n if (i >= matchsticks.size()) {\n // cout << a << \" \" << b << \" \" << c << \" \" << d << \" : \" << (a == b && b == c && c == d) << endl;\n return (a == 0 && a == b && b == c && c == d);\n }\n int count = 0;\n if (a == 0) count++;\n if (b == 0) count++;\n if (c == 0) count++;\n if (d == 0) count++;\n if (count >= 3) return true;\n \n if (dp.find({a, b, c, d}) != dp.end()) return dp[{a, b, c, d}];\n dp[{a, b, c, d}] = {\n recur(i+1, matchsticks, a-matchsticks[i], b, c, d, dp) ||\n recur(i+1, matchsticks, a, b-matchsticks[i], c, d, dp) || \n recur(i+1, matchsticks, a, b, c-matchsticks[i], d, dp) ||\n recur(i+1, matchsticks, a, b, c, d-matchsticks[i], dp)\n };\n // cout << a << \" \" << b << \" \" << c << \" \" << d << \" = \" << dp[{a, b, c, d}] << endl;\n return dp[{a, b, c, d}];\n }\n bool makesquare(vector<int>& matchsticks) {\n map<set<int>, bool> dp;\n sort(matchsticks.begin(), matchsticks.end(), std::greater<int>());\n int s = std::accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if (s % 4 != 0) return false;\n return recur(0, matchsticks, s/4, s/4, s/4, s/4, dp);\n }\n};", "memory": "90298" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>>dp;\n bool solve(vector<int>&nums, int k, int currSum,int mask,int target)\n { \n if(k<0 || currSum<0)return false;\n if(currSum == 0)\n { \n return solve(nums,k-1,target,mask,target);\n }\n if(mask == (1<<nums.size())-1 && k==0)return true;\n \n if(dp[k][mask] != -1)return dp[k][mask];\n \n bool ans = false;\n for(int i=0;i<nums.size();i++)\n {\n if((mask & (1<<i)) == 0)\n {\n ans = ans || solve(nums,k,currSum-nums[i],mask|(1<<i),target);\n }\n }\n\n return dp[k][mask]=ans;\n \n }\n\n\n bool makesquare(vector<int>& nums) {\n \n int sum=0;\n for(int i =0;i<nums.size();i++)\n {\n sum+= (nums[i]);\n }\n cout<<\"Sum:\"<<sum<<endl;\n if(sum%4)return false;\n\n int target = sum/4;\n cout<<\"Target\"<<target<<endl;\n int n = nums.size();\n dp.resize(5,vector<int>(1<<(n+1), -1));\n return solve(nums,4,target,0,target);\n }\n};", "memory": "92064" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\n vector<vector<int>> dp = vector<vector<int>> (3, vector<int>(1024 * 32, -1));\n\n bool topdown(int xi, int used, vector<int>& matchsticks, int target) {\n if (xi == 3)\n return true;\n\n if (dp[xi][used] != -1)\n return dp[xi][used] == 1;\n\n bool ans = false;\n for (int to_use = 0 ; to_use < (1 << (int)matchsticks.size()) && !ans ; to_use++) {\n if ((to_use & used) != 0)\n continue;\n\n int sum_xi = 0;\n for (int i = 0 ; i < matchsticks.size() ; i++)\n if (((to_use >> i) & 1) == 1)\n sum_xi += matchsticks[i];\n\n if (sum_xi != target)\n continue;\n\n ans |= topdown(xi+1, used ^ to_use, matchsticks, target);\n }\n\n dp[xi][used] = (ans)? 1:0;\n return ans;\n }\n\npublic:\n bool makesquare(vector<int>& matchsticks) {\n int sum = accumulate(matchsticks.begin(), matchsticks.end(), 0);\n if (sum % 4 != 0)\n return false;\n\n return topdown(0, 0, matchsticks, sum / 4);\n }\n};", "memory": "93830" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<vector<int>, bool> memo;\n bool helper(vector<int>& matchsticks, int a, int b, int c, int d, int res, int i) {\n vector<int> temp = {a, b, c, d};\n sort(temp.begin(), temp.end());\n\n if (i == matchsticks.size()) {\n if (a == b && b == c && c == d && d == res) {\n memo[temp] = true;\n } else {\n memo[temp] = false;\n }\n }\n\n if (a > res || b > res || c > res || d > res) {\n memo[temp] = false;\n }\n\n if (memo.find(temp) != memo.end()) {\n return memo[temp];\n }\n\n memo[temp] = helper(matchsticks, a + matchsticks[i], b, c, d, res, i + 1) ||\n helper(matchsticks, a, b + matchsticks[i], c, d, res, i + 1) ||\n helper(matchsticks, a, b, c + matchsticks[i], d, res, i + 1) ||\n helper(matchsticks, a, b, c, d + matchsticks[i], res, i + 1); \n return memo[temp];\n }\n\n bool makesquare(vector<int>& matchsticks) {\n int sum = 0;\n for (int i = 0; i < matchsticks.size(); i++) {\n sum += matchsticks[i];\n }\n\n if (sum % 4 != 0) {\n return false;\n } sum /= 4;\n\n for (int i = 0; i < matchsticks.size(); i++) {\n if (matchsticks[i] > sum) {\n return false;\n }\n }\n\n sort(matchsticks.begin(), matchsticks.end(), [](int a, int b) {\n return a > b;\n });\n \n return helper(matchsticks, 0, 0, 0, 0, sum, 0);\n }\n};", "memory": "95596" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nmap<pair<int,int>,bool> ma;\nint s=0;\nint format(vector<int> mat){\n int k =1;\n for(int i=0;i<mat.size();i++){\n k=k<<1;\n k = k|(mat[i]!=0);\n }\n // cout<<k<<\" \";\n return k;\n}\nbool cal(int k,vector<int> &mat,int c){\n // cout<<\"\\n\";\n if(k<2){\n return true;\n }\n bool ans =false;\n int key = format(mat);\n // cout<<c<<\" \"<<k<<\"**\";\n if(ma.find({key,k})!=ma.end()){\n return ma[{key,k}];\n }\n \n for(int i =0 ;i <mat.size();i++){\n if(mat[i]!=0){\n int y =mat[i];\n if(mat[i]>c){\n continue;\n }\n mat[i]=0;\n if(c-y==0){\n ans = ans||cal(k-1,mat,s);\n }else{\n ans=ans||cal(k,mat,c-y);\n }\n mat[i]=y;\n }\n }\n ma[{key,k}]=ans;\n return ans;\n}\nbool makesquare(vector<int>& matchsticks) {\n int sum=0;\n for(int i=0;i<matchsticks.size();i++){\n sum+=matchsticks[i];\n }\n if(sum%4!=0){\n return false;\n }\n s=sum/4;\n return cal(4,matchsticks,sum/4);\n}\n};", "memory": "97363" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nmap<pair<int,int>,bool> ma;\nint s=0;\nint format(vector<int> mat){\n int k =1;\n for(int i=0;i<mat.size();i++){\n k=k<<1;\n k = k|(mat[i]!=0);\n }\n // cout<<k<<\" \";\n return k;\n}\nbool cal(int k,vector<int> &mat,int c){\n // cout<<\"\\n\";\n if(k<2){\n return true;\n }\n bool ans =false;\n int key = format(mat);\n // cout<<c<<\" \"<<k<<\"**\";\n if(ma.find({key,k})!=ma.end()){\n return ma[{key,k}];\n }\n \n for(int i =0 ;i <mat.size();i++){\n if(mat[i]!=0){\n if(mat[i]>c){\n continue;\n }\n int y =mat[i];\n mat[i]=0;\n if(c-y==0){\n ans = ans||cal(k-1,mat,s);\n if(ans){\n mat[i]=y;\n ma[{key,k}]=ans;\n return ans;\n }\n }else{\n ans=ans||cal(k,mat,c-y);\n if(ans){\n mat[i]=y;\n ma[{key,k}]=ans;\n return ans;\n }\n }\n mat[i]=y;\n }\n }\n ma[{key,k}]=ans;\n return ans;\n}\nbool makesquare(vector<int>& matchsticks) {\n int sum=0;\n for(int i=0;i<matchsticks.size();i++){\n sum+=matchsticks[i];\n }\n if(sum%4!=0){\n return false;\n }\n s=sum/4;\n return cal(4,matchsticks,sum/4);\n}\n};", "memory": "99129" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool helper(vector<int>matchsticks,int sum, int target, int k, vector<int> &visited, int mask, vector<int> &dp) {\n if(k==0) {\n bool ans = 1;\n for(auto x : visited) {\n ans = ans & x;\n }\n return ans;\n }\n if(sum == target) {\n // cout<<sum<<\" \"<<target<<\" \"<<k<<\"\\n\";\n // for(auto x : visited) {\n // cout<<x<<\" \";\n // }\n // cout<<\"\\n\";\n return helper(matchsticks,0,target,k-1,visited,mask,dp);\n }\n if(dp[mask] != -1)\n return dp[mask];\n bool ans = false;\n for(int i =0 ;i<matchsticks.size();i++) {\n if(visited[i])\n continue;\n if(sum + matchsticks[i] <= target) {\n mask = mask | (1<<i);\n visited[i] += 1;\n // cout<<i<<\" \"<<sum + matchsticks[i]<<\"<-\\n\";\n ans = ans || helper(matchsticks,sum + matchsticks[i],target,k,visited,mask,dp);\n visited[i] -= 1;\n mask = mask ^ (1<<i);\n }\n }\n return dp[mask]=ans;\n }\n bool makesquare(vector<int>& matchsticks) {\n int sum=0,i,n=matchsticks.size();\n for(auto x : matchsticks)\n sum+=x;\n if(sum%4 != 0)\n return false;\n vector<int> visited(n,0);\n vector<int> dp((1 << n) -1, -1);\n int ans = helper(matchsticks,0,sum/4,4,visited,0,dp);\n return ans;\n }\n};", "memory": "100895" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(int mask, vector<int>& matchsticks, int index, int sum, int targetSum, vector<int> powerOf2, vector<int> &dp){\n if(sum > targetSum){\n return dp[mask] = false;\n }\n if(index == matchsticks.size()){\n return sum == targetSum;\n }\n if(dp[mask] != -1){\n return dp[mask];\n }\n if(sum == targetSum){\n sum = 0;\n }\n for(int i = 0; i < matchsticks.size(); i++){\n if(!(powerOf2[i] & mask)){\n if(solve(powerOf2[i] | mask, matchsticks, index + 1, sum + matchsticks[i], targetSum, powerOf2, dp)){\n return dp[mask] = true;\n }\n }\n }\n return dp[mask] = false;\n }\n \n bool makesquare(vector<int>& matchsticks) {\n int sum = 0;\n for(int stick : matchsticks){\n sum += stick;\n }\n if(sum % 4 != 0){\n return false;\n }\n vector<int> mp(matchsticks.size());\n vector<int> dp(1<<matchsticks.size(), -1);\n sort(matchsticks.rbegin(), matchsticks.rend());\n int pow = 1;\n for(int i=0;i<matchsticks.size();i++){\n if(matchsticks[i] > sum/4) return false;\n mp[i] = pow;\n pow *= 2;\n }\n return solve(0, matchsticks, 0, 0, sum/4, mp, dp);\n }\n};", "memory": "102661" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool f(vector<int>& a, int pos, long long c1, long long c2, long long c3, long long c4, long long s, unordered_map<int, int>& b) {\n if (pos < 0) {\n return c1 == c2 && c2 == c3 && c3 == c4;\n }\n if (c1 > s | c2 > s | c3 > s | c4 > s) {\n return false;\n }\n int d = (c1 << 25) | (c2 << 16) | (c3 << 8) | c4;\n if (b.count(d)!=0) {\n return b[d];\n }\n bool t1 = f(a, pos - 1, c1 + a[pos], c2, c3, c4, s, b);\n if(t1) return b[d]=true;\n bool t2 = f(a, pos - 1, c1, c2 + a[pos], c3, c4, s, b);\n if(t2) return b[d]=true;\n bool t3 = f(a, pos - 1, c1, c2, c3 + a[pos], c4, s, b);\n if(t3) return b[d]=true;\n bool t4 = f(a, pos - 1, c1, c2, c3, c4 + a[pos], s, b);\n return b[d] = t4;\n }\n\n bool makesquare(vector<int>& matchsticks) {\n sort(matchsticks.begin(),matchsticks.end());\n unordered_map<int,int> b;\n int n = matchsticks.size() - 1;\n long long c1 = 0;\n long long c2 = 0;\n long long c3 = 0;\n long long c4 = 0;\n long long sum = 0;\n for (int i = 0; i < matchsticks.size(); i++) {\n sum += matchsticks[i];\n }\n if (sum % 4 != 0) {\n return false;\n }\n return f(matchsticks, n, c1, c2, c3, c4, sum / 4, b);\n }\n};\n", "memory": "104428" }
473
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p> <p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" /> <pre> <strong>Input:</strong> matchsticks = [1,1,2,2,2] <strong>Output:</strong> true <strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> matchsticks = [3,3,3,3,4] <strong>Output:</strong> false <strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= matchsticks.length &lt;= 15</code></li> <li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool f(vector<int>& a, int pos, long long c1, long long c2, long long c3, long long c4, long long s, unordered_map<int, int>& b) {\n if (pos < 0) {\n return c1 == c2 && c2 == c3 && c3 == c4;\n }\n if (c1 > s | c2 > s | c3 > s | c4 > s) {\n return false;\n }\n int d = (c1 << 25) | (c2 << 16) | (c3 << 8) | c4;\n if (b.count(d)!=0) {\n return b[d];\n }\n bool t1 = f(a, pos - 1, c1 + a[pos], c2, c3, c4, s, b);\n if(t1) return b[d]=true;\n bool t2 = f(a, pos - 1, c1, c2 + a[pos], c3, c4, s, b);\n if(t2) return b[d]=true;\n bool t3 = f(a, pos - 1, c1, c2, c3 + a[pos], c4, s, b);\n if(t3) return b[d]=true;\n bool t4 = f(a, pos - 1, c1, c2, c3, c4 + a[pos], s, b);\n return b[d] = t4;\n }\n\n bool makesquare(vector<int>& matchsticks) {\n sort(matchsticks.begin(),matchsticks.end());\n unordered_map<int,int> b;\n int n = matchsticks.size() - 1;\n long long c1 = 0;\n long long c2 = 0;\n long long c3 = 0;\n long long c4 = 0;\n long long sum = 0;\n for (int i = 0; i < matchsticks.size(); i++) {\n sum += matchsticks[i];\n }\n if (sum % 4 != 0) {\n return false;\n }\n return f(matchsticks, n, c1, c2, c3, c4, sum / 4, b);\n }\n};\n", "memory": "106194" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\n int dp[101][101];\n int cnt[601][2];\n\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int sz = strs.size();\n \n for (int i = 0; i < sz; i++)\n for (char c : strs[i])\n cnt[i][c - '0']++;\n\n dp[cnt[0][1]][cnt[0][0]] = 1;\n\n for (int i = 1; i < sz; i++)\n for (int j = n; j >= cnt[i][1]; j--)\n for (int k = m; k >= cnt[i][0]; k--)\n dp[j][k] = max(dp[j][k], 1 + dp[j - cnt[i][1]][k - cnt[i][0]]);\n\n int ans = 0;\n for (int j = 0; j <= n; j++)\n for (int k = 0; k <= m; k++)\n ans = max(ans, dp[j][k]);\n return ans;\n }\n};", "memory": "12373" }