id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nbool flag = false;\nvoid recursive(TreeNode* check, ListNode* node) {\n if (node != nullptr) {\n if (check->left != nullptr && check->left->val == node->val) {\n recursive(check->left, node->next);\n }\n if (check->right != nullptr && check->right->val == node->val) {\n recursive(check->right, node->next);\n }\n } \n else {\n flag = true; return;\n }\n}\n bool isSubPath(ListNode* head, TreeNode* root) {\n queue<TreeNode*> paths, checks;\n paths.push(root);\n while (paths.size() != 0) {\n for (int i = paths.size() -1; i >= 0; paths.pop(), i--) {\n if (paths.front()->val == head->val) checks.push(paths.front());\n if (paths.front()->left != nullptr) paths.push(paths.front()->left);\n if (paths.front()->right != nullptr) paths.push(paths.front()->right);\n }\n }\n while (!checks.empty() && !flag) {\n recursive(checks.front(), head->next);\n checks.pop();\n }\n return flag;\n }\n};", "memory": "31700" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n function<bool(TreeNode*, ListNode*)> validPath = [&](TreeNode* treeNode, ListNode* listNode) -> bool {\n if (treeNode->val != listNode->val)\n return false;\n if (!listNode->next)\n return true;\n if (treeNode->left && validPath(treeNode->left, listNode->next))\n return true;\n if (treeNode->right && validPath(treeNode->right, listNode->next))\n return true;\n return false;\n };\n function<bool(TreeNode*)> traverse = [&](TreeNode* treeNode) -> bool {\n if (!treeNode)\n return false;\n\n if (validPath(treeNode, head))\n return true;\n\n return traverse(treeNode->left) || traverse(treeNode->right);\n };\n\n return traverse(root);\n }\n};", "memory": "31800" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n bool recurr(ListNode* head, TreeNode*& root){\n if(!head) cout<<\"NULL\\n\";\n if(head==NULL) return true;\n if(root==NULL) return false;\n\n if(root->val != head->val)\n return false;\n\n head = head->next;\n return recurr(head, root->left) || recurr(head, root->right);\n }\n\n bool isSubPath(ListNode* head, TreeNode* root) {\n if(root==NULL) return false;\n bool ans = recurr(head, root);\n\n if(ans) return ans;\n\n return isSubPath(head, root->left) || isSubPath(head, root->right);\n }\n};", "memory": "31900" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n function<bool(ListNode*, TreeNode*)> compare = [&](ListNode* list_node, TreeNode* tree_node) -> bool {\n if (!list_node) return true;\n if (!tree_node) return false;\n if (list_node->val != tree_node->val) return false;\n return compare(list_node->next, tree_node->left) || compare(list_node->next, tree_node->right);\n };\n function<bool(TreeNode*)> dfs = [&](TreeNode* tree_node) -> bool {\n if (!tree_node) return false;\n return compare(head, tree_node) || dfs(tree_node->left) || dfs(tree_node->right);\n };\n return dfs(root);\n }\n};", "memory": "31900" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) \n {\n vector<int> linked_list;\n while(head != NULL)\n {\n linked_list.push_back(head->val);\n head = head->next;\n }\n\n vector<int> valid_indicies;\n \n stack<vector<int>> valid_indicies_stack;\n stack<TreeNode*> treenode_stack;\n stack<int> visited_stack;\n TreeNode* node = root;\n int visited = 0;\n bool back_tracked = false;\n\n while(node)\n {\n if(!back_tracked)\n {\n for(int i = 0; i < valid_indicies.size(); i++)\n {\n valid_indicies[i] = valid_indicies[i] != -1 && node->val == linked_list[valid_indicies[i] + 1] ? valid_indicies[i] + 1 : -1;\n if(valid_indicies[i] + 1 == linked_list.size()) return true;\n }\n if(node->val == linked_list[0]) valid_indicies.push_back(0);\n if(node->val == linked_list[0] && linked_list.size() == 1) return true;\n }\n\n if(node->left && !back_tracked)\n {\n treenode_stack.push(node);\n visited_stack.push(-1);\n valid_indicies_stack.push(vector<int>(valid_indicies));\n\n visited = 0;\n node = node->left;\n back_tracked = false;\n }\n else if(node->right)\n {\n visited = 0;\n node = node->right;\n back_tracked = false;\n }\n else if(!treenode_stack.empty())\n {\n visited = visited_stack.top(); \n node = treenode_stack.top();\n valid_indicies = valid_indicies_stack.top();\n visited_stack.pop(); \n treenode_stack.pop();\n valid_indicies_stack.pop();\n back_tracked = true;\n }\n else\n {\n node = NULL;\n }\n }\n return false;\n }\n};", "memory": "32000" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) \n {\n vector<int> linked_list;\n while(head != NULL)\n {\n linked_list.push_back(head->val);\n head = head->next;\n }\n\n vector<int> valid_indicies;\n \n stack<vector<int>> valid_indicies_stack;\n stack<TreeNode*> treenode_stack;\n TreeNode* node = root;\n bool back_tracked = false;\n\n while(node)\n {\n if(!back_tracked)\n {\n for(int i = 0; i < valid_indicies.size(); i++)\n {\n valid_indicies[i] = valid_indicies[i] != -1 && node->val == linked_list[valid_indicies[i] + 1] ? valid_indicies[i] + 1 : -1;\n if(valid_indicies[i] + 1 == linked_list.size()) return true;\n }\n if(node->val == linked_list[0]) valid_indicies.push_back(0);\n if(node->val == linked_list[0] && linked_list.size() == 1) return true;\n }\n\n if(node->left && !back_tracked)\n {\n treenode_stack.push(node);\n valid_indicies_stack.push(vector<int>(valid_indicies));\n\n node = node->left;\n back_tracked = false;\n }\n else if(node->right)\n {\n node = node->right;\n back_tracked = false;\n }\n else if(!treenode_stack.empty())\n {\n node = treenode_stack.top();\n valid_indicies = valid_indicies_stack.top();\n treenode_stack.pop();\n valid_indicies_stack.pop();\n back_tracked = true;\n }\n else\n {\n node = NULL;\n }\n }\n return false;\n }\n};", "memory": "32000" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n if(!root) return false;\n\n function<bool(ListNode*, TreeNode*)> dfs = [&](ListNode* head, TreeNode* root) -> bool {\n if(!head) return true;\n if(!root || head->val != root->val) return false;\n\n return dfs(head->next, root->left) || dfs(head->next, root->right);\n };\n\n return dfs(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);\n }\n};", "memory": "32100" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void search(ListNode* head,ListNode* curr,TreeNode* root,bool& res){\n if(res)\n return ;\n \n if(curr==nullptr){\n res=true;\n return ;\n }\n\n if(root==nullptr){\n return ;\n }\n \n if(root->val == curr->val){\n curr = curr->next;\n search(head,curr,root->left,res);\n search(head,curr,root->right,res);\n } \n }\n \n bool isSubPath(ListNode* head, TreeNode* root) {\n if(root==nullptr)\n return false;\n\n bool res = false;\n search(head,head,root,res);\n return res or isSubPath(head,root->right) or isSubPath(head,root->left);\n }\n};", "memory": "32200" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n bool solve1(ListNode* head, TreeNode* root) {\n // If we reached the end of the linked list, return true\n if (!head) return true;\n\n // If the current tree node is null, return false (list cannot continue)\n if (!root) return false;\n\n // If the current node values match, continue checking the left and right subtrees\n if (root->val == head->val) {\n return solve1(head->next, root->left) || solve1(head->next, root->right);\n }\n\n // If the current node does not match, return false\n return false;\n }\n\n void solve(TreeNode* root, int headVal, vector<TreeNode*>& v) {\n if (!root) return;\n\n // If the current tree node matches the head of the linked list, store the node\n if (root->val == headVal) {\n v.push_back(root);\n }\n\n // Continue searching in the left and right subtrees\n solve(root->left, headVal, v);\n solve(root->right, headVal, v);\n }\n\n bool isSubPath(ListNode* head, TreeNode* root) {\n // Edge case: if the list or the tree is empty, return false\n if (!head || !root) return false;\n\n // Store all the potential starting points in the tree where the value matches the head of the list\n vector<TreeNode*> v;\n solve(root, head->val, v);\n\n // For each matching node, check if the linked list forms a path starting from that node\n for (auto node : v) {\n if (solve1(head, node)) {\n return true;\n }\n }\n\n return false; // No valid subpath found\n }\n};\n", "memory": "32299" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n bool dfs(ListNode* head,TreeNode* root){\n if(head == NULL) return 1;\n if(root == NULL) return 0;\n if(root->val != head->val) return 0;\n\n return dfs(head->next,root->left) || dfs(head->next,root->right);\n }\n\n vector<TreeNode*> temp;\n void dfs1(ListNode* head, TreeNode* root) {\n if(root == NULL) return;\n\n if(head->val == root->val) temp.push_back(root);\n dfs1(head,root->right);\n dfs1(head,root->left);\n }\n\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n dfs1(head,root);\n for(auto i : temp){\n if(dfs(head,i)) return 1;\n }\n return 0;\n }\n};", "memory": "32400" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n void trav(TreeNode*root,vector<TreeNode*>&nodes,int val){\n if(root==nullptr)\n return;\n if(root->val==val)\n nodes.push_back(root);\n trav(root->left,nodes,val);\n trav(root->right,nodes,val);\n return; \n } \nbool solve(TreeNode* root,ListNode* head){\n if(head==nullptr)\n return true;\n if(root==nullptr)\n return false;\n if(head->val==root->val){\n if(solve(root->left,head->next))\n return true;\n if(solve(root->right,head->next))\n return true; \n } \n return false; \n}\n\n\n\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n TreeNode* x = root;\n ListNode* temp = head;\n int val = head->val;\n vector<TreeNode*> nodes;\n trav(root,nodes,val);\n for(int i=0; i<nodes.size(); i++){\n if(solve(nodes[i],temp)==true)\n return true;\n }\n return false;\n }\n};", "memory": "32400" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void find(vector<TreeNode*>&v,TreeNode*root,int val)\n {\n if(root==NULL)\n return ;\n if(root->val==val)\n {\n // cout<<root->val<<endl; \n v.push_back(root); \n }\n find(v,root->left,val);\n find(v,root->right,val);\n \n }\n bool solve( TreeNode* root,ListNode* head)\n {\n \n if(head==NULL)\n return true;\n if(root==NULL)\n return false;\n\n if(head->val!=root->val)\n return false;\n \n return solve(root->left,head->next) || solve(root->right,head->next); \n \n \n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<TreeNode*>v;\n find(v,root,head->val);\n if(v.size()==0)\n return false;\n // cout<<v.size()<<endl; \n \n for(int i=0;i<v.size();i++)\n {\n if(solve(v[i],head))\n return true;\n \n }\n return false;\n }\n};", "memory": "32500" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(ListNode* head,TreeNode* node,ListNode* curr,bool &ans,set<TreeNode*> &s)\n {\n if(ans)\n return;\n \n if(curr==nullptr)\n {\n ans=1;\n return;\n }\n if(node==nullptr)\n return;\n if(curr->val!=node->val)\n {\n curr=head;\n }\n if(curr->val==node->val)\n curr=curr->next;\n \n \n solve(head,node->right,curr,ans,s);\n solve(head,node->left,curr,ans,s);\n if(head->val==node->val)\n {\n if(!s.count(node))\n {\n s.insert(node);\n solve(head,node->right,head->next,ans,s);\n solve(head,node->left,head->next,ans,s);\n }\n }\n \n\n\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n bool ans=0;\n ListNode* curr=head;\n set<TreeNode*> s;\n solve(head,root,curr,ans,s);\n return ans;\n }\n};", "memory": "32500" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void findTarget(TreeNode* root, ListNode* head, vector<TreeNode*>& start)\n {\n if(root==NULL) return;\n if(root->val==head->val) start.push_back(root);\n findTarget(root->left,head,start);\n findTarget(root->right,head,start);\n return;\n }\n bool findEnd(TreeNode* root, ListNode* head)\n {\n if(head==NULL) return true;\n if(root==NULL) return false;\n\n if(root->val!=head->val) return false;\n\n return findEnd(root->left, head->next) || findEnd(root->right,head->next); \n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<TreeNode*>start;\n findTarget(root,head,start);\n for(auto i : start){\n //cout<<i->val<<endl;\n if(findEnd(i,head)) return true;\n }\n return false;\n }\n};", "memory": "32600" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<TreeNode*> nums; \n inordertraversal(root, nums, head->val);\n for(auto&node:nums)\n {\n if(check(head,node))\n {\n return true;\n }\n }\n return false;\n }\n\n bool check(ListNode*head,TreeNode*root)\n {\n if(head==NULL)\n {\n return true;\n }\n if(root==NULL)\n {\n return false;\n }\n\n if(head->val!=root->val)\n {\n return false;\n }\n return check(head->next,root->left)||check(head->next,root->right);\n }\n\n\n void inordertraversal(TreeNode* root, vector<TreeNode*>& nums, int target) {\n if (root == NULL) {\n return;\n }\n inordertraversal(root->left, nums, target);\n if (root->val == target) {\n nums.push_back(root);\n }\n inordertraversal(root->right, nums, target);\n }\n};\n", "memory": "32600" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<TreeNode*> nums; \n inordertraversal(root, nums, head->val);\n for(auto&node:nums)\n {\n if(check(head,node))\n {\n return true;\n }\n }\n return false;\n }\n\n bool check(ListNode*head,TreeNode*root)\n {\n if(head==NULL)\n {\n return true;\n }\n if(root==NULL)\n {\n return false;\n }\n\n if(head->val!=root->val)\n {\n return false;\n }\n return check(head->next,root->left)||check(head->next,root->right);\n }\n\n\n void inordertraversal(TreeNode* root, vector<TreeNode*>& nums, int target) {\n if (root == NULL) {\n return;\n }\n inordertraversal(root->left, nums, target);\n if (root->val == target) {\n nums.push_back(root);\n }\n inordertraversal(root->right, nums, target);\n }\n};\n", "memory": "32700" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n set<TreeNode*> st;\n void rec(ListNode* head, TreeNode* root){\n if(!root)return;\n\n if(head->val==root->val) st.insert(root);\n rec(head,root->left);\n rec(head,root->right);\n\n }\n\n bool helper(ListNode *head,TreeNode* root){\n if(!head) return true;\n if(!root || root->val!=head->val) return false;\n\n return helper(head->next,root->left) || helper(head->next,root->right);\n\n }\n \n bool isSubPath(ListNode* head, TreeNode* root) {\n if(head==nullptr) return true;\n if(!root)return false;\n\n rec(head,root);\n\n for(auto it:st){\n if(helper(head,it)) return true;\n }\n\n return false;\n \n }\n};", "memory": "32800" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<int> path;\n while (head != NULL) {\n path.push_back(head->val);\n head = head->next;\n }\n int n = path.size();\n\n stack<pair<TreeNode*, int>> s;\n stack<TreeNode*> next_nodes;\n next_nodes.push(root);\n while (!next_nodes.empty()) {\n TreeNode* next = next_nodes.top();\n next_nodes.pop();\n if (next->val == path[0]) {\n s.push({next, 0});\n }\n if (next->left != NULL) {\n next_nodes.push(next->left);\n }\n if (next->right != NULL) {\n next_nodes.push(next->right);\n }\n }\n \n while (!s.empty()) {\n TreeNode* next = s.top().first;\n int i = s.top().second;\n s.pop();\n if (i + 1 == n) {\n return true;\n }\n if (next->left != NULL && next->left->val == path[i+1]) {\n s.push({next->left, i+1});\n } \n if (next->right != NULL && next->right->val == path[i+1]) {\n s.push({next->right, i+1});\n }\n }\n\n return false;\n }\n};", "memory": "32800" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "class Solution\n{\nprivate:\n ListNode* head;\n bool found = false;\n\npublic:\n bool isSubPath(ListNode* const head, \n TreeNode* const root)\n {\n this->head = head;\n\n ListNode* lnodes_next[101] = { };\n int l_next = 0;\n\n lnodes_next[l_next++] = head;\n\n this->isSubPathDFS(root, lnodes_next, l_next);\n\n return found;\n }\n\n void isSubPathDFS(TreeNode* const tnode, \n ListNode** lnodes, const int l)\n {\n ListNode* lnodes_next[101] = { };\n int l_next = 0;\n\n for (int i = 0; i < l; ++i)\n {\n ListNode* const lnode = lnodes[i];\n\n if (lnode == nullptr)\n {\n found = true;\n return;\n }\n\n if (tnode == nullptr)\n continue;\n\n if (lnode->val == tnode->val)\n lnodes_next[l_next++] = lnode->next;\n }\n\n if (tnode == nullptr)\n return;\n\n lnodes_next[l_next++] = head;\n\n this->isSubPathDFS(tnode->left, lnodes_next, l_next);\n\n if (found)\n return;\n\n this->isSubPathDFS(tnode->right, lnodes_next, l_next);\n\n return;\n }\n};\n", "memory": "32900" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "class Solution\n{\nprivate:\n ListNode* head;\n bool found = false;\n\npublic:\n bool isSubPath(ListNode* const head, \n TreeNode* const root)\n {\n this->head = head;\n\n ListNode* lnodes_next[101] = { };\n int l_next = 0;\n\n lnodes_next[l_next++] = head;\n\n this->isSubPathDFS(root, lnodes_next, l_next);\n\n return found;\n }\n\n void isSubPathDFS(TreeNode* const tnode, \n ListNode** lnodes, const int l)\n {\n ListNode* lnodes_next[101] = { };\n int l_next = 0;\n\n for (int i = 0; i < l; ++i)\n {\n ListNode* const lnode = lnodes[i];\n\n if (lnode == nullptr)\n {\n found = true;\n return;\n }\n\n if (tnode == nullptr)\n continue;\n\n if (lnode->val == tnode->val)\n lnodes_next[l_next++] = lnode->next;\n }\n\n if (tnode == nullptr)\n return;\n\n lnodes_next[l_next++] = head;\n\n this->isSubPathDFS(tnode->left, lnodes_next, l_next);\n\n if (found)\n return;\n\n this->isSubPathDFS(tnode->right, lnodes_next, l_next);\n\n return;\n }\n};\n", "memory": "32900" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nbool traverse(ListNode *head, ListNode *target, TreeNode *node, unordered_set<TreeNode*> &table) {\n if (target == nullptr) return true;\n if (node == nullptr) return false;\n\n if (target->val != node->val) {\n if (traverse(head, head, node->left, table)) return true;\n if (traverse(head, head, node->right, table)) return true;\n } else {\n if (traverse(head, target->next, node->left, table)) return true;\n if (traverse(head, target->next, node->right, table)) return true;\n }\n\n if (table.find(node) == table.end() && head != target && head->val == node->val) {\n table.insert(node);\n if (traverse(head, head->next, node->left, table)) return true;\n if (traverse(head, head->next, node->right, table)) return true;\n }\n return false;\n}\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n unordered_set<TreeNode*> table;\n return traverse(head, head, root, table);\n }\n};", "memory": "33000" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n if (!root)\n return false;\n bool result = dfs(head, head, root);\n result |= isSubPath(head, root->left);\n result |= isSubPath(head, root->right);\n cout << \"FINISH result = \" << result << endl;\n return result;\n }\n\n bool dfs(ListNode *headList, ListNode *curList, TreeNode *curTree, string indent = \"\") {\n // cout << indent << \"curList = \" << curList->val << \" curTree = \" << curTree->val << endl; \n if (curList->val == curTree->val && curList && !curList->next) {\n cout << indent << \" found \" << endl; \n return true;\n }\n\n ListNode *nextList = curList->next;\n if (curList->val != curTree->val)\n nextList = headList;\n // // if it's possible to start a new list from this node\n // if (headList != curList && curTree->val == headList->val) {\n // cout << indent + \"can start anew\" << endl;\n // if ((curTree->left && dfs(headList, headList, curTree->left, indent + \" \")) \n // || (curTree->right && dfs(headList, headList, curTree->right, indent + \" \")))\n // return true;\n // }\n\n if (curTree->left) {\n // if (curTree->left->val == curList->next->val) {\n // nextList = curList->next;\n if (dfs(headList, nextList, curTree->left))\n return true;\n }\n\n // nextList = nullptr;\n if (curTree->right) {\n // if (curTree->right->val == curList->next->val) \n // nextList = curList->next;\n if (dfs(headList, nextList, curTree->right))\n return true;\n }\n return false;\n }\n};", "memory": "33100" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution\n{\npublic:\n\tstruct Context\n\t{\n\t\tContext() : startDepth(0), failDepth(0) {}\n\t\tContext(int _startDepth) : startDepth(_startDepth), failDepth(0) {}\n\n\t\tconst int startDepth;\n\t\tint failDepth;\n\t};\n\n\tbool isSubPath(ListNode* head, TreeNode* root)\n\t{\n\t\tvector<int> numsToSearch;\n\t\tnumsToSearch.reserve(100);\n\t\tauto node = head;\n\t\twhile (node != nullptr)\n\t\t{\n\t\t\tnumsToSearch.push_back(node->val);\n\t\t\tnode = node->next;\n\t\t}\n\n\t\tvector<Context> contexts;\n\t\tcontexts.reserve(1000);\n\t\tTreeNode dummy(0, nullptr, root);\n\t\treturn dfs(&dummy, 0, numsToSearch, contexts);\n\t}\n\n\tbool dfs(TreeNode* node, int depth, const vector<int>& numsToSearch, vector<Context>& contexts)\n\t{\n\t\tbool failedAny = false;\n\t\tfor (auto& context : contexts)\n\t\t{\n\t\t\tif (context.failDepth != 0)\n\t\t\t\tcontinue;\n\n\t\t\tint numIndex = depth - context.startDepth;\n\t\t\tif (node->val != numsToSearch[numIndex])\n\t\t\t{\n\t\t\t\tfailedAny = true;\n\t\t\t\tcontext.failDepth = depth;\n\t\t\t}\n\t\t\telse if (numIndex + 1 == numsToSearch.size())\n\t\t\t\treturn true;\n\t\t}\n\n\t\tbool contextAdded = node->val == numsToSearch.front();\n\t\tif (contextAdded)\n\t\t{\n\t\t\tif (numsToSearch.size() == 1)\n\t\t\t\treturn true;\n\n\t\t\tcontexts.emplace_back(depth);\n\t\t}\n\n\t\tif (node->left != nullptr &&\n\t\t\tdfs(node->left, depth + 1, numsToSearch, contexts))\n\t\t{\n\t\t\treturn true;\n\t\t}\n\n\t\tif (node->right != nullptr &&\n\t\t\tdfs(node->right, depth + 1, numsToSearch, contexts))\n\t\t{\n\t\t\treturn true;\n\t\t}\n\n\t\tif (contextAdded)\n\t\t\tcontexts.pop_back();\n\n\t\tif (failedAny)\n\t\t{\n\t\t\tfor (auto& context : contexts)\n\t\t\t{\n\t\t\t\tif (context.failDepth == depth)\n\t\t\t\t\tcontext.failDepth = 0;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n};", "memory": "33100" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid preorder(TreeNode* root,vector<TreeNode*>&V)\n{\n if(!root) return;\n V.push_back(root);\n preorder(root->left,V);\n preorder(root->right,V);\n}\nbool check(ListNode* head,TreeNode* root)\n{\n if(!head || !root){\n if(!head) return true;\n return false;\n }\n if(root->val!=head->val) return false;\n if(check(head->next,root->left)) return true;\n if(check(head->next,root->right)) return true;\n return false;\n}\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<TreeNode*>V;\n preorder(root,V);\n for(auto p:V)\n {\n if(check(head,p)) return true;\n }\n return false;\n }\n};", "memory": "33200" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n\n function<bool(ListNode*,TreeNode*)>ispath=[&](ListNode*head,TreeNode*root){\n if(!head)\n return true;\n\n if(!root)\n return false;\n \n if(root->val==head->val)\n return ispath(head->next,root->left)||ispath\n (head->next,root->right);\n\n return false; \n };\n\n function<bool(ListNode*,TreeNode*)>isSubPath=[&](ListNode* head, TreeNode* root) {\n if(!root)\n return false;\n \n bool ans=false;\n\n if(root->val==head->val){\n ans=ispath(head->next,root->left)||ispath\n (head->next,root->right);\n }\n\n if(!ans){\n return isSubPath(head,root->left)||isSubPath \n (head,root->right);\n }\n\n return ans; \n };\n\n return isSubPath(head,root);\n }\n};", "memory": "33200" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool check(ListNode* head,TreeNode* root){\n if(head == NULL) return true;\n if(root == NULL) return false;\n\n if(root->val == head->val){\n head = head->next;\n bool chk1 = check(head,root->left);\n if(chk1) return true;\n bool chk2 = check(head,root->right);\n if(chk2) return true;\n }\n return false;\n }\n void inorder(TreeNode* root,ListNode* head,vector<TreeNode*>& vec){\n if(root == NULL) return;\n inorder(root->left,head,vec);\n vec.push_back(root);\n inorder(root->right,head,vec);\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<TreeNode*>vec;\n inorder(root,head,vec);\n\n for(int i = 0;i < vec.size(); i++){\n if(check(head,vec[i])) return true;\n }\n return false;\n }\n};", "memory": "33300" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n\nprivate:\n unordered_set<TreeNode*> s;\n void dfs_1(ListNode* head, TreeNode* root) {\n if (root == nullptr) return;\n\n if (head->val == root->val) {\n s.insert(root);\n } \n\n dfs_1(head, root->left);\n dfs_1(head, root->right);\n\n\n }\n bool dfs_2(ListNode* curr, TreeNode* root) {\n if (curr == nullptr) {\n return true;\n } else if (root == nullptr) {\n return false;\n }\n\n if (curr->val == root->val) {\n return dfs_2(curr->next, root->left) || dfs_2(curr->next, root->right);\n }\n\n return false;\n } \npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n dfs_1(head, root);\n\n for (TreeNode* node : s) {\n if (dfs_2(head, node)) {\n return true;\n }\n }\n return false;\n }\n};", "memory": "33300" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool solve(ListNode* head,TreeNode* root){\n if(head==nullptr) return true;\n if(root==nullptr) return false;\n if(head->val == root->val){\n return solve(head->next , root->left) || solve(head->next , root->right);\n }\n return false;\n }\n void dfs(TreeNode* root,vector<TreeNode*> &nodes){\n nodes.push_back(root);\n if(root->left!=nullptr) dfs(root->left,nodes);\n if(root->right!=nullptr) dfs(root->right,nodes); \n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n vector<TreeNode*> nodes;\n dfs(root,nodes);\n\n for(TreeNode* node : nodes){\n if(solve(head,node)){\n return true;\n }\n }\n return false;\n }\n};", "memory": "33400" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int integerBreak(int n) {\n if (n <= 3) return n - 1;\n \n int product = 1;\n while (n > 4) {\n product *= 3;\n n -= 3;\n }\n // The remaining n is 2, 3, or 4, so we multiply it\n product *= n;\n \n return product;\n }\n};\n", "memory": "7100" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int integerBreak(int n) {\n // If an optimal product contains a factor f >= 4, then we can replace it\n // with 2 and f - 2 without losing optimality. As 2(f - 2) = 2f - 4 >= f,\n // we never need a factor >= 4, meaning we only need factors 1, 2, and 3\n // (and 1 is wasteful).\n // Also, 3 * 3 is better than 2 * 2 * 2, so we never use 2 more than twice.\n if (n == 2) // 1 * 1\n return 1;\n if (n == 3) // 1 * 2\n return 2;\n\n int ans = 1;\n\n while (n > 4) {\n n -= 3;\n ans *= 3;\n }\n ans *= n;\n\n return ans;\n }\n};", "memory": "7200" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
0
{ "code": "class Solution {\n public:\n int integerBreak(int n) {\n // If an optimal product contains a factor f >= 4, then we can replace it\n // with 2 and f - 2 without losing optimality. As 2(f - 2) = 2f - 4 >= f,\n // we never need a factor >= 4, meaning we only need factors 1, 2, and 3\n // (and 1 is wasteful).\n // Also, 3 * 3 is better than 2 * 2 * 2, so we never use 2 more than twice.\n if (n == 2) // 1 * 1\n return 1;\n if (n == 3) // 1 * 2\n return 2;\n\n int ans = 1;\n\n while (n > 4) {\n n -= 3;\n ans *= 3;\n }\n ans *= n;\n\n return ans;\n }\n};", "memory": "7200" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int integerBreak(int n) {\n if (n == 2) return 1;\n if (n == 3) return 2;\n\n int product = 1;\n \n // Chia n thành các số 3 càng nhiều càng tốt\n while (n > 4) {\n product *= 3;\n n -= 3;\n }\n \n // Nhân phần còn lại (n sẽ là 2, 3 hoặc 4)\n product *= n;\n\n return product;\n }\n};", "memory": "7300" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int integerBreak(int n) {\n vector<int> dp(n+1,0);\n if(n == 2) return 1;\n if(n == 3) return 2;\n dp[1] = 1;\n dp[2] = 2; // not correct actually\n dp[3] = 3; // not correct actually\n\n for(int i = 4; i <= n; i++){\n for(int j = 1; j < i ; j++){\n dp[i] = max(dp[i], dp[i-j] * dp[j]);\n }\n }\n return dp[n];\n }\n};", "memory": "7800" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int integerBreak(int n) {\n vector<int> dp(n+1);\n dp[1] = 1;\n for(int i = 2; i <= n; i++){\n int max_brk = 0;\n for(int j = 1; j < i; j++) {\n max_brk = max(max_brk, max(dp[i-j]*j, (i-j)*j));\n }\n dp[i] = max_brk;\n }\n\n return dp[n];\n }\n};", "memory": "7800" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void searchAndFind(vector<int>&dp,int right){\n int l = 1;\n int target = right;\n int r = right -1;\n while(l <= r){\n int total = l + r;\n if(total == target){\n dp[target] = max({dp[l]*r, dp[r]*l, dp[target], dp[l]*dp[r], l*r});\n l++;\n r--;\n }\n else if(total < target){\n l++;\n }else{\n r--;\n }\n }\n return;\n }\n int integerBreak(int n) {\n vector<int> dp(n+1, 0);\n dp[2] = 1;\n dp[1] = 1;\n for(int i = 2; i<= n; i++){\n searchAndFind(dp, i);\n }\n for(int i = 0; i<= n; i++){\n cout<< dp[i]<< \" \";\n }\n return dp[n];\n }\n\n};", "memory": "7900" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int func(int n, int cnt, vector<vector<int>>& dp){\n if(n <= 0){\n if(cnt < 2) return 0;\n return 1;\n }\n if(dp[n][cnt] != -1) return dp[n][cnt];\n int ans = 0;\n for(int i = 1; i <= n; i++){\n ans = max(ans, i * func(n - i, cnt + 1, dp));\n }\n return dp[n][cnt] = ans;\n }\n int integerBreak(int n) {\n vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1));\n return func(n, 0, dp);\n }\n};", "memory": "8000" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n//--------------------------------->ओम नमो भगवते वासुदेवाय<---------------------------------------------------------------\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⡋⠉⢋⣝⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣆⣘⣿⢿⡟⡷⢶⣤⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢉⣡⡾⠃⣷⠀⡈⠙⢿⣶⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⡿⠋⢠⣾⠏⣸⣿⣦⡀⠙⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣾⡟⠋⠀⠀⣿⡏⠀⣿⠁⠹⣿⡄⠘⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⣿⠋⠀⠀⠀⠸⣿⡇⠀⣿⠀⠀⢹⣿⠀⢻⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡟⠀⠀⣀⣤⣶⣿⡿⢄⠘⢇⠀⢸⣿⡇⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\t⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡇⠀⣼⡿⠋⠁⠀⠀⠀⢀⣀⣀⣸⣿⢠⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⣀⣠⣤⣤⣶⣶⣶⣶⣦⣤⣈⠗⢰⡟⠀⠀⣠⣴⠾⠟⠛⠛⠛⠻⣷⡛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠘⠿⢿⣿⣍⠀⠀⠀⠀⠀⠉⠛⢿⣬⠇⢠⡾⠋⠀⠀⠀⠀⠀⠀⠲⣦⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠘⢿⣷⠀⠀⠀⠀⠀⠀⠀⢻⣧⠋⠀⠀⠀⠀⡀⠀⠀⠀⠀⠘⣧⠀⠙⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠈⢿⣧⠀⠀⢠⡀⠀⠀⢨⡿⠀⠀⠀⠀⠀⠉⠒⠒⠶⣦⡀⠙⢷⣶⣾⢿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠳⠤⠴⠟⠁⠀⢀⠀⠀⠀⠀⠀⠻⠟⠚⠃⠀⠀⠀⠸⠮⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠈⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠹⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢶⣤⣤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⣼⣧⡀⠀⠀⠀⠀\n// ⠀⣀⣀⣀⣀⠀⠀⠘⢻⣦⣀⠀⠀⢠⣄⠀⢠⣴⣦⣄⠀⠉⠙⠻⢿⣦⡀⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⣾⠋⠘⣷⠀⠀⠀⠀\n// ⠀⠹⣿⡿⠿⢿⣦⡀⠀⠈⡙⠛⠒⠚⣟⠀⣸⠛⠛⢻⡀⠀⠀⠀⠀⢹⣿⡀⠀⠀⠀⠀⣼⡟⠀⢀⣀⣘⣓⣚⣧⣶⣶⣶⠖\n// ⠀⠀⢸⣷⠀⠀⢿⣷⣠⣾⢿⣆⠀⠀⣿⠀⢻⡄⠀⠸⣧⣄⡀⠀⣀⣼⡿⠁⠀⠀⠀⣰⣿⢣⡾⠛⠉⠉⠛⣿⠋⢀⣿⠏⠀\n// ⠀⠀⢸⣿⠀⠀⠈⢿⣿⠁⠈⣿⠀⢸⡏⣀⡼⣿⣄⠀⠈⠛⠿⠿⠟⠋⠁⠀⠀⢀⣼⡿⠃⠏⣀⣀⠀⠀⠀⠀⠀⣼⡿⠀⠀\n// ⠀⠀⠀⣿⡀⠀⠀⠀⠁⠀⣸⡏⠴⠟⠛⠉⠀⠈⠻⢿⣦⣤⣄⣀⣀⣀⣠⣤⣶⣿⠋⠀⠀⢿⠏⠙⠻⢶⣦⣴⡾⠟⠁⠀⠀\n// ⠀⠀⠀⠘⣿⣄⣶⡄⢀⣴⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠛⠉⠀⢹⠀⠀⠀⢀⣤⣶⣶⣶⣦⣄⡀⠀⠀⠀⠀\n// ⠀⣠⡶⠋⠁⠈⠉⠁⠉⠉⠓⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠀⠀⣴⡿⠋⠁⠀⠀⠉⠛⢿⣆⠀⠀⠀\n// ⢰⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡏⢀⣾⡟⠁⠀⠀⠀⠀⠀⠀⠈⣿⡆⠀⠀\n// ⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢷⣄⠀⠀⠀⠀⠀⠀⣤⠀⠀⠀⣀⣴⠟⣠⣾⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⢹⣇⠀⠀\n// ⢻⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣷⣤⣀⠀⣀⣀⣡⣤⡶⢟⣫⣵⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⡏⠀⠀\n// ⠈⢿⣧⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⠶⠶⣶⡶⠿⠿⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⡟⠁⠀⠀\n// ⠀⠀⠙⢻⣷⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣧⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⠿⠋⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠈⠉⠛⠻⠿⣶⣶⣤⣄⣀⠀⠀⠀⠀⠀⣀⣼⡏⠀⠻⣧⣤⣀⠀⠀⢀⣠⠤⠤⠶⠶⠞⠛⠋⠁⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⠛⠿⣶⣤⡀⠀⠀⠙⣿⡆⠀⠈⢹⣯⠀⠈⠓⠲⣦⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⡆⠀⠀⢹⣷⠀⠀⠈⢿⣧⣀⠀⠀⠈⠛⣷⣶⠖⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢈⡿⠀⢀⣾⡏⠀⠀⠀⠀⠙⠻⢿⣷⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣞⣁⣤⡾⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n// ⠀⠀⠀⠀⠀⠀\n#define ll long long\n#define pb push_back\n#define pb1 pop_back\n#define ain int a[n]; for(int i = 0; i < n; i++) cin >> a[i];\n// #define v(n) vector<int>v(n)\n#define cy cout << \"Yes\" << endl;\n#define cn cout << \"No\" << endl;\n#define cY cout << \"YES\" << endl;\n#define cN cout << \"NO\" << endl;\n#define mp make_pair\n#define ff first\n#define ss second\n int arnold(vector<vector<int>>& dp, int n, int a[], int i , int cnt ) {\n if (n==0 && cnt>=2) {\n return 1; \n }\n if(i<0 || n<0) return 0 ; \n if (dp[i][n] != -1) return dp[i][n];\n\n int nottake = arnold(dp, n, a, i - 1 , cnt); // Case when we do not take a[i]\n int take = 0;\n\n if (a[i] <= n) {\n // Recursive case: take a[i] and solve for the remaining value (n - a[i])\n int result = arnold(dp, n - a[i], a, i , cnt+1);\n take = a[i] * result;\n }\n\n return dp[i][n] = max(take, nottake); // Take the max of the two choices\n }\n int integerBreak(int n) {\n int a[n] ;\n for(int i =0 ; i<n ; i++) a[i] = i+1; \n vector<vector<int>>dp(n+ 1 , vector<int>(n+1 , -1)) ; \n return arnold(dp , n , a , n-1, 0) ;\n }\n};", "memory": "8100" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int f(int n , int i , vector<vector<int>>&dp){\n if( n == 0 ) return 1;\n if( i == 0 ) return 0;\n\n if(dp[i][n]!=-1) return dp[i][n];\n\n long long np = f(n , i - 1 , dp);\n\n long long p = 0;\n if(i<=n){\n p = i*f(n - i, i , dp);\n }\n\n return dp[i][n] = max(p , np);\n }\n\n int integerBreak(int n) {\n vector<vector<int>>dp(n , vector<int>(n+1 , -1));\n\n return f(n , n-1 , dp);\n }\n};", "memory": "8100" }
343
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> 2 = 1 + 1, 1 &times; 1 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 36 <strong>Explanation:</strong> 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 58</code></li> </ul>
3
{ "code": "#include <vector>\n#include <algorithm>\n#include <climits>\n\nclass Solution {\npublic:\n int f(int i,int sum,vector<int> &v,int n, vector<vector<int>> &dp) {\n if(i >= v.size() || sum == n) return 1;\n if(sum > n) return 0;\n\n if(dp[i][sum] != -1) return dp[i][sum];\n if(v[i]+sum <= n){\n return max(v[i]*f(i,sum+v[i],v,n,dp),f(i+1,sum,v,n,dp));\n }\n else{\n return f(i+1,sum,v,n,dp);\n }\n \n }\n\n int integerBreak(int n) {\n vector<int>v;\n for(int i=1;i<n;i++) v.push_back(i);\n int m = v.size();\n vector<vector<int>> dp(m,vector<int>(n+1,-1));\n return f(1,0,v,n,dp);\n \n }\n};\n", "memory": "8200" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::array<int, 100000> nums;\nvoid parse_input_and_solve(std::ofstream& out, std::string& s) {\n s.erase(s.begin());\n s.pop_back();\n istringstream iss(s);\n string word;\n vector<string> res;\n while (getline(iss, word, ',')) {\n res.push_back(word);\n }\n out<<\"[\";\n for(int i=res.size()-1; i>=1; i--) out<<res[i]<<\",\";\n out<<res[0]<<\"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\nclass Solution {\npublic:\n void reverseString(vector<char>& s) {}\n};", "memory": "18300" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::array<int, 100000> nums;\nvoid parse_input_and_solve(std::ofstream& out, std::string& s) {\n s.erase(s.begin());\n s.pop_back();\n istringstream iss(s);\n string word;\n vector<string> res;\n while (getline(iss, word, ',')) {\n res.push_back(word);\n }\n out<<\"[\";\n for(int i=res.size()-1; i>=1; i--) out<<res[i]<<\",\";\n out<<res[0]<<\"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\nclass Solution {\npublic:\n void reverseString(vector<char>& s) {}\n};", "memory": "18600" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int n = s.size();\n int left = 0;\n int right = n-1;\n while(left <= right ){\n swap(s[left], s[right]);\n left++;\n right--;\n }\n }\n};", "memory": "26900" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int j=s.size()-1;\n int i=0;\n while(i<=j){\n swap(s[i],s[j]);\n i++;\n j--;\n }\n\n }\n};", "memory": "27000" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int j = s.size()-1;\n int i=0;\n while(i<j){\n swap(s[i],s[j]);\n j--;\n i++;\n \n }\n \n }\n};", "memory": "27000" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int start = 0;\n int end = s.size() - 1;\n while(start<end){\n swap(s[start++],s[end--]);\n }\n }\n};", "memory": "27100" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int start = 0;\n int end = s.size() - 1;\n while(start<end){\n swap(s[start++],s[end--]);\n }\n }\n};", "memory": "27100" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n\t\tint start = 0;\n\t\tint end = static_cast<int> (s.size()) - 1;\n\t\tchar c;\n\n\t\tif (end <= 0) {\n\t\t\treturn;\n\t\t}\n\n\t\twhile (start < end) {\n\t\t\tc = s[end];\n\t\t\ts[end] = s[start];\n\t\t\ts[start] = c;\n\n\t\t\tstart++;\n\t\t\tend--;\n\t\t}\n }\n};\n", "memory": "27200" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int left=0;\n int right=s.size()-1;\n char temp;\n while(left<right){\n temp=s[left];\n s[left]=s[right];\n s[right]=temp;\n left++;\n right--;\n }\n }\n};", "memory": "27200" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n reverse(s.begin(), s.end());\n // int left=0;\n // int right=s.size()-1;\n // char temp;\n // while(left<right){\n // temp=s[left];\n // s[left]=s[right];\n // s[right]=temp;\n // left++;\n // right--;\n // }\n }\n};", "memory": "27300" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int h=s.size();\n for(int i=0; i<h/2; i++){\n int temp=s[i];\n s[i]=s[h-i-1];\n s[h-i-1]=temp;\n }\n }\n};", "memory": "27300" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n int start=0;\n int end=s.size()-1;\n while(start<=end){\n swap(s[start++],s[end--]);\n }\n }\n};", "memory": "27400" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n long long int n=s.size();\n for(long long int i=0;i<n;i++){\n swap(s[i],s[n-1]);\n n--;\n }\n for(auto ch:s){\n cout<<ch<<\" \";\n }\n }\n};", "memory": "27400" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n stack <char> st ; \n for(int i=0 ; i<s.size() ; i++){\n st.push(s[i]) ;\n }\n s.clear() ;\n while( ! st.empty()){\n s.push_back(st.top()) ;\n st.pop() ;\n }\n }\n};", "memory": "27800" }
344
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = ["h","e","l","l","o"] <strong>Output:</strong> ["o","l","l","e","h"] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = ["H","a","n","n","a","h"] <strong>Output:</strong> ["h","a","n","n","a","H"] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii character</a>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void reverseString(vector<char>& s) {\n stack <char> ns;\n for(int i=0;i<s.size();i++)\n ns.push(s[i]);\n for(int i=0;i<s.size();i++)\n {\n s[i]=ns.top();\n ns.pop();\n }\n }\n};", "memory": "27800" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n int start = 0;\n int end = s.size() - 1;\n\n while (start < end) {\n if (!isVowel(s[start])) {\n start++;\n } else if (!isVowel(s[end])) {\n end--;\n } else {\n char temp = s[start];\n s[start] = s[end];\n s[end] = temp;\n start++;\n end--;\n }\n }\n return s;\n }\n\n bool isVowel(char c) {\n if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {\n return true;\n }\n return false;\n }\n};", "memory": "9000" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool check(char ch) {\n return (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U');\n }\n\n string reverseVowels(string s) {\n int l = 0, r = s.size() - 1;\n while (l < r) {\n while (l < s.size() && !check(s[l])) {\n ++l;\n }\n while (r >= 0 && !check(s[r])) {\n --r;\n }\n if (l < r) {\n swap(s[l], s[r]);\n ++l;\n --r;\n }\n }\n return s;\n }\n};\n", "memory": "9100" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isVowel(char i){\n if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or\n i=='A' or i=='E' or i=='I' or i=='O' or i=='U'){\n return true;\n }\n return false;\n }\n string reverseVowels(string s) {\n \n int i=0, j=s.size()-1;\n\n while(i<j){\n\n if(isVowel(s[i]) and isVowel(s[j])){\n if(s[i]==s[j]){\n j--;\n i++;\n }\n else{\n swap(s[i],s[j]);\n i++;j--;\n }\n }\n else if(isVowel(s[i]) and !isVowel(s[j])){\n j--;\n }\n else{\n i++;\n }\n }\n return s;\n }\n};", "memory": "9100" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
0
{ "code": "class Solution {\n bool isVowel(char c){\n return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'\n || c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';\n }\npublic:\n string reverseVowels(string s) {\n int start=0,end=s.size()-1;\n string check = \"aeiouAEIOU\";\n while(start < end){\n // char a = s[start];\n // char b = s[end];\n if(isVowel(s[start]) && isVowel(s[end])){\n swap(s[start],s[end]);\n start++;end--;\n }\n while(start < end && !isVowel(s[start])){\n start++;\n }\n while(start < end && !isVowel(s[end])){\n end--;\n }\n }\n return s;\n }\n};", "memory": "9200" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isVowel(char c) {\n c = tolower(c);\n if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {\n return true;\n }\n return false;\n }\n string reverseVowels(string s) {\n int x = 0 , y = s.length() - 1;\n\n while (x < y) {\n while (x < y && !isVowel(s[x])) {\n ++ x;\n }\n while (x < y && !isVowel(s[y])) {\n -- y;\n }\n\n swap(s[x], s[y]);\n ++ x , -- y;\n }\n return s;\n }\n};", "memory": "9200" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int i = 0;\n int j = s.size() - 1;\n\n // Function to check if a character is a vowel\n auto isVowel = [](char c) {\n c = tolower(c);\n return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';\n };\n\n // Two-pointer approach to reverse the vowels\n while (i < j) {\n // Find the next vowel from the start\n while (i < j && !isVowel(s[i])) {\n i++;\n }\n // Find the next vowel from the end\n while (i < j && !isVowel(s[j])) {\n j--;\n }\n\n // Swap the vowels\n if (i < j) {\n swap(s[i], s[j]);\n i++;\n j--;\n }\n }\n\n return s; // Return the modified string\n }\n};\n", "memory": "9600" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\nbool isvowel(char c){\n c = tolower(c); \n if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u') return true;\n return false;\n}\n string reverseVowels(string s) {\n string p=s;\n int i=0,j=s.length()-1;\n while(i<j){\n if(isvowel(p[i]) && isvowel(p[j])){\n swap(p[i], p[j]); \n i++;\n j--;\n }\n else if(isvowel(p[i]) && !isvowel(p[j])){\n j--;\n }\n else if(isvowel(p[j]) && !isvowel(p[i])) i++;\n else {\n i++;\n j--;\n }\n }\n return p;\n }\n};", "memory": "9600" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n vector<char> S;\n int itr=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i]=='A' || s[i]=='e' || s[i]=='E' || s[i]=='i' || s[i]=='I' || s[i]=='o' || s[i]=='O' || s[i]=='u' ||s[i]=='U' ){\n S.push_back(s[i]);\n }\n }\n reverse(S.begin(),S.end());\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i]=='A' || s[i]=='e' || s[i]=='E' || s[i]=='i' || s[i]=='I' || s[i]=='o' || s[i]=='O' || s[i]=='u' ||s[i]=='U' ){\n s[i] = S[itr++];\n }\n }\n return s;\n }\n};", "memory": "9700" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n // Convert the input string to a character array.\n string word = s;\n int start = 0;\n int end = s.length() - 1;\n string vowels = \"aeiouAEIOU\";\n \n // Loop until the start pointer is no longer less than the end pointer.\n while (start < end) {\n // Move the start pointer towards the end until it points to a vowel.\n while (start < end && vowels.find(word[start]) == string::npos) {\n start++;\n }\n \n // Move the end pointer towards the start until it points to a vowel.\n while (start < end && vowels.find(word[end]) == string::npos) {\n end--;\n }\n \n // Swap the vowels found at the start and end positions.\n swap(word[start], word[end]);\n \n // Move the pointers towards each other for the next iteration.\n start++;\n end--;\n }\n \n // Return the modified string.\n return word;\n }\n};", "memory": "9700" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n char vowels[] = {'a','e','i','o','u','A','E','I','O','U'};\n int len = sizeof(vowels)/sizeof(vowels[0]);\n vector<char> v;\n for(int i=0;i<s.length();i++){\n for(int j=0;j<len;j++){\n if(s[i]==vowels[j]){\n v.push_back(s[i]);\n }\n }\n }\n reverse(v.begin(),v.end());\n\n int indx=0;\n for(int i=0;i<s.length();i++){\n for(int j=0;j<len;j++){\n if(s[i]==vowels[j]){\n s[i]=v[indx++];\n break;\n }\n }\n }\n return s;\n }\n};", "memory": "9800" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n char vowels[] = {'a','e','i','o','u','A','E','I','O','U'};\n int len = sizeof(vowels)/sizeof(vowels[0]);\n vector<char> v;\n for(int i=0;i<s.length();i++){\n for(int j=0;j<len;j++){\n if(s[i]==vowels[j]){\n v.push_back(s[i]);\n }\n }\n }\n reverse(v.begin(),v.end());\n\n int indx=0;\n for(int i=0;i<s.length();i++){\n for(int j=0;j<len;j++){\n if(s[i]==vowels[j]){\n s[i]=v[indx++];\n break;\n }\n }\n }\n return s;\n }\n};", "memory": "9800" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool vowel(char ch){\n return (ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u');\n }\n\n string reverseVowels(string s) {\n string vowels = \"\";\n for(int i=0;i<s.length();i++){\n if(vowel(s[i])){\n vowels+=s[i];\n }\n }\n string rvowels = \"\";\n for(int i=vowels.length()-1;i>=0;i--){\n rvowels+=vowels[i];\n }\n int j = 0;\n for(int i=0;i<s.length();i++){\n if(vowel(s[i])){\n s[i] = rvowels[j++];\n }\n }\n return s;\n }\n};", "memory": "9900" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n int i=0;\n int n=s.size()-1;\n set<char> ch{'a','e','i','o','u','A','E','I','O','U'};\n while(i<n){\n if(ch.find(s[n])==ch.end()) n--;\n else if(ch.find(s[i])==ch.end()) i++;\n else {swap(s[i],s[n]);i++;n--;}\n } return s;\n }\n};", "memory": "9900" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n stack<char> sw;\n char dd;\n\n // Push vowels onto the stack\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == 'I' || s[i] == 'i' || s[i] == 'a' || s[i] == 'A' || \n s[i] == 'E' || s[i] == 'e' || s[i] == 'o' || s[i] == 'O' || \n s[i] == 'U' || s[i] == 'u') {\n sw.push(s[i]);\n }\n }\n\n // Pop from the stack and replace vowels in the original string\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == 'I' || s[i] == 'i' || s[i] == 'a' || s[i] == 'A' || \n s[i] == 'E' || s[i] == 'e' || s[i] == 'o' || s[i] == 'O' || \n s[i] == 'U' || s[i] == 'u') {\n dd = sw.top();\n s[i] = dd;\n sw.pop();\n }\n }\n\n return s;\n }\n};", "memory": "10000" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n set<char>vowels={'A','E','I','O','U','a','e','i','o','u'};\n int l=0;\n int r=s.size()-1;\n while(l<r){\n while(l<r&&vowels.find(s[l])==vowels.end()){\n l++;\n }\n while (l<r&&vowels.find(s[r]) == vowels.end()){\n r--;\n }\n if(l<r){\n swap(s[l],s[r]);\n l++;\n r--;\n }\n }\n return s;\n }\n};", "memory": "10000" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n set<char>vowels={'A','E','I','O','U','a','e','i','o','u'};\n int l=0;\n int r=s.size()-1;\n while(l<r){\n while(l<r&&vowels.find(s[l])==vowels.end()){\n l++;\n }\n while(l<r&&vowels.find(s[r])==vowels.end()){\n r--;\n }\n if(l<r){\n swap(s[l],s[r]);\n l++;\n r--;\n }\n }\n return s;\n }\n};", "memory": "10100" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n int len = s.size();\n string result = \"\";\n vector<int> vec;\n\n for (int i = 0; i < len; i++) {\n if (s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U') vec.push_back(i);\n }\n\n len = vec.size();\n\n for (int i = 0; i < len / 2; i++) {\n swap(s[vec[i]], s[vec[len - 1 - i]]);\n }\n return s;\n }\n};", "memory": "10100" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n vector<char>buffer;\n string ans;\n for(int i = 0; i < s.length(); i++)\n {\n if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')\n {\n buffer.push_back(s[i]);\n }\n }\n for(int i = 0; i < s.length(); i++)\n {\n if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')\n {\n ans+=buffer[buffer.size()-1];\n buffer.pop_back();\n }\n else\n ans+=s[i];\n }\n return ans;\n }\n};", "memory": "10200" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n int len = s.size();\n string result = \"\";\n vector<int> vec;\n\n for (int i = 0; i < len; i++) {\n if (s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U') vec.push_back(i);\n }\n\n len = vec.size();\n\n for (int i = 0; i < len / 2; i++) {\n swap(s[vec[i]], s[vec[len - 1 - i]]);\n }\n return s;\n }\n};", "memory": "10200" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n vector<char> v;\n for(int i=0;i<s.length();i++){\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O'|| s[i]=='U'){\n v.push_back(s[i]);\n }\n }\n reverse(v.begin(),v.end());\n string ans=\"\";\n int k=0;\n for(int i=0;i<s.length();i++){\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O'|| s[i]=='U'){\n ans.push_back(v[k]);\n k++;\n }\n else{\n ans.push_back(s[i]);\n }\n\n }\n return ans;\n\n \n }\n};", "memory": "10300" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n unordered_set<char> vowels ({'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'});\n\n for (int i = 0, j = s.size()-1; i < j; i++) {\n if ( !vowels.contains(s[i]) )\n continue;\n \n while ( !vowels.contains(s[j]) ) { \n if (i > j)\n return s;\n j--;\n }\n\n char tmp = s[i];\n s[i] = s[j];\n s[j] = tmp;\n\n --j;\n }\n\n return s;\n }\n};", "memory": "10300" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', \n 'A', 'E', 'I', 'O', 'U'}; // Use set for O(1) lookup\n int start = 0;\n int end = s.size() - 1;\n\n while (start < end) {\n // Move start forward if not a vowel\n while (start < end && vowels.find(s[start]) == vowels.end()) {\n start++;\n }\n \n // Move end backward if not a vowel\n while (start < end && vowels.find(s[end]) == vowels.end()) {\n end--;\n }\n\n // Swap vowels\n if (start < end) {\n swap(s[start], s[end]);\n start++;\n end--;\n }\n }\n\n return s;\n }\n};", "memory": "10400" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n string vowel = \"\";\n unordered_set<char> vowels = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'};\n\n for (int i = 0; i < s.size(); i++) {\n if (vowels.count(s[i])) { \n vowel.push_back(s[i]);\n }\n }\n\n for (int i = 0; i < s.size(); i++) {\n if (vowels.count(s[i])) { \n s[i] = vowel.back(); \n vowel.pop_back(); \n }\n }\n\n return s;\n }\n};\n", "memory": "10400" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n vector<int> ind;\n vector<char> vow;\n for(int i=0;i<s.length();i++){\n if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){\n ind.push_back(i);\n vow.push_back(s[i]);\n }\n }\n reverse(vow.begin(),vow.end());\n for(int i=0;i<ind.size();i++){\n s[ind[i]]=vow[i];\n }\n return s;\n }\n};", "memory": "10500" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n vector<int> v;\n vector<char> v1;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i]=='e' ||s[i]=='i' || s[i]=='o'|| s[i]=='u' || s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n \n v.push_back(i);\n v1.push_back(s[i]);\n }\n }\n reverse(v1.begin(),v1.end());\n for(int i=0; i<v.size();i++){\n s[v[i]]=v1[i];\n }\n return s;\n }\n};", "memory": "10500" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n vector<char> vowels;\n string all_vowel = \"aeiouAEIOU\";\n int left = 0;\n int right = s.size() - 1;\n bool left_vowel = false;\n bool right_vowel = false;\n while(left <= right){\n left_vowel = is_vowel(s[left], all_vowel);\n right_vowel = is_vowel(s[right], all_vowel);\n if (!left_vowel) left++;\n if (!right_vowel) right--;\n if (left_vowel && right_vowel){\n char temp = s[left];\n s[left] = s[right];\n s[right] = temp;\n left++;\n right--;\n }\n }\n return s;\n }\n bool is_vowel(char s, string all_vowels){\n bool flag = false;\n for (int i = 0; i < all_vowels.size(); i++){\n if (all_vowels[i] == s){\n flag = true;\n break;\n }\n }\n return flag;\n }\n\n};", "memory": "10600" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n string reverseVowels(string s) {\n int i = 0, j = s.size()-1;\n unordered_set<char> vovels;\n vovels.insert('a');\n vovels.insert('e');\n vovels.insert('i');\n vovels.insert('o');\n vovels.insert('u');\n vovels.insert('A');\n vovels.insert('E');\n vovels.insert('I');\n vovels.insert('O');\n vovels.insert('U');\n\n while(i < j)\n {\n while(vovels.find(s[i]) == vovels.end() && i < s.size()-1) {\n i++;\n // cout << \"i: \"<< i << endl;\n }\n while(vovels.find(s[j]) == vovels.end() && j > 0) {\n j--;\n // cout << \"j: \"<< j << endl;\n }\n if(i < j) {\n swap(s[i], s[j]);\n i++;\n j--;\n }\n\n } \n return s; \n }\n};", "memory": "10700" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n string vs = \"\";\n unordered_set<char> vovelSet;\n vovelSet.insert('a');\n vovelSet.insert('e');\n vovelSet.insert('i');\n vovelSet.insert('o');\n vovelSet.insert('u');\n vovelSet.insert('A');\n vovelSet.insert('E');\n vovelSet.insert('I');\n vovelSet.insert('O');\n vovelSet.insert('U');\n for(char c:s)\n if(vovelSet.find(c)!=vovelSet.end())\n vs += c;\n reverse(vs.begin(), vs.end());\n int itr = 0;\n for(int i=0; i<s.length(); i++)\n if(vovelSet.find(s[i])!=vovelSet.end())\n s[i] = vs[itr++];\n return s;\n }\n};", "memory": "10700" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n vector<pair<char, int>> vowels;\n string vowelSet = \"aeiouAEIOU\";\n \n for (int i = 0; i < s.size(); ++i) {\n if (vowelSet.find(s[i]) != string::npos) {\n vowels.push_back({s[i], i});\n }\n }\n \n int j = vowels.size() - 1;\n for (auto &pair : vowels) {\n s[pair.second] = vowels[j--].first;\n }\n \n return s;\n }\n};\n", "memory": "10800" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n string result = \"\";\n stack<char> strStack;\n vector<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};\n\n for (char c : s) {\n if (find(vowels.begin(), vowels.end(), c) != vowels.end()) strStack.push(c);\n }\n\n for (char c : s) {\n if (find(vowels.begin(), vowels.end(), c) != vowels.end()) {\n result += strStack.top();\n strStack.pop();\n }\n else result += c;\n }\n return result;\n }\n};", "memory": "10900" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n int n = s.length(),k = 0;\n string st = s;\n vector<char> v = {'a','e','i','o','u','A','E','I','O','U'};\n vector<pair<char,int>> store;\n \n for(int i = 0; i < n;i++){\n if(find(v.begin(), v.end(), s[i]) != v.end()){\n store.push_back({s[i],i});\n k++;\n }\n }\n for(int i = 0;i < k;i++)\n s[store[i].second] = store[k-i-1].first;\n\n return s;\n }\n};", "memory": "10900" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n int i=0, j=s.size()-1;\n string x = \"aeiouAEIOU\";\n map<char, bool> mp;\n for(auto &ch:x) mp[ch]=true;\n while(i<j){\n while(i<j && mp.find(s[i])==mp.end()) i++;\n while(i<j && mp.find(s[j])==mp.end()) j--;\n if(i<j){\n swap(s[i], s[j]); i++; j--;\n }\n }\n return s;\n }\n};", "memory": "11000" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "/*\nAa - 65/97\nEe - 69/101\nIi - 73/105\nOo - 79/111\nUu - 85/117\n\non finding vowel, note which vowel & location\n*/\n\nclass Solution {\npublic:\n string reverseVowels(string s) {\n vector<char> vowels;\n vector<size_t> positions;\n\n for (auto i = 0; i < s.length(); i++) {\n switch (s[i]) {\n case 'A':\n case 'a':\n case 'E':\n case 'e':\n case 'I':\n case 'i':\n case 'O':\n case 'o':\n case 'U':\n case 'u':\n vowels.push_back(s[i]);\n positions.insert(positions.begin(), i);\n }\n }\n\n for (auto i = 0; i < vowels.size(); i++)\n s[positions[i]] = vowels[i];\n\n return s;\n }\n};", "memory": "11000" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n \n\n int indexFromStart = 0;\n int indexFromEnd = s.length() - 1;\n\n while (indexFromStart < indexFromEnd) {\n\n // inc the Startindex until its on a wowel\n\n while (!isVowel(s[indexFromStart]) && indexFromStart<indexFromEnd) {\n indexFromStart++;\n }\n\n // dec the endIndex until its on a vowel\n while (!isVowel(s[indexFromEnd]) &&indexFromEnd> indexFromStart) {\n indexFromEnd--;\n }\n\n // break if they went too far\n if (indexFromStart >= indexFromEnd) {\n break;\n }\n\n cout<<\"reachedHere\";\n cout<<endl<<indexFromStart<<endl<<indexFromEnd<<endl;\n\n // swap the two\n char temp = s[indexFromStart];\n s[indexFromStart] = s[indexFromEnd];\n s[indexFromEnd] = temp;\n\n indexFromStart++;\n indexFromEnd--;\n }\n\n return s;\n }\n\n \n bool isVowel(char c) {\n string vowelList = \"aeiouAEIOU\";\n for (int i = 0; i < vowelList.length(); i++) {\n if (c == vowelList[i]) {\n return true;\n }\n }\n\n return false;\n }\n};", "memory": "11100" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string reverseVowels(string s) {\n unordered_set<char> vowels{\n 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};\n vector<char> exist_vowels;\n for (char c : s) {\n if (vowels.count(c) > 0) {\n exist_vowels.push_back(c);\n }\n }\n string result;\n for (char c : s) {\n if (vowels.count(c) > 0) {\n result.push_back(exist_vowels.back());\n exist_vowels.pop_back();\n } else {\n result.push_back(c);\n }\n }\n return result;\n }\n};", "memory": "11100" }
345
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p> <p><strong>Explanation:</strong></p> <p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool check(char c){\n string str = \"aeiouAEIOU\";\n \n for(int i=0;i<str.length();i++)\n {\n if(str[i]==c) return true;\n }\n \n return false;\n }\n string reverseVowels(string s) {\n string exa = \"\";\n int n = s.length();\n int i =0;\n int j = n-1;\n\n while(i<j){\n if(check(s[i])&&check(s[j])){\n swap(s[i],s[j]);\n i++;\n j--;\n }\n \n else if(!check(s[i])){\n i++;\n }\n else if(!check(s[j])){\n j--;\n }\n }\n\n\n return s;\n\n }\n};", "memory": "11200" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n // vector<int> alt(gain.size()+1,0);\n // for(int i=0;i<gain.size();i++){\n // alt[i+1]=gain[i]+alt[i];\n // }\n // return *max_element(alt.begin(),alt.end());\n int cur=0;\n int maxi=0;\n for(int i=0;i<gain.size();i++){\n cur+=gain[i];\n maxi=max(maxi,cur);\n }\n return maxi;\n }\n};", "memory": "9700" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n int current_alt = 0, max_alt = 0;\n for(int alt: gain){\n current_alt += alt;\n max_alt = max(current_alt,max_alt);\n }\n return max_alt;\n }\n};", "memory": "9800" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n int currAlt = 0;\n int maxAlt = 0;\n for(int i = 0; i < gain.size(); i++){\n currAlt += gain[i];\n maxAlt = max(maxAlt, currAlt);\n }\n return maxAlt;\n }\n};", "memory": "9800" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n int alt = 0;\n int maxAlt = 0;\n for(int step : gain){\n alt += step;\n maxAlt = max(maxAlt, alt);\n }\n return maxAlt;\n }\n};", "memory": "9900" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n int prefixSum=0;\n int maxAltitude=0;//starts from 0 altitude\n for(int g:gain){\n prefixSum+=g;\n maxAltitude=max(maxAltitude, prefixSum);\n }\n return maxAltitude;\n }\n};", "memory": "9900" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n int height=0;\n int maxheight=0;\n\n for (int i =0;i<gain.size();i++){\n height=height+gain[i];\n if(maxheight<height){\n maxheight=height;\n\n }\n }\n return maxheight ;\n }\n};", "memory": "10000" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n int ans = 0;\n int st = 0;\n for(int i = 0;i<gain.size();i++){\n st+= gain[i];\n ans = max(ans,st);\n }\n return ans;\n }\n};", "memory": "10000" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n int currAltitude = 0;\n for (int& n : gain) {\n int nextAltitude = currAltitude + n;\n n = currAltitude;\n currAltitude = nextAltitude;\n }\n gain.push_back(currAltitude);\n sort(gain.begin(), gain.end());\n return gain.back();\n }\n};", "memory": "10100" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) {\n vector <int> result;\n int size = gain.size();\n result.push_back(0);\n\n for(int i = 0 ; i<size ; i++){\n result.push_back(result[i]+gain[i]);\n }\n return *max_element(result.begin(), result.end());\n }\n};", "memory": "10100" }
1,833
<p>There is a biker going on a road trip. The road trip consists of <code>n + 1</code> points at different altitudes. The biker starts his trip on point <code>0</code> with altitude equal <code>0</code>.</p> <p>You are given an integer array <code>gain</code> of length <code>n</code> where <code>gain[i]</code> is the <strong>net gain in altitude</strong> between points <code>i</code>​​​​​​ and <code>i + 1</code> for all (<code>0 &lt;= i &lt; n)</code>. Return <em>the <strong>highest altitude</strong> of a point.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gain = [-5,1,5,0,-7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gain = [-4,-3,-2,-1,4,3,2] <strong>Output:</strong> 0 <strong>Explanation:</strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == gain.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>-100 &lt;= gain[i] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int largestAltitude(vector<int>& gain) \n {\n int maxalt = 0;\n int currentalt = 0;\n for(int i=0;i<gain.size();i++)\n {\n currentalt = currentalt + gain[i];\n if(currentalt>maxalt)\n {\n maxalt = currentalt;\n }\n }\n return maxalt;\n }\n};", "memory": "10200" }