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 |
|---|---|---|---|---|---|---|---|
[Java/C++/Python/JavaScript/Kotlin/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022 | multiply-strings | 1 | 1 | ***Hello it would be my pleasure to introduce myself Darian.***\n\n***Java***\n```\npublic String multiply(String num1, String num2) {\n int m = num1.length(), n = num2.length();\n int[] pos = new int[m + n];\n \n for(int i = m - 1; i >= 0; i--) {\n for(int j = n - 1; j >= 0; j--) {\n int m... | 4 | Given two non-negative integers `num1` and `num2` represented as strings, return the product of `num1` and `num2`, also represented as a string.
**Note:** You must not use any built-in BigInteger library or convert the inputs to integer directly.
**Example 1:**
**Input:** num1 = "2", num2 = "3"
**Output:** "6"
**Ex... | null |
Bijil Boby | wildcard-matching | 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 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Easy To understand | wildcard-matching | 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)$$ --... | 3 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Python3 || DP space optimized | wildcard-matching | 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$$O(mn)$$ where m, n are the length of s and p, respectively\n\n- Space comp... | 1 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
✨💯💥Linear Time Solution | Detailed Explanation | Images | Visualisations💥💯✨ | wildcard-matching | 0 | 1 | # Intuition\nI solved this problem using dynamic programming .Its time complexity was $$O(len(s)*len(p))$$. However this post doesn\'t explain that solution. I am sure there are many more posts to explain that.\n\nThereafter I found a solution in linear time using 2 pointer approach. I have made some visualisations usi... | 5 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
BEATS 95% OF THE USERS IN PYTHON3 O(n^2) DYNAMIC PROGRAMMING SOLUTION | wildcard-matching | 0 | 1 | # Intuition\n\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 yo... | 2 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Python || faster than 90% || Explained with comments | wildcard-matching | 0 | 1 | # Complexity\n- Time complexity:\nBetter than 90%\n\n- Space complexity:\nBetter than 90%\n\n# Code\n```\nclass Solution:\n def isMatch(self, s: str, p: str) -> bool:\n # Initialize the pointers for the input string and the pattern\n i = 0\n j = 0\n \n # Initialize the pointers for the... | 18 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Wildcard Matching with step by step explanation | wildcard-matching | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a 2D boolean array dp with m + 1 rows and n + 1 columns, where m is the length of string s and n is the length of string p.\n2. Set dp[0][0] to True, which means an empty string matches an empty pattern.\n3. Fo... | 4 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Simple Backtracking with DP | wildcard-matching | 0 | 1 | # Intuition\n- The problem can be classified as Backtracking but to optimize it, we can add memoization to eliminate repeat recursions when computing \'*\'s.\n\n# Approach\n - Backtracking with Dynamic Programming (memoization)\n\n# Code\n```\nclass Solution:\n\n def isMatch(self, s: str, p: str) -> bool:\n ... | 2 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Python Bottom up dp solution | wildcard-matching | 0 | 1 | \n# Code\n```\n def isMatch(self, s: str, p: str) -> bool:\n n = len(s);\n m = len(p);\n dp = [[0]*(m+1) for _ in range(0,n+1)]\n\n dp[0][0] = 1\n for j in range(1,m+1):\n if(p[j-1] == \'*\' ): dp[0][j] = dp[0][j-1];\n\n for i in range(1,n+1):\n for j i... | 5 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Python Solution Recursion Memo , tabulation, Space Optimization | wildcard-matching | 0 | 1 | \n# Code\n```\nclass Solution:\n def isallstar(self,p,i) -> bool:\n for ii in range (1,i+1):\n if (p[ii-1]!="*"):\n return False\n return True\n\n def memo(self,p,s,i,j,dp):\n if (i<0 and j<0):\n return True\n if (i<0 and j>=0):\n return ... | 2 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
💡💡 Neatly coded recursion with memoization in python3 | wildcard-matching | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimilar to LCS, difference in the conditions. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nConditions:\n1. If the characters match or the char in pattern is "?": we decremet both i and j by 1\n2. If the char in... | 1 | Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
* `'?'` Matches any single character.
* `'*'` Matches any sequence of characters (including the empty sequence).
The matching should cover the **entire** input string (not partial).
**Exam... | null |
Clean Codes🔥🔥|| Full Explanation✅|| Implicit BFS✅|| C++|| Java|| Python3 | jump-game-ii | 1 | 1 | # Intuition :\n- We have to find the minimum number of jumps required to reach the end of a given array of non-negative integers i.e the shortest number of jumps needed to reach the end of an array of numbers.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Explanation to Approach :\n- We are ... | 194 | You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
* `0 <= j <= nums[i]` and
* `i +... | null |
Jiraiya's Barrier technique || Linear solution || intuitive explanation | jump-game-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is about finding the minimum number of jumps to reach the end of the array. We can think of this problem as Jiraiya(A popular character from Naruto Universe) trying to detect an enemy at the n-1th position using his barrier ar... | 8 | You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
* `0 <= j <= nums[i]` and
* `i +... | null |
🚀Super Easy Solution🚀||🔥Fully Explained🔥|| C++ || Python || Commented | jump-game-ii | 0 | 1 | # Consider\uD83D\uDC4D\n```\n Please Upvote If You Find It Helpful\n```\n\n# Intuition\nIn this question we have to find the minimum number of jumps to reach the last index.\nSo, we calculate the maximum index we can reach from the current index.\nIf our pointer `i` reaches the last index that can be... | 66 | You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
* `0 <= j <= nums[i]` and
* `i +... | null |
Python DP + Memo solution | jump-game-ii | 0 | 1 | # Approach\nCreate an array containing minimum steps you need to take to get to the i-th position.\nWe can simply go through all elements of the array and then iterate over all possible jump lengths updating information in our array.\nWe can either jump from our current position, or some other position that we consider... | 5 | You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
* `0 <= j <= nums[i]` and
* `i +... | null |
Python | jump-game-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nGreedy Approach\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def jump(self, a: List[int]) -> int:\n jump... | 1 | You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
* `0 <= j <= nums[i]` and
* `i +... | null |
Easy & Clear Solution Python3 | jump-game-ii | 0 | 1 | \n# Code\n```\nclass Solution:\n def jump(self, nums: List[int]) -> int:\n n=len(nums)\n dp=[-1 for _ in range(n-1)]\n dp+=[0]\n for i in range(n-2,-1,-1):\n for j in range(i+1,min(n,i+nums[i]+1)):\n if dp[j]!=-1:\n if dp[i]==-1:\n ... | 6 | You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
* `0 <= j <= nums[i]` and
* `i +... | null |
PYTHON | BEATS 99.99% | EASY-SOLUTION | jump-game-ii | 0 | 1 | ```\nclass Solution:\n def jump(self, nums: List[int]) -> int:\n\n x = [nums[i]+i for i in range(len(nums))] \n # each element in this represent max index that can be reached from the current index \n\n l,r,jumps = 0,0,0\n\n while r < len(nums)-1 :\n jumps += 1\n l,... | 8 | You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
* `0 <= j <= nums[i]` and
* `i +... | null |
🔐 [VIDEO] 100% Unlocking Recursive & Backtracking for Permutations | permutations | 1 | 1 | # Intuition\nWhen I first looked at this problem, I realized it was a classic case of generating all possible permutations of a given list of numbers. My initial thought was to use a recursive approach to solve it. Recursive algorithms, especially backtracking, often provide a clean and efficient solution for generatin... | 51 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Python Solution Using Backtracking | permutations | 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 array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
One Liner.Built In Function | permutations | 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 array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
ONE LINER 96% BEATS | permutations | 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:12MS\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:14 MB\n<!-- Add your space complexity here, e.g. $$... | 2 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Permutation.py | permutations | 0 | 1 | # Code\n```\nclass Solution:\n def permute(self, nums: List[int]) -> List[List[int]]:\n temp,ans=[],[]\n def param(i,nums):\n if i==len(nums):\n ans.append(nums.copy())\n return\n for j in range(i,len(nums)):\n nums[i],nums[j]=nums[j],n... | 4 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
✔️C++||Python||C🔥0ms✅Backtrack with graph explained || Beginner-friendly ^_^ | permutations | 0 | 1 | # Approach\nUse a recursive function `backtrack`, in the function, we pass in a index.\nIn the function, we use a for loop swap `nums[index]` with different numbers in `nums`.\nAfter each swap, call `backtrack` again with `index+1`.\nThen swap back the values, so that we can get every permutations as the graph shown be... | 23 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
DFS/backtracking - Python/Java/Javascript, PICTURE | permutations | 1 | 1 | Classic combinatorial search problem, we can solve it using 3-step system \n\n1. Identify states\nWhat state do we need to know whether we have reached a solution (and using it to construct a solution if the problem asks for it).\nWe need a state to keep track of the list of letters we have chosen for the current permu... | 244 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
🔥 100% Backtracking / Recursive Approach - Unlocking Permutations | permutations | 1 | 1 | # Intuition\nThe problem requires generating all possible permutations of a given array of distinct integers. A clear and intuitive understanding of the recursive approach can be achieved by watching vanAmsen\'s video explanation, where the permutations are constructed by choosing one element at a time and recursively ... | 10 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Ex-Amazon explains a solution with a video, Python, JavaScript, Java and C++ | permutations | 1 | 1 | # Intuition\nUsing backtracking to create all possible combinations\n\n---\n\n# Solution Video\n## *** Please upvote for this article. *** \n\nhttps://youtu.be/J7m7W7P6e3s\n\n# Subscribe to my channel from here. I have 238 videos as of August 2nd\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation... | 18 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Python short and clean. Generic solution. Functional programming. | permutations | 0 | 1 | # Approach\n\n# Complexity\n- Time complexity: $$O(n!)$$\n\n- Space complexity: $$O(n!)$$\n\n# Code\n```python\nclass Solution:\n def permute(self, nums: List[int]) -> List[List[int]]:\n T = TypeVar(\'T\')\n def permutations(pool: Iterable[T], r: int = None) -> Iterator[Iterator[T]]:\n pool ... | 1 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
ONE LINER 97% Beats | permutations | 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:47ms\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:16mb\n<!-- Add your space complexity here, e.g. $$O... | 1 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Python easy solutions || time & memory efficient | permutations | 0 | 1 | # Intuition\nThe problem requires generating all possible permutations of a given array of distinct integers. One intuitive approach to achieve this is by using the itertools.permutations function, which provides a straightforward way to generate all permutations of a sequence.\n\n# Approach\nThe solution makes use of ... | 2 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Very simple[C,C++,Java,python3 codes] Easy to understand Approach. | permutations | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to generate all possible permutations of a given array of distinct integers. A permutation is an arrangement of elements in a specific order. For example, given the array [1, 2, 3], the permutations are [[1, 2, 3], [1, 3, 2... | 11 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Short straight forward solution in Python | permutations | 0 | 1 | # Intuition\n\n\n\n\n# Approach\nDirectly implement as is shown on the image using recursion \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O... | 2 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Python3 | Easy to understand + simple recursive approach | Beats 85% | permutations | 0 | 1 | # Approach\nRecursive call tries to add a number that was not used earlier (not in the list). If the list gains the right length it goes to the result. The process repeats with each num as a first element.\n# Code\n```re\nclass Solution:\n def permute(self, nums: List[int]) -> List[List[int]]:\n res = []\n ... | 6 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Layman's Code in 🐍 | One Liner Hack | 🔥Beats 99.9%🔥 | permutations | 0 | 1 | ---\n\n\n---\n\n# Complexity\n- Time complexity: $$O(n!)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n!)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n--... | 1 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
🔥PYTHON🔥 Seriously 1 Liner 😱😱 | permutations | 0 | 1 | \n# Code\n```\nclass Solution:\n def permute(self, nums: List[int]) -> List[List[int]]:\n return list(itertools.permutations(nums,len(nums)))\n``` | 7 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
🚀 [VIDEO] Backtracking 100% - Unlocking Permutations | permutations | 0 | 1 | # Intuition\nUpon encountering this problem, I was immediately struck by its resemblance to a well-known puzzle: generating all possible permutations of a given sequence. It\'s like holding a Rubik\'s Cube of numbers and twisting it to discover every conceivable arrangement. With the challenge laid out, my mind gravita... | 9 | Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,1\],\[3,1,2\],\[3,2,1\]\]
**Example 2:**
**Input:** nums = \[0,1\]
**Output:** \[\[0,1\],\[1,0\... | null |
Python || Recursive Code Slight Modification || SubSequence Method 🔥 | permutations-ii | 0 | 1 | # Code\n```\nclass Solution:\n def permuteUnique(self, nums: List[int]) -> List[List[int]]:\n ans = []\n nums.sort()\n def subset(p, up):\n if len(up) == 0:\n if p not in ans:\n ans.append(p)\n return \n ch = up[0]\n ... | 1 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
✅ Python Simple Backtrack - Beats ~90% | permutations-ii | 0 | 1 | The problem is solved using a backtracking approach. For this particular case, as we have duplicates in input, we can track the count of each number. Python provides a built-in lib `Counter` which I will be using for this problem. As the order of output results doesn\'t matter, we can use this `Counter` variable to t... | 44 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Beats 99.70% Permutations II | permutations-ii | 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 a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Python | 80% Faster Code | Backtracking | permutations-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n**Backtracking**\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... | 1 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
A simple code with built-in functions. Life is beautiful don't make it complex by backtracking. | permutations-ii | 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 a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Backtracking / Recursion - Beat 99.61% in runtime. | permutations-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn each recursion, only fill a position in a new list. Assume the exsit *n* non-repeated elements in each recursion, so the position has *n* candidates.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the lis... | 2 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Backtracking concept | permutations-ii | 0 | 1 | # Backtracking concept\n```\nclass Solution:\n def permute(self, nums: List[int]) -> List[List[int]]:\n def back(nums,ans,temp):\n if len(nums)==0:\n ans.append(temp)\n return \n for i in range(len(nums)):\n back(nums[:i]+nums[i+1:],ans,temp+[... | 3 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Cheat one-liner in Python using in-built permutations method | permutations-ii | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def permuteUnique(self, nums: List[int]) -> List[List[int]]:\n return set(permutations(nums)) \n``` | 5 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Recursive,Intutive ,using Set to optimize search | permutations-ii | 0 | 1 | \n\n# Approach\n<!--ma -->\nmaintaining a set of un-visited indexes of *nums* for every permutation along with a dictionary to check whether the value has already been used at the position ,the variable "*lv*" is indicating the postion till which numbers have been filled \n# Complexity\n- Time complexity:\n<!-- Add you... | 1 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
ONE LINER 97.93% Beats | permutations-ii | 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:12ms\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:11mb\n<!-- Add your space complexity here, e.g. $$O... | 3 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Easy and Clear Solution Python 3 | permutations-ii | 0 | 1 | ```\nclass Solution:\n def permuteUnique(self, nums: List[int]) -> List[List[int]]:\n res=set()\n def per(nm: List[int],x: List[int]) -> None:\n if nm ==[]:\n aa=""\n for i in x:\n aa+=str(i)+"*"\n res.add(aa)\n else:... | 3 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
BEATS 90% SUBMISSIONS || EASIEST || BACKTRACKING || HASHMAP | permutations-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRECURSIVE APPROACH USING A HASHMAP.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBACKTRACKING AFTER APPENDING THE ELEMENTS TO THE LIST WHILE USING A HASHMAP WHICH STORES THE COUNT OF THE ELEMENTS PRESENT IN THE ... | 1 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Python3 || Beats 94.85% ||simple beginner solution. | permutations-ii | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def permuteUnique(self, nums: List[int]) -> List[List[int]]:\n per = permutations(nums)\n l = []\n for i in per:\n if i not in l:\n ... | 2 | Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
**Example 1:**
**Input:** nums = \[1,1,2\]
**Output:**
\[\[1,1,2\],
\[1,2,1\],
\[2,1,1\]\]
**Example 2:**
**Input:** nums = \[1,2,3\]
**Output:** \[\[1,2,3\],\[1,3,2\],\[2,1,3\],\[2,3,... | null |
Rotating 2-D visualization and Python solution | rotate-image | 0 | 1 | # Intuition\nThink like concentric circles and moving around in the direction from outwards to inwards. You can use it vice versa also i.e. from inwards to outwards. So, the total number of concentric cicles would be total_rows/2. \n##### Q. Why total number of concentric circles would be total_rows/2?\n##### A. Becuas... | 1 | You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotati... | null |
Easy Python from scratch (2 Steps) | rotate-image | 0 | 1 | \t# reverse\n\tl = 0\n\tr = len(matrix) -1\n\twhile l < r:\n\t\tmatrix[l], matrix[r] = matrix[r], matrix[l]\n\t\tl += 1\n\t\tr -= 1\n\t# transpose \n\tfor i in range(len(matrix)):\n\t\tfor j in range(i):\n\t\t\tmatrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]\n\t\t\t\nWe want to rotate\n[1,2,3],\n[4,5,6],\n[7,8,... | 310 | You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotati... | null |
Easiest python solution with step-by-step explanation beats 61% python solution👨🏾💻💥🚀 | rotate-image | 0 | 1 | # Intuition\n`matrix = [[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]]`\n\n```\n[1] [2] [3]\n[4] [5] [6]\n[7] [8] [9]\n```\n\nIf we take the each column and reverse it and put it horizontally , we get:\n\n```\n[7] [4] [1]\n[8] [5] [2]\n[9] [6] [3]\n```\n# Approach\n\n---\n\n\n\n**Step 1:** Copy the ... | 2 | You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotati... | null |
Simplest Solution in 2 steps | rotate-image | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nFor Rotating matrix 90 degree clockwise we need to do this simple steps:\n1. Transpose of Matrix\n2. Reverse each rows\n\nThat\'s all now our matrix will be rotated by 90 degree clockwise.\n\n# Complexity\n- Time complexity: O(m * n)\n<!-- Add your ti... | 3 | You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotati... | null |
2 Simple Solutions | 10ms | 🚀🚀Accepted🔥 | rotate-image | 0 | 1 | # Code\n```\nclass Solution:\n def rotate(self, x: List[List[int]]) -> None:\n """\n Do not return anything, modify matrix in-place instead.\n """\n for i in range(len(x)):\n for j in range(i,len(x)):\n x[i][j],x[j][i]=x[j][i],x[i][j]\n for i in range(len(... | 3 | You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotati... | null |
✅Beats 100% || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥 | group-anagrams | 1 | 1 | \n# Intuition:\n\nThe intuition is to group words that are anagrams of each other together. Anagrams are words that have the `same` characters but in a `different` order.\n\n# Explanation:\n\nLet\'s go through the code step by step using the example input `["eat","tea","tan","ate","nat","bat"]` to understand how it wor... | 822 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
🔥Python || Easily Understood ✅ || Hash Table || Fast || Simple | group-anagrams | 0 | 1 | **Appreciate if you could upvote this solution**\n\n\nMethod: `Hash Table`\n\nSince the output needs to group the anagrams, it is suitable to use `dict` to store the different anagrams.\nThus, we need to find a common `key` for those anagrams.\nAnd one of the best choices is the `sorted string` as all the anagrams have... | 163 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
An odd solution using a Counter | group-anagrams | 0 | 1 | # Intuition\nMy first thought was that all anagrams have identical counts of letters, so immediately thought of using a Counter().\n\n# Approach\nI keep a dict of Counters and check if the counter is already a key. The problem I ran into is that the Counter class is not a hashable object. So I created a hashable versio... | 0 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
using Hashtable Easy to understand | group-anagrams | 0 | 1 | \n\n# Python Solution\n```\nclass Solution:\n def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n dic={}\n for word in strs:\n sorted_word="".join(sorted(word))\n if sorted_word not in dic:\n dic[sorted_word]=[word]\n else:\n dic... | 38 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Easy Python Solution 👨💻 || Briefly Explained ✅✅ | group-anagrams | 0 | 1 | # Code Explanation\nThe provided Python code defines a class `Solution` with a method `groupAnagrams`. This method takes a list of strings `strs` as input and is supposed to group the anagrams from the input list into lists of lists, where each inner list contains words that are anagrams of each other. Anagrams are wor... | 13 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Solution | group-anagrams | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<vector<string>> groupAnagrams(vector<string>& strs) {\n unordered_map<string,vector<string>>mp;\n vector<vector<string>>ans;\n for(int i=0;i<strs.size();i++){\n string t=strs[i];\n sort(t.begin(),t.end());\n ... | 6 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
🔥🔥Python3 Beats 96% With explanation🔥🔥 | group-anagrams | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n$$Solution$$ $$above$$ $$code.$$\n\n\n\n\n# Code\n```\nclass Solution:\n def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n #create a hashmap, itterate through strs and create a new variable\n #for each word ... | 4 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Simple and clever solution for Python3 | group-anagrams | 0 | 1 | # Intuition\nTo solve the problem of grouping anagrams, one common method is to represent each word by a unique signature. Since anagrams have the same letters, just rearranged, their signatures will be the same. One way to create such a signature is by counting the frequency of each letter in the word.\n\n# Approach\n... | 5 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Python O(N) Solution | group-anagrams | 0 | 1 | # Intuition & Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nSorted anagrams string will always be equal.\nHence, create a hash_map where key will the sorted string.\nAdd current string if its key is present, other wise create a new entry.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\nA... | 6 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
95.28% Beats Python || Beginner Friendly || Code Explained | group-anagrams | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- We will use a hashmap to associate anagrams (words formed by using same letters).\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a hashmap/dictionary `d`.\n\n2. Iterate through the given list `strs`.... | 3 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Python || Beats 99.38% | group-anagrams | 0 | 1 | \n# Code\n```\nclass Solution:\n def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n answer = defaultdict(list)\n for word in strs:\n answer["".join(sorted(word))].append(word)\n return list(answer.values())\n\n\n``` | 6 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
100 % Animation to the Hard Solution and Easy Solution Explained | group-anagrams | 1 | 1 | [https://youtu.be/3cAxlUsBfas]()\n | 30 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Python 3 solution; detailed explanation; faster than 97.5% | group-anagrams | 0 | 1 | ```\nclass Solution:\n def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n h = {}\n for word in strs:\n sortedWord = \'\'.join(sorted(word))\n if sortedWord not in h:\n h[sortedWord] = [word]\n else:\n h[sortedWord].append(word)\... | 124 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Dictionary | Sorting | Python easy solution with explanation | group-anagrams | 0 | 1 | # Intuition\n**Find a way to associate similar words together.** \nWe can utilize the **wo**rd **co**unt **ap**proach but I preferred the approach where you **so**rt the **wo**rd. This allows **an**agrams to be **so**rted and we can then **ma**tch the **wo**rds. \nFor example, \n\n`\'tan\' and \'nat\' when sorted would... | 19 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Prime | group-anagrams | 0 | 1 | Use prime to make a unique key for each anagram \n\n# Code\n```\nclass Solution:\n def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n anagrams: dict[int, int] = dict()\n i = 0\n # first 26 prime numbers\n primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,\n 31, 37,... | 1 | Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Example 1:**
**Input:** strs = \["eat","tea","tan... | null |
Python Simple Recursive | powx-n | 0 | 1 | # Intuition\n\nDivide and Conquer\n\nTake $x^{10}$ and as example\n\n$x^{10} = x^5 * x^5 * x^0$\n$x^5 = x^2 * x^2 * x^1$\n$x^2 = x^1 * x^1 * x^0$\n\n# Complexity\n- Time complexity: $$O(logN)$$\n\n- Space complexity: $$O(logN)$$\n\n# Code\n```\nclass Solution:\n @cache\n def myPow(self, x: float, n: int) -> floa... | 19 | Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., `xn`).
**Example 1:**
**Input:** x = 2.00000, n = 10
**Output:** 1024.00000
**Example 2:**
**Input:** x = 2.10000, n = 3
**Output:** 9.26100
**Example 3:**
**Input:** x = 2.00000, n = -2
**... | null |
Python3 Beats 95% runtime Recursion Short Answer | powx-n | 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: logn\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: logn\n<!-- Add your space complexity here, e.g. $... | 1 | Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., `xn`).
**Example 1:**
**Input:** x = 2.00000, n = 10
**Output:** 1024.00000
**Example 2:**
**Input:** x = 2.10000, n = 3
**Output:** 9.26100
**Example 3:**
**Input:** x = 2.00000, n = -2
**... | null |
Python Recursive Solution - Faster than 99 % | powx-n | 0 | 1 | \n```\nclass Solution:\n def myPow(self, x: float, n: int) -> float:\n\n def function(base=x, exponent=abs(n)):\n if exponent == 0:\n return 1\n elif exponent % 2 ==... | 147 | Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., `xn`).
**Example 1:**
**Input:** x = 2.00000, n = 10
**Output:** 1024.00000
**Example 2:**
**Input:** x = 2.10000, n = 3
**Output:** 9.26100
**Example 3:**
**Input:** x = 2.00000, n = -2
**... | null |
50. Pow(x, n) Solution in Python | powx-n | 0 | 1 | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nSure, let me explain the code in a more understandable way.\n\nThis code defines a Python class called "Solution" with a method (function) called "myPow." The purpose of this method is to calculate the result of raising a given number \'x\' to a... | 3 | Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., `xn`).
**Example 1:**
**Input:** x = 2.00000, n = 10
**Output:** 1024.00000
**Example 2:**
**Input:** x = 2.10000, n = 3
**Output:** 9.26100
**Example 3:**
**Input:** x = 2.00000, n = -2
**... | null |
Easy to understanable soltuion | n-queens | 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 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Easy To Understand || Beats 99%|| | n-queens | 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 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
🔥 [Python3] Easy backtracking with 1 hash | n-queens | 0 | 1 | ```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n d, boards = set(), []\n\n def backtracking(row, leaft, board):\n if not leaft:\n boards.append([\'\'.join(row )for row in board])\n return\n\n line = [\'.\' for _ in range(n)]... | 8 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
O(1) solution 220 IQ moment | n-queens | 0 | 1 | # Intuition\ncommon sense\n\n# Approach\nFind answer of each from test case then copy-paste the results.\nBy writing 9 if conditions.\n# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n return [] | 2 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Most efficient solution with complete explanation | n-queens | 1 | 1 | \n\n# Approach\n**solveNQueens Function Setup:**\n\n- Initialize an empty vector of vector of strings named ans to store the solutions.\n- Create a vector of strings named board to represent the current state of the chessboard. Initialize each string with N \'.\' characters.\n - Create three vectors of integers:\n -... | 4 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
✅ Python Solution with Explanation | n-queens | 0 | 1 | The approach that we will be using is backtracking. I have added comments in the code to help understand better.\n\n```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n state= [["."] * n for _ in range(n)] # start with empty board\n res=[]\n \n # for tracking the co... | 46 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Python backtracking solution | n-queens | 0 | 1 | # Intuition\nUsing backtracking, we\'ll add combinations of Qs to an empty n * n board conditional upon visited sets marking columns, bottom right diagonals, and bottom left diagonals, while iterating by a row-by-row basis. Upon iterating our row to n, append to our answer array.\n# Approach\n1. We first declare our se... | 3 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Python3 || Backtracking || Language Independent Approach | n-queens | 0 | 1 | ```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n def issafe(r,c):\n n = len(board)\n for i in range(n):\n if board[i][c] == \'Q\':\n return False\n if r - i >= 0 and c - i >= 0 and board[r-i][c-i] == \'Q\':\n ... | 21 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Backtracking Logic Solution | n-queens | 0 | 1 | \n# 1. BackTracking Solution\n```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n board=[]\n ans=[]\n lrow=[0]*n\n upperd=[0]*(2*n-1)\n lowerd=[0]*(2*n-1)\n self.solve(0,board,ans,lrow,lowerd,upperd,n)\n return ans\n def solve(self,col,board... | 4 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
EFFICIENT PYTHON SOLUTION-EASY TO UNDERSTAND | n-queens | 0 | 1 | \n# Approach\nUsed Hashing to check ifSafe to place the Queen without attack\n\n\n# Code\n```\nclass Solution:\n def solve(self,col,board,ans,n,leftrow,upperdia,lowerdia):\n if(col==n):\n ans.append(board.copy())\n return\n for row in range(n):\n if(leftrow[row]==0 and ... | 2 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Easy Understanding Python Solution || Beats 97% of other solutions | n-queens | 0 | 1 | Please Upvote if you like it.\n\nPython Code:\n```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n def b(a):\n if a == n:\n ans.append(["".join(a) for a in c])\n return\n for i in range(n):\n if not e[i] and not d[a+i] ... | 3 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Python Easy Solution||Beats 98.41% Solutions||Backtracking | n-queens | 0 | 1 | # Python Solution\n\n# Beats 98.41% Solutions\n\n# Approach\nBackTracking Approach\n\n\n# Code\n```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n ############## Diagonal = row-col AntiDiagonal = row+col ##########\n\n def solver(row):\n if row == n:\... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
✔️ ||BACKTRACKING EXPLAINED || ✔️ | n-queens | 0 | 1 | We first create a **( n X n )** chess board and assign **0** to every index.\nWhenever a queen will be placed, index will be made **1**.\n\nIn this approach , we fill queens **column-wise** starting from left side.\n\nWhenever a queen is placed, at first it is checked if it satisfies the conditions given that it is not... | 4 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
✔️ PYTHON || ✔️ EXPLAINED || ; ] | n-queens | 0 | 1 | We first create a **( n X n )** chess board and assign **0** to every index.\nWhenever a queen will be placed, index will be made **1**.\n\nIn this approach , we fill queens **column-wise** starting from left side.\n\nWhenever a queen is placed, at first it is checked if it satisfies the conditions given that it is not... | 4 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
Each solution contains a distinct board configuration of the n-... | null |
Python fast backtracking + horizontal symmetry [97.39%] | n-queens-ii | 0 | 1 | # Intuition\nTry to check all possible combinations with backtracking.\nE.g.\nFind a row where you can put a queen in the first column. \nFind a row where you can put a queen in the second column.\n...\nIf it\'s not last column, but you can\'t put queen. Then go back to the previous column and find the next row where y... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
Python || Recursive BackTracking || Just Print the length 🔥 | n-queens-ii | 0 | 1 | # Code\n```\nclass Solution:\n def IsSafe(self,row,column,n,board):\n for i in range(column):\n if board[row][i] == "Q":\n return False\n i,j = row,column\n while(i>=0 and j>=0):\n if(board[i][j] == "Q"):\n return False\n i -= 1\n ... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
Python3 Backtracking Easy solution beats 98% | n-queens-ii | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def totalNQueens(self, n: int) -> int:\n ans=0\n left,upleft,lowleft=[0]*n,[0]*(2*n-1),[0]*(2*n-1)\n def solve(col,board):\n nonlocal ans\n if col==n:\n ans+=1\n return \n for row in range(n):\n ... | 2 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
Python || 96.52% Faster || Two Approaches | n-queens-ii | 0 | 1 | **Brute Force Approch:**\n```\nclass Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n def safe(row,col,board):\n i,j=row,col\n while i>=0 and j>=0: # for diagonaly upwards direction in left\n if board[i][j]==\'Q\':\n return False\n ... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
BackTracking Logic Solution | n-queens-ii | 0 | 1 | \n\n# 1. BackTracking Logic Solution\n```\nclass Solution:\n def totalNQueens(self, n: int) -> int:\n def addans(board,ans):\n temp=[]\n for row in board:\n for j in range(len(row)):\n if row[j]=="Q":\n temp.append(j+1)\n ... | 4 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
✅ Python Solution with Explanation | n-queens-ii | 0 | 1 | This problem is an extension on the yesterday\'s daily challenge problem [N-Queens](https://leetcode.com/problems/n-queens/discuss/2107719/python-solution-with-explanation). Please refer the link to understand how **N-Queens** is implemented. \n\n\nThe below code is just a slight modification on yesterday\'s [solution]... | 22 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
52. N-Queens II with step by step explanation | n-queens-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- In this solution, we use depth first search (dfs) to find all solutions to the N-Queens problem\n- The queens list keeps track of the row number for each column, where queens[i] represents the row number for the ith column... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
BackTracking Logic Solution | n-queens-ii | 0 | 1 | \n\n# 1. BackTracking Logic Solution\n\n```\nclass Solution:\n def totalNQueens(self, n: int) -> int:\n res,col,pos,neg=0,set(),set(),set()\n def backtracking(r):\n if n==r:\n nonlocal res\n res+=1\n for c in range(n):\n if c in col or ... | 5 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
Easiest Python Solution Beating 81.66% | n-queens-ii | 0 | 1 | \n\n\n# Code\n```\nclass Solution:\n def totalNQueens(self, n: int) -> int:\n def search(queens, dif, sum):\n l = len(queens)\n if l == n:\n self.ans += 1\n ... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
Easiest solution you will ever see! | n-queens-ii | 0 | 1 | Placing a queen at [r, c] imposes 4 bans on further placements.\n1. Can\'t place another queen in row r.\n2. Can\'t place another queen in col c.\n3. Can\'t place another queen in [r\',c\'] where r\'+c\' == r+c (same diagonal, lets call it sum diagonal).\n4. Can\'t place another queen in [r\',c\'] where r\'-c\' == r-... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
Beats 99.20% (32ms) clean recursive solution in Python | n-queens-ii | 0 | 1 | This solution is basically the same as the solution introduced in the article, but I condensed it into 10 lines of code to look more compact.\n\n```\nclass Solution:\n def totalNQueens(self, n: int) -> int: \n def rec(col, horizontal, diag, anti_diag):\n if col == n: return 1\n res = 0\n... | 5 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
python3- Simple | n-queens-ii | 0 | 1 | Simple understanding\n```\nclass Solution:\n def totalNQueens(self, n: int) -> int:\n def verify(r,c,board):\n for i in range(n):\n for j in range(n):\n if (i==r and j!=c) or (j==c and i!=r) or abs(r-i)==abs(c-j):\n if board[i][j]=="Q":\n ... | 1 | The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
**Example 1:**
**Input:** n = 4
**Output:** 2
**Explanation:** There are two distinct solutions ... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.