title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
✅ [Python] 2-coloring using DFS / BFS | possible-bipartition | 0 | 1 | **\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\n\n**Comment.** The idea of this solution consists in traversing the graph of dislikes and coloring each next vertex into the opposite color. If, on some iteratrion, there is a collision (i.e., coloring second time with a different color) then the bipartition i... | 10 | We want to split a group of `n` people (labeled from `1` to `n`) into two groups of **any size**. Each person may dislike some other people, and they should not go into the same group.
Given the integer `n` and the array `dislikes` where `dislikes[i] = [ai, bi]` indicates that the person labeled `ai` does not like the... | null |
✅ [Python] 2-coloring using DFS / BFS | possible-bipartition | 0 | 1 | **\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\n\n**Comment.** The idea of this solution consists in traversing the graph of dislikes and coloring each next vertex into the opposite color. If, on some iteratrion, there is a collision (i.e., coloring second time with a different color) then the bipartition i... | 10 | Given an array of integers `nums`, half of the integers in `nums` are **odd**, and the other half are **even**.
Sort the array so that whenever `nums[i]` is odd, `i` is **odd**, and whenever `nums[i]` is even, `i` is **even**.
Return _any answer array that satisfies this condition_.
**Example 1:**
**Input:** nums =... | null |
A terrible, iterative Python solution using far too many parallel data structures | possible-bipartition | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI immediately recognized this as a Union-Find problem, then wasted a bunch of time trying to code that from memory. I am trying blind challenges, so not allowing myself to look things up while coming up with a solution. Failing that, I d... | 2 | We want to split a group of `n` people (labeled from `1` to `n`) into two groups of **any size**. Each person may dislike some other people, and they should not go into the same group.
Given the integer `n` and the array `dislikes` where `dislikes[i] = [ai, bi]` indicates that the person labeled `ai` does not like the... | null |
A terrible, iterative Python solution using far too many parallel data structures | possible-bipartition | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI immediately recognized this as a Union-Find problem, then wasted a bunch of time trying to code that from memory. I am trying blind challenges, so not allowing myself to look things up while coming up with a solution. Failing that, I d... | 2 | Given an array of integers `nums`, half of the integers in `nums` are **odd**, and the other half are **even**.
Sort the array so that whenever `nums[i]` is odd, `i` is **odd**, and whenever `nums[i]` is even, `i` is **even**.
Return _any answer array that satisfies this condition_.
**Example 1:**
**Input:** nums =... | null |
Python | DP | Binary Search | super-egg-drop | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | You are given `k` identical eggs and you have access to a building with `n` floors labeled from `1` to `n`.
You know that there exists a floor `f` where `0 <= f <= n` such that any egg dropped at a floor **higher** than `f` will **break**, and any egg dropped **at or below** floor `f` will **not break**.
Each move, y... | null |
Python | DP | Binary Search | super-egg-drop | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.
As the answer can be very large, return it **modulo** `109 + 7`.
**Example 1:**
**Input:** arr = \[1,1,2,2,3,3,4,4,5,5\], target = 8
**Output:** 20
**Explanatio... | null |
One liner | O(KlogN) Time | O(1) Memory | Intuition + Explanation | Beats 95% time and 100% Memory | super-egg-drop | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe don\'t have to worry about finding the optimal strategy to determine the answer. We just need to find the number of minimum drops (by playing some optimal strategy).\n\n# Approach\n<!-- Describe your approach to solving the problem. --... | 1 | You are given `k` identical eggs and you have access to a building with `n` floors labeled from `1` to `n`.
You know that there exists a floor `f` where `0 <= f <= n` such that any egg dropped at a floor **higher** than `f` will **break**, and any egg dropped **at or below** floor `f` will **not break**.
Each move, y... | null |
One liner | O(KlogN) Time | O(1) Memory | Intuition + Explanation | Beats 95% time and 100% Memory | super-egg-drop | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe don\'t have to worry about finding the optimal strategy to determine the answer. We just need to find the number of minimum drops (by playing some optimal strategy).\n\n# Approach\n<!-- Describe your approach to solving the problem. --... | 1 | Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.
As the answer can be very large, return it **modulo** `109 + 7`.
**Example 1:**
**Input:** arr = \[1,1,2,2,3,3,4,4,5,5\], target = 8
**Output:** 20
**Explanatio... | null |
Matrix chain multiplication with Python | super-egg-drop | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given `k` identical eggs and you have access to a building with `n` floors labeled from `1` to `n`.
You know that there exists a floor `f` where `0 <= f <= n` such that any egg dropped at a floor **higher** than `f` will **break**, and any egg dropped **at or below** floor `f` will **not break**.
Each move, y... | null |
Matrix chain multiplication with Python | super-egg-drop | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.
As the answer can be very large, return it **modulo** `109 + 7`.
**Example 1:**
**Input:** arr = \[1,1,2,2,3,3,4,4,5,5\], target = 8
**Output:** 20
**Explanatio... | null |
Solution | super-egg-drop | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int superEggDrop(int K, int N) {\n int dp[N+1][K+1]; memset(dp, 0, sizeof dp);\n int m = 0;\n while (dp[m][K] < N) {\n m++;\n for (int k = 1; k <= K; ++k)\n dp[m][k] = dp[m-1][k- 1] + dp[m-1][k] + 1;\n }\n ... | 1 | You are given `k` identical eggs and you have access to a building with `n` floors labeled from `1` to `n`.
You know that there exists a floor `f` where `0 <= f <= n` such that any egg dropped at a floor **higher** than `f` will **break**, and any egg dropped **at or below** floor `f` will **not break**.
Each move, y... | null |
Solution | super-egg-drop | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int superEggDrop(int K, int N) {\n int dp[N+1][K+1]; memset(dp, 0, sizeof dp);\n int m = 0;\n while (dp[m][K] < N) {\n m++;\n for (int k = 1; k <= K; ++k)\n dp[m][k] = dp[m-1][k- 1] + dp[m-1][k] + 1;\n }\n ... | 1 | Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.
As the answer can be very large, return it **modulo** `109 + 7`.
**Example 1:**
**Input:** arr = \[1,1,2,2,3,3,4,4,5,5\], target = 8
**Output:** 20
**Explanatio... | null |
Solution | fair-candy-swap | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {\n bitset<200002> bf;\n int sumA = 0, sumB = 0;\n for(auto n: A) {\n sumA += n;\n }\n for(auto n: B) {\n sumB += n;\n bf.set(2*n);\n }\n ... | 1 | Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and `bobSizes` where `aliceSizes[i]` is the number of candies of the `ith` box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has.
Since they are friends, they wo... | null |
Solution | fair-candy-swap | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {\n bitset<200002> bf;\n int sumA = 0, sumB = 0;\n for(auto n: A) {\n sumA += n;\n }\n for(auto n: B) {\n sumB += n;\n bf.set(2*n);\n }\n ... | 1 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Math behind lee215's solution | fair-candy-swap | 0 | 1 | Assume that alice has sizes $[x_1, x_2, ..., x_m]$, while bob has sizes $[y_1, y_2, ..., y_n]$. Also, we are going to switch $x_a$ and $y_b$ so that they will have some size total at the end. Then, we have\n\n$$\\sum_{i=1}^nx_i - x_a + y_b = \\sum_{j=1}^my_j - y_b + x_a$$\n\nwhich can be rewritten as \n\n$$\\sum_{i=1}^... | 10 | Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and `bobSizes` where `aliceSizes[i]` is the number of candies of the `ith` box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has.
Since they are friends, they wo... | null |
Math behind lee215's solution | fair-candy-swap | 0 | 1 | Assume that alice has sizes $[x_1, x_2, ..., x_m]$, while bob has sizes $[y_1, y_2, ..., y_n]$. Also, we are going to switch $x_a$ and $y_b$ so that they will have some size total at the end. Then, we have\n\n$$\\sum_{i=1}^nx_i - x_a + y_b = \\sum_{j=1}^my_j - y_b + x_a$$\n\nwhich can be rewritten as \n\n$$\\sum_{i=1}^... | 10 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Python. Super simple solution. | fair-candy-swap | 0 | 1 | ```\nclass Solution:\n def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:\n difference = (sum(A) - sum(B)) / 2\n A = set(A)\n for candy in set(B):\n if difference + candy in A:\n return [difference + candy, candy] | 15 | Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and `bobSizes` where `aliceSizes[i]` is the number of candies of the `ith` box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has.
Since they are friends, they wo... | null |
Python. Super simple solution. | fair-candy-swap | 0 | 1 | ```\nclass Solution:\n def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:\n difference = (sum(A) - sum(B)) / 2\n A = set(A)\n for candy in set(B):\n if difference + candy in A:\n return [difference + candy, candy] | 15 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
python3 with explanation | fair-candy-swap | 0 | 1 | sum(alice)-i+j=sum(bob)-j+i\n diff=2(i-j)\n diff/2=(i-j)\n diff/2+j=i\n j=i-diff/2\n```\nclass Solution:\n def fairCandySwap(self, aliceSizes: List[int], bobSizes: List[int]) -> List[int]:\n total_alice=sum(aliceSizes)\n total_bob=sum(bobSizes)\n diff=(total_alice-tot... | 5 | Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and `bobSizes` where `aliceSizes[i]` is the number of candies of the `ith` box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has.
Since they are friends, they wo... | null |
python3 with explanation | fair-candy-swap | 0 | 1 | sum(alice)-i+j=sum(bob)-j+i\n diff=2(i-j)\n diff/2=(i-j)\n diff/2+j=i\n j=i-diff/2\n```\nclass Solution:\n def fairCandySwap(self, aliceSizes: List[int], bobSizes: List[int]) -> List[int]:\n total_alice=sum(aliceSizes)\n total_bob=sum(bobSizes)\n diff=(total_alice-tot... | 5 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
[Python] Binary Search with explanation | fair-candy-swap | 0 | 1 | * This is the first time I posted my solution, and I am a beginner in algorism, hope this simple solution can give you some help. \n* Please feel free to comment, hope to learn more from you!\n```\nclass Solution(object):\n def fairCandySwap(self, aliceSizes, bobSizes):\n """\n :type aliceSizes: List[i... | 6 | Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and `bobSizes` where `aliceSizes[i]` is the number of candies of the `ith` box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has.
Since they are friends, they wo... | null |
[Python] Binary Search with explanation | fair-candy-swap | 0 | 1 | * This is the first time I posted my solution, and I am a beginner in algorism, hope this simple solution can give you some help. \n* Please feel free to comment, hope to learn more from you!\n```\nclass Solution(object):\n def fairCandySwap(self, aliceSizes, bobSizes):\n """\n :type aliceSizes: List[i... | 6 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Solution | construct-binary-tree-from-preorder-and-postorder-traversal | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {\n int n = preorder.size() - 1;\n return build(preorder, postorder, 0, n, 0, n);\n }\n TreeNode* build(vector<int>& preorder, vector<int>& postorder,\n int preL... | 1 | Given two integer arrays, `preorder` and `postorder` where `preorder` is the preorder traversal of a binary tree of **distinct** values and `postorder` is the postorder traversal of the same tree, reconstruct and return _the binary tree_.
If there exist multiple answers, you can **return any** of them.
**Example 1:**... | null |
Solution | construct-binary-tree-from-preorder-and-postorder-traversal | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {\n int n = preorder.size() - 1;\n return build(preorder, postorder, 0, n, 0, n);\n }\n TreeNode* build(vector<int>& preorder, vector<int>& postorder,\n int preL... | 1 | Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times.
You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly ... | null |
🔥🔥 || Beats 100% python Binary search Tree || 🔥🔥 | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | Given two integer arrays, `preorder` and `postorder` where `preorder` is the preorder traversal of a binary tree of **distinct** values and `postorder` is the postorder traversal of the same tree, reconstruct and return _the binary tree_.
If there exist multiple answers, you can **return any** of them.
**Example 1:**... | null |
🔥🔥 || Beats 100% python Binary search Tree || 🔥🔥 | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times.
You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly ... | null |
Perhaps shortest Python O(n) solution | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n\nModified from [lee215\'s solution](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/solutions/161268/c-java-python-one-pass-real-o-n/), with two index vars replaced by pop operations.\n\n# Complexity\n- Time ... | 1 | Given two integer arrays, `preorder` and `postorder` where `preorder` is the preorder traversal of a binary tree of **distinct** values and `postorder` is the postorder traversal of the same tree, reconstruct and return _the binary tree_.
If there exist multiple answers, you can **return any** of them.
**Example 1:**... | null |
Perhaps shortest Python O(n) solution | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n\nModified from [lee215\'s solution](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/solutions/161268/c-java-python-one-pass-real-o-n/), with two index vars replaced by pop operations.\n\n# Complexity\n- Time ... | 1 | Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times.
You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly ... | null |
Python3 Solution with a Detailed Explanation - Construct Binary Tree from | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | Make sure you understand the preorder and postorder traversal and how the nodes are arranged in each, with respect to the other one. [This](https://www.***.org/if-you-are-given-two-traversal-sequences-can-you-construct-the-binary-tree/) might help. I also find [this](https://www.***.org/full-and-complete-binary-tree-fr... | 43 | Given two integer arrays, `preorder` and `postorder` where `preorder` is the preorder traversal of a binary tree of **distinct** values and `postorder` is the postorder traversal of the same tree, reconstruct and return _the binary tree_.
If there exist multiple answers, you can **return any** of them.
**Example 1:**... | null |
Python3 Solution with a Detailed Explanation - Construct Binary Tree from | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | Make sure you understand the preorder and postorder traversal and how the nodes are arranged in each, with respect to the other one. [This](https://www.***.org/if-you-are-given-two-traversal-sequences-can-you-construct-the-binary-tree/) might help. I also find [this](https://www.***.org/full-and-complete-binary-tree-fr... | 43 | Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times.
You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly ... | null |
[Python3] Same template to solve 3 Binary tree construction problems | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | **Construct using Preorder and Postorder**\n\n```\nclass Solution:\n def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> Optional[TreeNode]:\n if not preorder or not postorder:\n return\n \n root = TreeNode(preorder[0])\n if len(preorder) == 1:\n ... | 1 | Given two integer arrays, `preorder` and `postorder` where `preorder` is the preorder traversal of a binary tree of **distinct** values and `postorder` is the postorder traversal of the same tree, reconstruct and return _the binary tree_.
If there exist multiple answers, you can **return any** of them.
**Example 1:**... | null |
[Python3] Same template to solve 3 Binary tree construction problems | construct-binary-tree-from-preorder-and-postorder-traversal | 0 | 1 | **Construct using Preorder and Postorder**\n\n```\nclass Solution:\n def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> Optional[TreeNode]:\n if not preorder or not postorder:\n return\n \n root = TreeNode(preorder[0])\n if len(preorder) == 1:\n ... | 1 | Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times.
You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly ... | null |
Beats 95 / 98 % || Simple solution with HashMap + HashSet in Python3 / TypeScript | find-and-replace-pattern | 0 | 1 | # Intuition\nLet\'s briefly explain what the problem is:\n- there\'s a list of `words` and `pattern`\n- our goal is to find such words from `words`, that could be replaced by `pattern` and vice-versa\n\nWe\'d like to use **HashMap** to establish **links** between characters from a particular word to `pattern`.\n\n```\n... | 1 | Given a list of strings `words` and a string `pattern`, return _a list of_ `words[i]` _that match_ `pattern`. You may return the answer in **any order**.
A word matches the pattern if there exists a permutation of letters `p` so that after replacing every letter `x` in the pattern with `p(x)`, we get the desired word.... | null |
Beats 95 / 98 % || Simple solution with HashMap + HashSet in Python3 / TypeScript | find-and-replace-pattern | 0 | 1 | # Intuition\nLet\'s briefly explain what the problem is:\n- there\'s a list of `words` and `pattern`\n- our goal is to find such words from `words`, that could be replaced by `pattern` and vice-versa\n\nWe\'d like to use **HashMap** to establish **links** between characters from a particular word to `pattern`.\n\n```\n... | 1 | A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none).
You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`.
Return _the minimum number of flips to make_ `s` _monotone increa... | null |
Solution | find-and-replace-pattern | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<string> findAndReplacePattern(vector<string>& words, string pattern) {\n vector<string> ans;\n string yash;\n unordered_map<char,char> m;\n int cnt=0;\n for(int i=0;i<pattern.size();i++)\n {\n if(m.find(pattern[i])!=m.... | 1 | Given a list of strings `words` and a string `pattern`, return _a list of_ `words[i]` _that match_ `pattern`. You may return the answer in **any order**.
A word matches the pattern if there exists a permutation of letters `p` so that after replacing every letter `x` in the pattern with `p(x)`, we get the desired word.... | null |
Solution | find-and-replace-pattern | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<string> findAndReplacePattern(vector<string>& words, string pattern) {\n vector<string> ans;\n string yash;\n unordered_map<char,char> m;\n int cnt=0;\n for(int i=0;i<pattern.size();i++)\n {\n if(m.find(pattern[i])!=m.... | 1 | A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none).
You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`.
Return _the minimum number of flips to make_ `s` _monotone increa... | null |
Python | Easy Solution✅ | find-and-replace-pattern | 0 | 1 | ```\ndef findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:\n # words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb \n output = []\n new_pattern = self.create_new_pattern(pattern)\n for word in words:\n current_pattern = self.create_new_pattern(wor... | 4 | Given a list of strings `words` and a string `pattern`, return _a list of_ `words[i]` _that match_ `pattern`. You may return the answer in **any order**.
A word matches the pattern if there exists a permutation of letters `p` so that after replacing every letter `x` in the pattern with `p(x)`, we get the desired word.... | null |
Python | Easy Solution✅ | find-and-replace-pattern | 0 | 1 | ```\ndef findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:\n # words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb \n output = []\n new_pattern = self.create_new_pattern(pattern)\n for word in words:\n current_pattern = self.create_new_pattern(wor... | 4 | A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none).
You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`.
Return _the minimum number of flips to make_ `s` _monotone increa... | null |
Python3 Solution using Simple approach | find-and-replace-pattern | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given a list of strings `words` and a string `pattern`, return _a list of_ `words[i]` _that match_ `pattern`. You may return the answer in **any order**.
A word matches the pattern if there exists a permutation of letters `p` so that after replacing every letter `x` in the pattern with `p(x)`, we get the desired word.... | null |
Python3 Solution using Simple approach | find-and-replace-pattern | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none).
You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`.
Return _the minimum number of flips to make_ `s` _monotone increa... | null |
Python one-line solution | find-and-replace-pattern | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nList comprehension\n\n# Code\n```\nclass Solution:\n def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:\n return (word for word in words if len(set(word)) == len(set(pattern)) == len(set(zip(word, patt... | 1 | Given a list of strings `words` and a string `pattern`, return _a list of_ `words[i]` _that match_ `pattern`. You may return the answer in **any order**.
A word matches the pattern if there exists a permutation of letters `p` so that after replacing every letter `x` in the pattern with `p(x)`, we get the desired word.... | null |
Python one-line solution | find-and-replace-pattern | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nList comprehension\n\n# Code\n```\nclass Solution:\n def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:\n return (word for word in words if len(set(word)) == len(set(pattern)) == len(set(zip(word, patt... | 1 | A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none).
You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`.
Return _the minimum number of flips to make_ `s` _monotone increa... | null |
Solution | sum-of-subsequence-widths | 1 | 1 | ```C++ []\nconst long M=1e9+7;\nclass Solution {\npublic:\n int sumSubseqWidths(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n long sum=0, cnt=1;\n int n=nums.size();\n sort(nums.begin(), nums.end());\n for(int i=0, j=n-1; i<n; sum=(sum+(nums[i++]-... | 1 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
Solution | sum-of-subsequence-widths | 1 | 1 | ```C++ []\nconst long M=1e9+7;\nclass Solution {\npublic:\n int sumSubseqWidths(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n long sum=0, cnt=1;\n int n=nums.size();\n sort(nums.begin(), nums.end());\n for(int i=0, j=n-1; i<n; sum=(sum+(nums[i++]-... | 1 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
Easiest Solution | sum-of-subsequence-widths | 1 | 1 | \n\n# Code\n```java []\nclass Solution {\n long MOD=1000000007;\n public int sumSubseqWidths(int[] nums) {\n int i=0;\n int j=nums.length-1;\n long multi=1;\n long width=0;\n Arrays.sort(nums);\n while(i<nums.length&&j>=0){\n width=(width+nums[i]*multi-nums[j]*... | 0 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
Easiest Solution | sum-of-subsequence-widths | 1 | 1 | \n\n# Code\n```java []\nclass Solution {\n long MOD=1000000007;\n public int sumSubseqWidths(int[] nums) {\n int i=0;\n int j=nums.length-1;\n long multi=1;\n long width=0;\n Arrays.sort(nums);\n while(i<nums.length&&j>=0){\n width=(width+nums[i]*multi-nums[j]*... | 0 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
Python Solution, Less than 10 Lines, Faster than 95% | sum-of-subsequence-widths | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe input size is large so we find an approach that accumulates the answer at the position `i` with the information from the position `i-1`.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort `nums` and compute t... | 0 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
Python Solution, Less than 10 Lines, Faster than 95% | sum-of-subsequence-widths | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe input size is large so we find an approach that accumulates the answer at the position `i` with the information from the position `i-1`.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort `nums` and compute t... | 0 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
DP | sum-of-subsequence-widths | 0 | 1 | \n# Code\n```\nfrom typing import List\n\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n nums.sort()\n n = len(nums)\n mod = 10**9 + 7\n\n # Calculate powers of 2 modulo mod\n powers_of_2 = [1]\n for i in range(1, n):\n powers_of_2.append((... | 0 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
DP | sum-of-subsequence-widths | 0 | 1 | \n# Code\n```\nfrom typing import List\n\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n nums.sort()\n n = len(nums)\n mod = 10**9 + 7\n\n # Calculate powers of 2 modulo mod\n powers_of_2 = [1]\n for i in range(1, n):\n powers_of_2.append((... | 0 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
Easy solution in python | sum-of-subsequence-widths | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nPhase-1-> ans=0+(1-3)*1\n ans=-2%kmod\n exp=1*2%kmod\nPhase-2-> ans=(-2)+(2-2)*2\n ans=-2%kmod\n exp=2*2%kmod\nPhase-3-> ans=(-2)... | 0 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
Easy solution in python | sum-of-subsequence-widths | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nPhase-1-> ans=0+(1-3)*1\n ans=-2%kmod\n exp=1*2%kmod\nPhase-2-> ans=(-2)+(2-2)*2\n ans=-2%kmod\n exp=2*2%kmod\nPhase-3-> ans=(-2)... | 0 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
O(MAX) Time Complexity | Counting Sort | Python | sum-of-subsequence-widths | 0 | 1 | Since the range of numbers are small, we can sort number in ```O(n)``` time complexity with counting sort.\n\n# Code\n```\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n mod = int(1e9 + 7)\n mx = max(nums)\n cnt = [0] * (mx + 1)\n for num in nums:\n cnt[... | 0 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
O(MAX) Time Complexity | Counting Sort | Python | sum-of-subsequence-widths | 0 | 1 | Since the range of numbers are small, we can sort number in ```O(n)``` time complexity with counting sort.\n\n# Code\n```\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n mod = int(1e9 + 7)\n mx = max(nums)\n cnt = [0] * (mx + 1)\n for num in nums:\n cnt[... | 0 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
python3 Solution | sum-of-subsequence-widths | 0 | 1 | \n```\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n MOD = 10**9+7\n ans = 0 \n for i, x in enumerate(sorted(nums)): \n ans += x * (pow(2, i, MOD) - pow(2, len(nums)-i-1, MOD))\n return ans % MOD\n``` | 0 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
python3 Solution | sum-of-subsequence-widths | 0 | 1 | \n```\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n MOD = 10**9+7\n ans = 0 \n for i, x in enumerate(sorted(nums)): \n ans += x * (pow(2, i, MOD) - pow(2, len(nums)-i-1, MOD))\n return ans % MOD\n``` | 0 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
MATH + DP | sum-of-subsequence-widths | 0 | 1 | # Code\n```\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n MOD = 10**9 + 7\n n = len(nums)\n nums.sort()\n\n dp = [0] * n\n\n p = 2\n temp = nums[0]\n\n for i in range(1, n):\n dp[i] = ((dp[i-1] + ((p-1)*nums[i])%MOD)%MOD - temp)%MO... | 0 | The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers `nums`, return _the sum of the **widths** of all the non-empty **subsequences** of_ `nums`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** is a sequ... | null |
MATH + DP | sum-of-subsequence-widths | 0 | 1 | # Code\n```\nclass Solution:\n def sumSubseqWidths(self, nums: List[int]) -> int:\n MOD = 10**9 + 7\n n = len(nums)\n nums.sort()\n\n dp = [0] * n\n\n p = 2\n temp = nums[0]\n\n for i in range(1, n):\n dp[i] = ((dp[i-1] + ((p-1)*nums[i])%MOD)%MOD - temp)%MO... | 0 | You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
* `arr[0], arr[1], ..., arr[i]` is the first part,
* `arr[i + 1], ar... | null |
Simple Python explained | surface-area-of-3d-shapes | 0 | 1 | ```\nclass Solution:\n def surfaceArea(self, grid: List[List[int]]) -> int:\n \n l = len(grid)\n area=0\n for row in range(l):\n for col in range(l):\n if grid[row][col]:\n area += (grid[row][col]*4) +2 #surface area of each block if blocks wer... | 6 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
Simple Python explained | surface-area-of-3d-shapes | 0 | 1 | ```\nclass Solution:\n def surfaceArea(self, grid: List[List[int]]) -> int:\n \n l = len(grid)\n area=0\n for row in range(l):\n for col in range(l):\n if grid[row][col]:\n area += (grid[row][col]*4) +2 #surface area of each block if blocks wer... | 6 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Beatiful Python Solution | surface-area-of-3d-shapes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity : O(n^2) \n- => n is the length or grid[i].length\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : O(1)... | 1 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
Beatiful Python Solution | surface-area-of-3d-shapes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity : O(n^2) \n- => n is the length or grid[i].length\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : O(1)... | 1 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Solution | surface-area-of-3d-shapes | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int surfaceArea(vector<vector<int>>& grid) {\n int area = 0, n = grid.size();\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n int v = grid[i][j];\n if (v) {\n area += 2;\n ... | 1 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
Solution | surface-area-of-3d-shapes | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int surfaceArea(vector<vector<int>>& grid) {\n int area = 0, n = grid.size();\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n int v = grid[i][j];\n if (v) {\n area += 2;\n ... | 1 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Beats 94% in space! - Python | surface-area-of-3d-shapes | 0 | 1 | # Code\n```\nclass Solution:\n def surfaceArea(self, grid: List[List[int]]) -> int:\n A, s = 0, 0\n for i in range(len(grid)):\n for j in range(len(grid[0])):\n if grid[i][j] > 0:\n s += 1\n B = 5 * grid[i][j] - (grid[i][j] - 1)\n A += B\n if not ... | 0 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
Beats 94% in space! - Python | surface-area-of-3d-shapes | 0 | 1 | # Code\n```\nclass Solution:\n def surfaceArea(self, grid: List[List[int]]) -> int:\n A, s = 0, 0\n for i in range(len(grid)):\n for j in range(len(grid[0])):\n if grid[i][j] > 0:\n s += 1\n B = 5 * grid[i][j] - (grid[i][j] - 1)\n A += B\n if not ... | 0 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Simple python code with explanation | surface-area-of-3d-shapes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ncalculating surface area for each cell and adding them up.\n# Complexity\n- Time complexity:O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n... | 0 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
Simple python code with explanation | surface-area-of-3d-shapes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ncalculating surface area for each cell and adding them up.\n# Complexity\n- Time complexity:O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n... | 0 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
[python3] works 👍🏿 | surface-area-of-3d-shapes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
[python3] works 👍🏿 | surface-area-of-3d-shapes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Easy Python Solution | surface-area-of-3d-shapes | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nStep 1 : Find the area of each piller on the matrix(First Loop)\nStep 2 : Remove all the walls of two adjence pillers(Second Loop)\nStep 3 : Get the transpose of the matrix\nStep 4 : Repet Step 2. \n# Complexity\n- Time complexity: O(n^2)\n<!-- Add yo... | 0 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
Easy Python Solution | surface-area-of-3d-shapes | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nStep 1 : Find the area of each piller on the matrix(First Loop)\nStep 2 : Remove all the walls of two adjence pillers(Second Loop)\nStep 3 : Get the transpose of the matrix\nStep 4 : Repet Step 2. \n# Complexity\n- Time complexity: O(n^2)\n<!-- Add yo... | 0 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Python solution | surface-area-of-3d-shapes | 0 | 1 | # Code\n```\nclass Solution:\n def surfaceArea(self, grid: List[List[int]]) -> int:\n n = len(grid)\n neighbors = ((-1, 0), (0, -1), (0, 1), (1, 0))\n res = 0\n for r in range(n):\n for c in range(n):\n for n_r, n_c in neighbors:\n if (0 <= r +... | 0 | You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return _the tota... | null |
Python solution | surface-area-of-3d-shapes | 0 | 1 | # Code\n```\nclass Solution:\n def surfaceArea(self, grid: List[List[int]]) -> int:\n n = len(grid)\n neighbors = ((-1, 0), (0, -1), (0, 1), (1, 0))\n res = 0\n for r in range(n):\n for c in range(n):\n for n_r, n_c in neighbors:\n if (0 <= r +... | 0 | You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the `ith` node is directly connected to the `jth` node if `graph[i][j] == 1`.
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected... | null |
Python 3 || 5 lines, w/ example || T/M: 88%/27% | groups-of-special-equivalent-strings | 0 | 1 | \n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n # Example: \n wSet = set() # words = ["abc","acb","bac","bca","cab","cba"]\n\n for word in words: ... | 3 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Python 3 || 5 lines, w/ example || T/M: 88%/27% | groups-of-special-equivalent-strings | 0 | 1 | \n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n # Example: \n wSet = set() # words = ["abc","acb","bac","bca","cab","cba"]\n\n for word in words: ... | 3 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Solution | groups-of-special-equivalent-strings | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int numSpecialEquivGroups(vector<string>& words) {\n unordered_set<string> st;\n for (auto& w : words) {\n string odd, even;\n for (int i = 0; i < w.size(); i++) {\n if (i % 2) even += w[i];\n else odd += w[i];\n... | 1 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Solution | groups-of-special-equivalent-strings | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int numSpecialEquivGroups(vector<string>& words) {\n unordered_set<string> st;\n for (auto& w : words) {\n string odd, even;\n for (int i = 0; i < w.size(); i++) {\n if (i % 2) even += w[i];\n else odd += w[i];\n... | 1 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Pyton3 easy to understand solution | groups-of-special-equivalent-strings | 0 | 1 | # Approach\nSort the odd indices and even indices of the word seperately. Then combine those 2 to get the key value which is the same for special equivalent strings.\n\n\n\n# Code\n```\nfrom collections import defaultdict\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n d = defau... | 1 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Pyton3 easy to understand solution | groups-of-special-equivalent-strings | 0 | 1 | # Approach\nSort the odd indices and even indices of the word seperately. Then combine those 2 to get the key value which is the same for special equivalent strings.\n\n\n\n# Code\n```\nfrom collections import defaultdict\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n d = defau... | 1 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
[python3] detail explanation of special-equivalent | groups-of-special-equivalent-strings | 0 | 1 | * The point here is to understand the following requirement:\nA move consists of choosing two indices i and j with i % 2 == j % 2, and swapping S[i] with S[j].\nDon\'t trap your thoughts by the word \'swap\'.\nYour goal is how to identify the equivalent strings. \n* There are two possible outcomes of i%2: 1 or 0. i is ... | 39 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
[python3] detail explanation of special-equivalent | groups-of-special-equivalent-strings | 0 | 1 | * The point here is to understand the following requirement:\nA move consists of choosing two indices i and j with i % 2 == j % 2, and swapping S[i] with S[j].\nDon\'t trap your thoughts by the word \'swap\'.\nYour goal is how to identify the equivalent strings. \n* There are two possible outcomes of i%2: 1 or 0. i is ... | 39 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Python O(n * k lg k) sol. by signature. 90%+ [w/ Hint] | groups-of-special-equivalent-strings | 0 | 1 | Python sol. by signature. \n\n---\n\n**Hint**:\n\nThink of the concept for **anagram** judgement.\n\nWhat we need in this challenge is to verify the so-called "**special equivalent**", \nwhich means even-indexed substring of input is anagram and odd-indexed substring of input is another anagram.\n\nWe can verify by si... | 8 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Python O(n * k lg k) sol. by signature. 90%+ [w/ Hint] | groups-of-special-equivalent-strings | 0 | 1 | Python sol. by signature. \n\n---\n\n**Hint**:\n\nThink of the concept for **anagram** judgement.\n\nWhat we need in this challenge is to verify the so-called "**special equivalent**", \nwhich means even-indexed substring of input is anagram and odd-indexed substring of input is another anagram.\n\nWe can verify by si... | 8 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Python3 Simple O(N^2 logN) solution with sorting (98.98% Runtime) | groups-of-special-equivalent-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nUtilize hash table to store group (unique key) & use sorting to generate unique key\n\n# Approach\n<!-- Describe your appr... | 0 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Python3 Simple O(N^2 logN) solution with sorting (98.98% Runtime) | groups-of-special-equivalent-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nUtilize hash table to store group (unique key) & use sorting to generate unique key\n\n# Approach\n<!-- Describe your appr... | 0 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Easy to understand using dictionary(HashMap) with Explanation|| Beats 85% | groups-of-special-equivalent-strings | 0 | 1 | \n\n\n# Code\n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n d = defaultdict(list)\n for i in words:\n m,v = \'\',\'\'\n for j in ra... | 0 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Easy to understand using dictionary(HashMap) with Explanation|| Beats 85% | groups-of-special-equivalent-strings | 0 | 1 | \n\n\n# Code\n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n d = defaultdict(list)\n for i in words:\n m,v = \'\',\'\'\n for j in ra... | 0 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Easy to understand Python3 solution | groups-of-special-equivalent-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Easy to understand Python3 solution | groups-of-special-equivalent-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Python3 - Convert characters at odd and even indices into a hash, count unique hashes | groups-of-special-equivalent-strings | 0 | 1 | # Code\n```\nclass Solution:\n # Approach: Form a dictionary (h1, h2) -> [s1,s2...sn]\n # where h1 and h2 are two hashes - sorted characters\n # at the odd and even indices respectively in the word.\n # So (h1,h2) will be the same for all special-equivalent strings.\n # Then count the number of unique h... | 0 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Python3 - Convert characters at odd and even indices into a hash, count unique hashes | groups-of-special-equivalent-strings | 0 | 1 | # Code\n```\nclass Solution:\n # Approach: Form a dictionary (h1, h2) -> [s1,s2...sn]\n # where h1 and h2 are two hashes - sorted characters\n # at the odd and even indices respectively in the word.\n # So (h1,h2) will be the same for all special-equivalent strings.\n # Then count the number of unique h... | 0 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
BEST & EASIEST SOLUTION ON WEB ROGHT NOW ||| I AM A GENIUS 〠 | groups-of-special-equivalent-strings | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
BEST & EASIEST SOLUTION ON WEB ROGHT NOW ||| I AM A GENIUS 〠 | groups-of-special-equivalent-strings | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
hashmap python solution | groups-of-special-equivalent-strings | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n gloss = {}\n answer = 0\n for w in words:\n ev = []\n od = []\n for i, ch in enumerate(w):\n if i%2==0:\n ev.append(ch)\n ... | 0 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
hashmap python solution | groups-of-special-equivalent-strings | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n gloss = {}\n answer = 0\n for w in words:\n ev = []\n od = []\n for i, ch in enumerate(w):\n if i%2==0:\n ev.append(ch)\n ... | 0 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Python | Simple | set | groups-of-special-equivalent-strings | 0 | 1 | \n# Code\n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n res = set()\n for w in words: res.add(\'\'.join(sorted(w[::2]) + sorted(w[1::2])))\n return len(res)\n\n``` | 0 | You are given an array of strings of the same length `words`.
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
* For example,... | null |
Python | Simple | set | groups-of-special-equivalent-strings | 0 | 1 | \n# Code\n```\nclass Solution:\n def numSpecialEquivGroups(self, words: List[str]) -> int:\n res = set()\n for w in words: res.add(\'\'.join(sorted(w[::2]) + sorted(w[1::2])))\n return len(res)\n\n``` | 0 | Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
* For example, in `"alice@leetcode.com "`, `"alice "` is the **local name**, and `"leetcode.com "` is the **domain name**.
If you add pe... | null |
Unorthodox Combinatorial Search + Backtracking Approach (Python 3) | all-possible-full-binary-trees | 0 | 1 | # Intuition\n\nDP solutions are much faster, but the first solution that came to my head is a recursive combinatorial search + backtracking, similar to combinations problem.\n\nJust wanted to include this post as an alternate method of solving the problem albeit with much slower time since it doesn\'t leverage memoizat... | 1 | Given an integer `n`, return _a list of all possible **full binary trees** with_ `n` _nodes_. Each node of each tree in the answer must have `Node.val == 0`.
Each element of the answer is the root node of one possible tree. You may return the final list of trees in **any order**.
A **full binary tree** is a binary tr... | null |
Unorthodox Combinatorial Search + Backtracking Approach (Python 3) | all-possible-full-binary-trees | 0 | 1 | # Intuition\n\nDP solutions are much faster, but the first solution that came to my head is a recursive combinatorial search + backtracking, similar to combinations problem.\n\nJust wanted to include this post as an alternate method of solving the problem albeit with much slower time since it doesn\'t leverage memoizat... | 1 | Given a binary array `nums` and an integer `goal`, return _the number of non-empty **subarrays** with a sum_ `goal`.
A **subarray** is a contiguous part of the array.
**Example 1:**
**Input:** nums = \[1,0,1,0,1\], goal = 2
**Output:** 4
**Explanation:** The 4 subarrays are bolded and underlined below:
\[**1,0,1**,0... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.