post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/minimum-height-trees/discuss/923881/Python-Clean-and-Simple | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1: return [0]
graph = defaultdict(set)
for src, dst in edges:
graph[src].add(dst)
graph[dst].add(src)
leaves = [node for node in graph if len(g... | minimum-height-trees | [Python] Clean & Simple | yo1995 | 2 | 120 | minimum height trees | 310 | 0.385 | Medium | 5,400 |
https://leetcode.com/problems/minimum-height-trees/discuss/2132725/Python-topological-sort | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n <= 2:
return [i for i in range(n)]
adj = defaultdict(list)
edge_count = defaultdict(int)
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
... | minimum-height-trees | Python, topological sort | blue_sky5 | 1 | 130 | minimum height trees | 310 | 0.385 | Medium | 5,401 |
https://leetcode.com/problems/minimum-height-trees/discuss/799117/Python3-two-BFSs | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
graph = [[] for _ in range(n)]
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
def fn(x):
"""Return most distant node and trace."""
q... | minimum-height-trees | [Python3] two BFSs | ye15 | 1 | 195 | minimum height trees | 310 | 0.385 | Medium | 5,402 |
https://leetcode.com/problems/minimum-height-trees/discuss/799117/Python3-two-BFSs | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
graph = [set() for _ in range(n)]
for u, v in edges:
graph[u].add(v)
graph[v].add(u)
leaves = [x for x in range(n) if len(graph[x]) <= 1]
while n > 2:... | minimum-height-trees | [Python3] two BFSs | ye15 | 1 | 195 | minimum height trees | 310 | 0.385 | Medium | 5,403 |
https://leetcode.com/problems/minimum-height-trees/discuss/2816738/My-DFS-solution-(backtracking) | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1: return [0]
adj = defaultdict(list)
for i, j in edges:
adj[i].append(j)
adj[j].append(i)
def backtracking(node, parenet, path, longestPath):
... | minimum-height-trees | My DFS solution (backtracking) | toshiwu006 | 0 | 4 | minimum height trees | 310 | 0.385 | Medium | 5,404 |
https://leetcode.com/problems/minimum-height-trees/discuss/2769386/Naive-Solution-and-Efficient-Solution-Explained | class Solution:
# O(n^2) solution using dfs and treating all nodes as roots for once
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1: return [0]
graph = defaultdict(list)
nodes = set()
for a, b in edges:
nodes.add(a)
no... | minimum-height-trees | Naive Solution and Efficient Solution Explained | shiv-codes | 0 | 11 | minimum height trees | 310 | 0.385 | Medium | 5,405 |
https://leetcode.com/problems/minimum-height-trees/discuss/2769386/Naive-Solution-and-Efficient-Solution-Explained | class Solution:
# Fast Efficient solution using topological sorting
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
graph = defaultdict(list)
indegree = defaultdict(int)
for a, b in edges:
graph[a].append(b)
graph[b].append(a)
... | minimum-height-trees | Naive Solution and Efficient Solution Explained | shiv-codes | 0 | 11 | minimum height trees | 310 | 0.385 | Medium | 5,406 |
https://leetcode.com/problems/minimum-height-trees/discuss/2730030/Multi-source-BFS-similar-to-Rotting-Oranges-problem | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1:
return [0]
adjMap = {i:[] for i in range(n)}
indegree = {i:0 for i in range(n)}
for source1,source2 in edges:
adjMap[source1].append(sou... | minimum-height-trees | Multi-source BFS similar to Rotting Oranges problem | ngnhatnam188 | 0 | 16 | minimum height trees | 310 | 0.385 | Medium | 5,407 |
https://leetcode.com/problems/minimum-height-trees/discuss/2506370/Python-BFS-Queue-80-faster | class Solution:
def findMinHeightTrees(self, total_node: int, edges: List[List[int]]) -> List[int]:
graph = defaultdict(list)
for u,v in edges:
graph[u].append(v)
graph[v].append(u)
queue = deque()
degree = {}
for node, val in graph.items():
... | minimum-height-trees | Python BFS Queue 80% faster | Abhi_009 | 0 | 173 | minimum height trees | 310 | 0.385 | Medium | 5,408 |
https://leetcode.com/problems/minimum-height-trees/discuss/1392181/Easy-to-read-solution | class Solution:
def make_graph(self, n: int, edges: List[List[int]]) -> Dict:
graph = {k: set() for k in range(n)}
for edge in edges:
graph[edge[0]].add(edge[1])
graph[edge[1]].add(edge[0])
return graph
def make_leaves(self, graph: Dict) -> Set:
... | minimum-height-trees | Easy to read solution | ssshukla26 | 0 | 105 | minimum height trees | 310 | 0.385 | Medium | 5,409 |
https://leetcode.com/problems/minimum-height-trees/discuss/1304406/Python3-DFS-with-memorization | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
dicts = {}
for source, target in edges:
if source not in dicts:
dicts[source] = {target}
else:
dicts[source].add(target)
if target not in dicts:
dicts[target] ... | minimum-height-trees | Python3 DFS with memorization | xxHRxx | 0 | 154 | minimum height trees | 310 | 0.385 | Medium | 5,410 |
https://leetcode.com/problems/minimum-height-trees/discuss/1258380/Python-Solution-based-on-Topological | class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n<2:
return range(n)
graph=defaultdict(set)
indegree=[0]*n
for u,v in edges:
graph[u].add(v)
graph[v].add(u)
indegree[u]+=1
... | minimum-height-trees | Python Solution based on Topological | jaipoo | 0 | 223 | minimum height trees | 310 | 0.385 | Medium | 5,411 |
https://leetcode.com/problems/burst-balloons/discuss/1477014/Python3-or-Top-down-Approach | class Solution(object):
def maxCoins(self, nums):
n=len(nums)
nums.insert(n,1)
nums.insert(0,1)
self.dp={}
return self.dfs(1,nums,n)
def dfs(self,strt,nums,end):
ans=0
if strt>end:
return 0
if (strt,end) in self.dp:
return s... | burst-balloons | [Python3] | Top-down Approach | swapnilsingh421 | 2 | 269 | burst balloons | 312 | 0.568 | Hard | 5,412 |
https://leetcode.com/problems/burst-balloons/discuss/1191940/Python3-why-1-less-n-less-500 | class Solution:
def maxCoins(self, nums: List[int]) -> int:
nums = [1] + nums + [1] # augmented
n = len(nums)
dp = [[0]*n for _ in range(n)]
for i in reversed(range(n)):
for j in range(i, n):
for k in range(i+1, j):
dp[i][j... | burst-balloons | [Python3] why 1 <= n <= 500? | ye15 | 2 | 123 | burst balloons | 312 | 0.568 | Hard | 5,413 |
https://leetcode.com/problems/burst-balloons/discuss/1659820/Python3-3-liner-recursive-solution | class Solution:
def maxCoins(self, nums):
nums = [1] + nums + [1]
@cache
def solve(l,r):
return max((solve(l,k-1)+nums[l-1]*nums[k]*nums[r+1]+solve(k+1,r) for k in range(l,r+1)),default=0)
return solve(1,len(nums)-2) | burst-balloons | Python3 3-liner recursive solution | pknoe3lh | 1 | 142 | burst balloons | 312 | 0.568 | Hard | 5,414 |
https://leetcode.com/problems/burst-balloons/discuss/2669841/Python-DP-Tabulation-Easy | class Solution:
def maxCoins(self, nums: List[int]) -> int:
n=len(nums)
nums.append(1)
nums.insert(0,1)
dp=[[0]*(n+2) for i in range(n+2)]
for i in range(n,0,-1):
for j in range(1,n+1):
if i>j:
continue
ma=float(... | burst-balloons | Python DP Tabulation Easy | ayush-09 | 0 | 12 | burst balloons | 312 | 0.568 | Hard | 5,415 |
https://leetcode.com/problems/burst-balloons/discuss/2591655/Python-or-Bottom-Up-DP-or-Clear-Explanation | class Solution:
def maxCoins(self, nums: List[int]) -> int:
dp = [[0 for j in range(len(nums))] for i in range(len(nums))]
# fill the (0,0), (1,1) etc
for i in range(len(nums)):
dp[i][i] = nums[i]*(nums[i-1] if i-1>=0 else 1)*(nums[i+1] if i+1<len(nums) else 1)
for offset in range(1,len(nums)):
x... | burst-balloons | Python | Bottom Up DP | Clear Explanation | vishyarjun1991 | 0 | 84 | burst balloons | 312 | 0.568 | Hard | 5,416 |
https://leetcode.com/problems/burst-balloons/discuss/2345158/Dynamic-Programming-or-Python | class Solution:
def maxCoins(self, nums: List[int]) -> int:
n = len(nums)
points = [1] * (n + 2)
for i in range(1, n + 1):
points[i] = nums[i - 1]
dp = [[0] * (n + 2) for _ in range(n + 2)]
for i in range(n + 1, -1, -1):
for j in rang... | burst-balloons | Dynamic Programming | Python | Kiyomi_ | 0 | 79 | burst balloons | 312 | 0.568 | Hard | 5,417 |
https://leetcode.com/problems/burst-balloons/discuss/1747409/NEED-HELP-About-Running-Time!!! | class Solution:
def maxCoins(self, nums: List[int]) -> int:
nums = [1] + nums + [1]
N = len(nums)
"""
Recursive relation
Base case i==j: 0
max(res, dp(i,k) + dp(k + 1, j) + nums[i-1] * nums[k] * nums[j]) for k in (i,j)
"""
# return dp(1, N-1)
d... | burst-balloons | 🔴 NEED HELP About Running Time!!! | atiq1589 | 0 | 38 | burst balloons | 312 | 0.568 | Hard | 5,418 |
https://leetcode.com/problems/burst-balloons/discuss/1379272/Python-Easy-Solution-!!! | class Solution:
def maxCoins(self, nums: List[int]) -> int:
n=len(nums)
dp=[[0 for i in range(n)] for j in range(n)]
for gap in range(n):
for row in range(n-gap):
col=row+gap
prev=1 if row==0 else nums[row-1]
next=1 if col==n-1 else... | burst-balloons | Python Easy Solution !!! | reaper_27 | 0 | 165 | burst balloons | 312 | 0.568 | Hard | 5,419 |
https://leetcode.com/problems/burst-balloons/discuss/1416741/Python-DP-(memoized-bottom-up)-solution | class Solution:
def maxCoins(self, nums: List[int]) -> int:
nums = [1] + nums + [1]
size = len(nums)
t = [[-1 for p in range(0,size+1)]
for q in range(0,size+1)]
return self.solve(nums,1,size-1,t)
def solve(self,arr,i,j,t):
i... | burst-balloons | Python DP (memoized, bottom up) solution | Saura_v | -2 | 453 | burst balloons | 312 | 0.568 | Hard | 5,420 |
https://leetcode.com/problems/super-ugly-number/discuss/2828266/heapq-did-the-job | class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
hp=[1]
dc={1}
i=1
while(n):
mn=heapq.heappop(hp)
if(n==1):
return mn
for p in primes:
newno=mn*p
if(newno in dc):
... | super-ugly-number | heapq did the job | droj | 5 | 40 | super ugly number | 313 | 0.458 | Medium | 5,421 |
https://leetcode.com/problems/super-ugly-number/discuss/788267/Python3-k-pointers | class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
ans = [1]
ptr = [0]*len(primes) #all pointing to 0th index
for _ in range(1, n):
ans.append(min(ans[ptr[i]]*p for i, p in enumerate(primes)))
for i, p in enumerate(primes):
... | super-ugly-number | [Python3] k pointers | ye15 | 2 | 179 | super ugly number | 313 | 0.458 | Medium | 5,422 |
https://leetcode.com/problems/super-ugly-number/discuss/788267/Python3-k-pointers | class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
ans = [1]
hp = [(p, 0) for p in primes]
for _ in range(1, n):
ans.append(hp[0][0])
while ans[-1] == hp[0][0]:
val, i = heappop(hp)
val = val//(ans[i]) * a... | super-ugly-number | [Python3] k pointers | ye15 | 2 | 179 | super ugly number | 313 | 0.458 | Medium | 5,423 |
https://leetcode.com/problems/super-ugly-number/discuss/2818384/Python3-Heap | class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
h, cnt, last, seen = [1], 0, -1, set()
max_seen = -1
while cnt < n:
c = heappop(h)
cnt += 1
seen.add(c)
for p in primes:
if c * p not in seen and (... | super-ugly-number | Python3 - Heap | godshiva | 0 | 7 | super ugly number | 313 | 0.458 | Medium | 5,424 |
https://leetcode.com/problems/super-ugly-number/discuss/2795540/Python-(Simple-Heap) | class Solution:
def nthSuperUglyNumber(self, n, primes):
ans = [1]
while n:
val = heappop(ans)
while ans and ans[0] == val:
heappop(ans)
for p in primes:
heappush(ans,p*val)
n -= 1
return val | super-ugly-number | Python (Simple Heap) | rnotappl | 0 | 4 | super ugly number | 313 | 0.458 | Medium | 5,425 |
https://leetcode.com/problems/super-ugly-number/discuss/2753210/easy-solution-(similar-to-nth-ugly-number) | class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
dp = [0]*(n+1)
dp[1] = 1
p_ = [1]*len(primes)
for i in range(2,n+1):
mini = float('inf')
for p in range(len(primes)):
mini = min(mini,primes[p]*dp[p_[p]])
... | super-ugly-number | easy solution (similar to nth ugly number) | neeshumaini55 | 0 | 4 | super ugly number | 313 | 0.458 | Medium | 5,426 |
https://leetcode.com/problems/super-ugly-number/discuss/369835/Three-Short-Solutions-in-Python-3-(Bisect-Heap-DP) | class Solution:
def nthSuperUglyNumber(self, n: int, p: List[int]) -> int:
N, I, L = [1], [0]*len(p), len(p)
for _ in range(n-1):
N.append(min([N[I[i]]*p[i] for i in range(L)]))
for i in range(L): I[i] += N[I[i]]*p[i] == N[-1]
return N[-1]
- Junaid Mansuri
(LeetCode ID)@hotmail.co... | super-ugly-number | Three Short Solutions in Python 3 (Bisect, Heap, DP) | junaidmansuri | -1 | 448 | super ugly number | 313 | 0.458 | Medium | 5,427 |
https://leetcode.com/problems/super-ugly-number/discuss/1237418/Python3-simple-solution-using-min-heap | class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
ugly = [1]
seen = set()
x = []
heapq.heapify(x)
while len(ugly) != n:
for i in primes:
if ugly[-1]*i not in seen:
seen.add(ugly[-1]*i)
... | super-ugly-number | Python3 simple solution using min-heap | EklavyaJoshi | -3 | 155 | super ugly number | 313 | 0.458 | Medium | 5,428 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/2320000/Python3.-oror-binSearch-6-lines-w-explanation-oror-TM%3A-9784 | class Solution: # Here's the plan:
# 1) Make arr, a sorted copy of the list nums.
# 2) iterate through nums. For each element num in nums:
# 2a) use a binary search to determine the count of elements
# in the arr that ... | count-of-smaller-numbers-after-self | Python3. || binSearch 6 lines, w/ explanation || T/M: 97%/84% | warrenruud | 27 | 3,000 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,429 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/1045763/Merge-sort-solution | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
counts = [0] * len(nums) # to save counts
indexed_nums = [(nums[key_index], key_index) for key_index in range(len(nums))] # change the nums into (number, index) pairs
desc_nums, counts = self.mergeWithCount(indexed_nums, c... | count-of-smaller-numbers-after-self | Merge sort solution | greatidea | 18 | 1,500 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,430 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/1178309/Python3-Fenwick-tree | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
ans = [0]*len(nums)
nums = list(enumerate(nums))
def fn(nums, aux, lo, hi):
"""Sort nums via merge sort and populate ans."""
if lo+1 >= hi: return
mid = lo + hi >> 1
... | count-of-smaller-numbers-after-self | [Python3] Fenwick tree | ye15 | 4 | 244 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,431 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/2321807/python3-simple-solution-using-bisect | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
ans = []
deque = collections.deque()
for num in nums[::-1]:
index = bisect.bisect_left(deque,num)
ans.append(index)
deque.insert(index,num)
return ans[::-1] | count-of-smaller-numbers-after-self | python3, simple solution, using bisect | pjy953 | 1 | 40 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,432 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/2321289/Python-solution-with-MergeSort-and-Fenwick-tree | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
def merge(nums,start,mid,end,res):
left = start
right = mid + 1
result = []
inversion_count = 0
while left <= mid and right <= end:
if nums[left][1] <= nums[righ... | count-of-smaller-numbers-after-self | Python solution with MergeSort and Fenwick tree | manojkumarmanusai | 1 | 119 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,433 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/2320775/Python-Simple-Python-Solution-Using-Binary-Search-(-Bisect_Left-) | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
array = sorted(nums)
result = []
for num in nums:
return_value = bisect.bisect_left(array, num)
del array[return_value]
if return_value == -1:
result.append(0)
else:
result.append(return_value)
return result | count-of-smaller-numbers-after-self | [ Python ] ✅✅ Simple Python Solution Using Binary Search ( Bisect_Left ) 🔥🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 1 | 117 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,434 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/1389517/Bisect-the-tail | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
len_nums = len(nums)
counts = [0] * len_nums
tail = [nums[len_nums - 1]]
for i in range(len_nums - 2, -1, -1):
idx = bisect_left(tail, nums[i])
counts[i] = idx
tail.insert(idx, n... | count-of-smaller-numbers-after-self | Bisect the tail | EvgenySH | 1 | 121 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,435 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/2050139/Python-or-Binary-Search | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
l=len(nums)
ans=[0]*l
arr=[nums[-1]]
i=l-2
for n in nums[-2::-1]:#Starting from 2nd last el
lo,hi=0,len(arr)-1
while lo<=hi:
mid=lo+(hi-lo)//2
... | count-of-smaller-numbers-after-self | Python | Binary Search | heckt27 | 0 | 134 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,436 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/1964059/Python-easy-to-read-and-understand-or-divide-and-conquer | class Solution:
def merge(self, left, right):
#print(left, right)
m, n = len(left), len(right)
cnt = 0
i, j = 0, 0
sorted_arr = []
while i < m and j < n:
if left[i][1] > right[j][1]:
sorted_arr.append(right[j])
j +=... | count-of-smaller-numbers-after-self | Python easy to read and understand | divide and conquer | sanial2001 | 0 | 306 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,437 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/1383367/Python3-BIT-and-padding-beat-%2B96 | class Solution:
def countSmaller(self, A: List[int]) -> List[int]:
n = len(A)
_min = min(A) - 1 # padding value
_max = max(A) - _min
result = [0] * n
bit = [0] * (_max+1)
def count(a):
c = 0;
while a > 0:
c += bit[a]
... | count-of-smaller-numbers-after-self | [Python3] BIT and padding, beat +96% | hieuvpm | 0 | 120 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,438 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/1299385/Python3-Simple-7-line-binary-search-solution | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
res = []
seq = sorted(nums)
for num in nums:
pos = bisect_left(seq, num)
res.append(pos)
seq.pop(pos)
return res | count-of-smaller-numbers-after-self | [Python3] Simple 7-line binary search solution | alsvia | 0 | 109 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,439 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/914190/Python-Intuitive-O(NlogN)-Solution-using-Merge-sort-divide-and-conquer-strategy | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
if not nums:
return []
st = 0
end = len(nums) - 1
res = [0] * len(nums)
self.merge_process(list(enumerate(nums)), st, end, res)
return res
def merge_process(self, nums, st, ... | count-of-smaller-numbers-after-self | [Python] Intuitive O(NlogN) Solution using Merge sort divide and conquer strategy | vasu6 | 0 | 197 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,440 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/914180/Python-Intuitive-O(NlogN)-Solution-using-Merge-sort-divide-and-conquer-strategy | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
if not nums:
return []
st = 0
end = len(nums) - 1
res = [0] * len(nums)
self.merge_process(list(enumerate(nums)), st, end, res)
return res
def merge_process(self, nums, st, ... | count-of-smaller-numbers-after-self | [Python] Intuitive O(NlogN) Solution using Merge sort divide and conquer strategy | vasu6 | 0 | 92 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,441 |
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/2323721/Beat-95-Python3-solutions | class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
l = len(nums)
arr, ans = sorted(nums), [0] * l
if l > 99:
for i in range(l-1):
ans[i] = bisect_left(arr, nums[i]) # binary search index
del arr[ans[i]]
else:
for i in range(l):
ans[i] = arr.... | count-of-smaller-numbers-after-self | Beat 95% Python3 solutions | AgentIvan | -1 | 30 | count of smaller numbers after self | 315 | 0.428 | Hard | 5,442 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1687144/Python-3-Simple-solution-using-a-stack-and-greedy-approach-(32ms-14.4MB) | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
stack = []
for idx, character in enumerate(s):
if not stack:
stack.append(character)
elif character in stack:
continue
else:
while stack and (... | remove-duplicate-letters | [Python 3] Simple solution using a stack and greedy approach (32ms, 14.4MB) | seankala | 5 | 498 | remove duplicate letters | 316 | 0.446 | Medium | 5,443 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/2050746/Python3-Runtime%3A-51ms-54.58-Memory%3A-13.9mb-82.79 | class Solution:
def removeDuplicateLetters(self, string: str) -> str:
lastIndex = [0] * 26
self.getLastIndexOfChar(string, lastIndex)
stack = self.maintainLexoOrder(string, lastIndex)
return ''.join(chr(ord('a') + char) for char in stack)
def getLastIndex... | remove-duplicate-letters | Python3 Runtime: 51ms 54.58% Memory: 13.9mb 82.79% | arshergon | 1 | 104 | remove duplicate letters | 316 | 0.446 | Medium | 5,444 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1860601/Python-Solution | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
n, lastIdx, insideStack, stack = len(s), [0] * 26, [0] * 26, [] # insideStack: it will show the current status of the stack, which character is present inside the stack at any particular instance of the loop.
getIdx = lam... | remove-duplicate-letters | ✅ Python Solution | dhananjay79 | 1 | 289 | remove duplicate letters | 316 | 0.446 | Medium | 5,445 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1860601/Python-Solution | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
n, stack, dic = len(s), [], {}
for i in range(n): dic[s[i]] = [0,i]
for i in range(n):
currChar = s[i]
if dic[currChar][0]: continue
while stack and stack[-1] > currChar and dic[stac... | remove-duplicate-letters | ✅ Python Solution | dhananjay79 | 1 | 289 | remove duplicate letters | 316 | 0.446 | Medium | 5,446 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1860300/93-Faster-oror-Python-Simple-Python-Solution-Using-Stack-and-Iterative-Approach | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
LastIndex = {}
for i in range(len(s)):
LastIndex[s[i]] = i
stack = []
AlreadySeen = set()
for i in range(len(s)):
if s[i] in AlreadySeen:
continue
else:
while stack and stack[-1] > s[i] and LastIndex[stack[-1]] > i:
... | remove-duplicate-letters | 93% Faster || [ Python ] ✔✔ Simple Python Solution Using Stack and Iterative Approach 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 1 | 272 | remove duplicate letters | 316 | 0.446 | Medium | 5,447 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/890287/my-python-Solution-using-counter-dict | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
myC=collections.Counter(s)
res=[]
seen=set()
for c in s:
while res and c<res[-1] and myC[res[-1]]>0 and c not in seen:
x=res.pop()
seen.discard(... | remove-duplicate-letters | my python 🐍 Solution using counter dict | InjySarhan | 1 | 446 | remove duplicate letters | 316 | 0.446 | Medium | 5,448 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/2821524/Python-oror-STACK-SOLUTION-oror-EASY | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
d = defaultdict(lambda:0)
for i in s : d[i]+=1
st,i = [] , 0
while i < len(s):
if st == []:
st.append([s[i],ord(s[i])])
d[s[i]] -= 1
elif [s[i],ord(s[i])] in ... | remove-duplicate-letters | Python || STACK SOLUTION || EASY | cheems_ds_side | 0 | 5 | remove duplicate letters | 316 | 0.446 | Medium | 5,449 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/2692696/Greedy-Approach-or-O(n)-time-or-O(n)-space-due-26-alphabets | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
charMaxIndex = {}
for i in range(len(s)):
charMaxIndex[s[i]] = i
visited = set()
stack = []
for i in range(len(s)):
if s[i] in visited:
continue
... | remove-duplicate-letters | Greedy Approach | O(n) time | O(n) space due 26 alphabets | wakadoodle | 0 | 7 | remove duplicate letters | 316 | 0.446 | Medium | 5,450 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/2666355/Python-easy-Stack-O(n) | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
count = Counter(s)
stack = deque()
result = set()
for i in s:
if i in result:
count[i] -= 1
continue
while(stack and stack[-1] > i and count[stack[-1]] > 1):
... | remove-duplicate-letters | Python easy Stack O(n) | anu1rag | 0 | 7 | remove duplicate letters | 316 | 0.446 | Medium | 5,451 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/2663022/Remove-Duplicate-Letters-oror-Python3-oror-Stack | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
d={}
for i in range(len(s)):
d[s[i]]=i
stack=[]
st=set()
for i in range(len(s)):
curr=s[i]
if curr in st:
continue
if len(stack)!=0 and stack[-1]<... | remove-duplicate-letters | Remove Duplicate Letters || Python3 || Stack | shagun_pandey | 0 | 4 | remove duplicate letters | 316 | 0.446 | Medium | 5,452 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/2557646/Simple-python-solution | class Solution:
def smallestSubsequence(self, s: str) -> str:
new = {}
for i in range(len(s)):
new[s[i]]=i
stak,seen=[],set()
for i in range(len(s)):
if s[i] not in seen:
while stak and new[stak[-1]]>i and stak[-1]>s[i]:
seen.remove(stak[-1])
stak.pop()
stak.append(s[i])
seen.add(s[... | remove-duplicate-letters | Simple python solution | SaiManoj1234 | 0 | 40 | remove duplicate letters | 316 | 0.446 | Medium | 5,453 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/2271058/Python-Stack-Solution | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
max_index = {}
for i in range(len(s)):
max_index[s[i]] = i
seen = set()
stack = []
for i in range(len(s)):
if s[i] in seen:continue
while stack and stac... | remove-duplicate-letters | Python Stack Solution | Abhi_009 | 0 | 51 | remove duplicate letters | 316 | 0.446 | Medium | 5,454 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1892465/Sliding-window-in-Python-no-stack-employed. | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
lower_bound, upper_bound = -1, len(s) - 1
alphabet, result = set(s), {}
while alphabet:
seen, indices = set(), {}
for i in range(upper_bound, lower_bound, -1):
seen.add(char := s[i])
... | remove-duplicate-letters | Sliding window in Python; no stack employed. | kmierzej | 0 | 52 | remove duplicate letters | 316 | 0.446 | Medium | 5,455 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1860666/python-stack-fast-easy-code | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
s = list(s)
last_index = {}
for i in range(len(s)):
last_index[s[i]] = i
stack =[]
visited = [False]*26
for i in range(0,len(s)):
if not vi... | remove-duplicate-letters | python stack fast easy code | Brillianttyagi | 0 | 63 | remove duplicate letters | 316 | 0.446 | Medium | 5,456 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1860336/python-oror-Simple-Solution-with-explanation | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
counter = Counter(s)
not_used = {c: True for c in s}
ans = ""
for c in s:
counter[c] -= 1
if not_used[c]:
while ans and ord(ans[-1]) >= ord(c) and counter[ans[-1]]>0:
... | remove-duplicate-letters | python || Simple Solution with explanation | zouhair11elhadi | 0 | 53 | remove duplicate letters | 316 | 0.446 | Medium | 5,457 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1859915/Python-or-TC-O(N)SC-O(N)-or-Easy-to-understand-with-comments-or | class Solution(object):
def removeDuplicateLetters(self, s):
count = Counter(s)
stack = []
for char in s:
# this condition is for removing duplicates
if char in stack:
count[char] -= 1
continue
# this condition is f... | remove-duplicate-letters | Python | TC-O(N)/SC-O(N) | Easy to understand with comments | | Patil_Pratik | 0 | 34 | remove duplicate letters | 316 | 0.446 | Medium | 5,458 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1634652/Pythonic-solution(with-speedy-improvement) | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
seen = {el: index for index, el in enumerate(s)} # write down last indexes for the values
result = [s[0]] # variable for future result
for i in range(1, len(s)): # start from index 1 as the first value has already b... | remove-duplicate-letters | Pythonic solution(with speedy improvement) | Dany_Sulimov | 0 | 249 | remove duplicate letters | 316 | 0.446 | Medium | 5,459 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1449546/Python3-Solution-using-stack | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
d = {c: i for i, c in enumerate(s)}
stack = ['#']
visited = set()
for idx, symb in enumerate(s):
if symb in visited:
continue
while symb < stack[-1] and ... | remove-duplicate-letters | [Python3] Solution using stack | maosipov11 | 0 | 166 | remove duplicate letters | 316 | 0.446 | Medium | 5,460 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/1449546/Python3-Solution-using-stack | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
stack = []
c2c = collections.Counter(s) # char to count
visited = set()
for c in s:
if c not in visited:
while stack and c2c[stack[-1]] > 0 and stack[-1] > c:
ele... | remove-duplicate-letters | [Python3] Solution using stack | maosipov11 | 0 | 166 | remove duplicate letters | 316 | 0.446 | Medium | 5,461 |
https://leetcode.com/problems/remove-duplicate-letters/discuss/894596/Python3-stack-O(N)-time-O(N)-space | class Solution:
def removeDuplicateLetters(self, s: str) -> str:
mp = {c: i for i, c in enumerate(s)}
stack = []
for i, c in enumerate(s):
if c not in stack:
while stack and c < stack[-1] and i < mp[stack[-1]]: stack.pop()
stack.append(c)
... | remove-duplicate-letters | [Python3] stack O(N) time O(N) space | ye15 | 0 | 157 | remove duplicate letters | 316 | 0.446 | Medium | 5,462 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085316/Python-or-or-Easy-3-Approaches-explained | class Solution:
def maxProduct(self, words: List[str]) -> int:
n=len(words)
char_set = [set(words[i]) for i in range(n)] # precompute hashset for each word
max_val = 0
for i in range(n):
for j in ra... | maximum-product-of-word-lengths | ✅ Python | | Easy 3 Approaches explained | constantine786 | 57 | 3,700 | maximum product of word lengths | 318 | 0.601 | Medium | 5,463 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085316/Python-or-or-Easy-3-Approaches-explained | class Solution:
def maxProduct(self, words: List[str]) -> int:
return max([len(s1) * len(s2) for s1, s2 in combinations(words, 2) if not (set(s1) & set(s2))], default=0) | maximum-product-of-word-lengths | ✅ Python | | Easy 3 Approaches explained | constantine786 | 57 | 3,700 | maximum product of word lengths | 318 | 0.601 | Medium | 5,464 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085316/Python-or-or-Easy-3-Approaches-explained | class Solution:
def maxProduct(self, words: List[str]) -> int:
n=len(words)
bit_masks = [0] * n
lengths = [0] * n
for i in range(n):
for c in words[i]:
bit_masks[i]|=1<<(ord(c) - ord('a')) # set the character bit
... | maximum-product-of-word-lengths | ✅ Python | | Easy 3 Approaches explained | constantine786 | 57 | 3,700 | maximum product of word lengths | 318 | 0.601 | Medium | 5,465 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2086001/Python-Simple-Solution-oror-Brute-force-to-Optimized-code | class Solution:
def maxProduct(self, words: List[str]) -> int:
l = []
for i in words:
for j in words:
# creating a string with common letters
com_str = ''.join(set(i).intersection(j))
# if there are no common letters
if len(com_str) == 0: ... | maximum-product-of-word-lengths | Python Simple Solution || Brute force to Optimized code | Shivam_Raj_Sharma | 5 | 262 | maximum product of word lengths | 318 | 0.601 | Medium | 5,466 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2088561/What-about-a-trie | class Solution:
def maxProduct(self, words: List[str]) -> int:
n = len(words)
best = 0
trie = {}
# Build a trie
# O(N * U * logU) where U is the number of unique letters (at most 26), simplified to O(N)
for word in words:
node = trie
letters = sorte... | maximum-product-of-word-lengths | What about a trie? | corcoja | 3 | 70 | maximum product of word lengths | 318 | 0.601 | Medium | 5,467 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/788487/Python3-%22brute-force%22ish | class Solution:
def maxProduct(self, words: List[str]) -> int:
mp = dict()
for word in words:
for c in word:
mp.setdefault(c, set()).add(word)
s = set(words)
ans = 0
for word in words:
comp = set().union(*(mp[c] for c in word))
... | maximum-product-of-word-lengths | [Python3] "brute-force"ish | ye15 | 2 | 127 | maximum product of word lengths | 318 | 0.601 | Medium | 5,468 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/788487/Python3-%22brute-force%22ish | class Solution:
def maxProduct(self, words: List[str]) -> int:
mp = defaultdict(int)
for word in words:
mask = 0
for ch in word: mask |= 1 << ord(ch)-97
mp[mask] = max(mp[mask], len(word))
return max((mp[x]*mp[y] for x in mp for y in mp if not x & y)... | maximum-product-of-word-lengths | [Python3] "brute-force"ish | ye15 | 2 | 127 | maximum product of word lengths | 318 | 0.601 | Medium | 5,469 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/788487/Python3-%22brute-force%22ish | class Solution:
def maxProduct(self, words: List[str]) -> int:
mp = {} #mapping from mask to length
for word in words:
mask = reduce(or_, (1 << ord(c)-97 for c in word), 0)
mp[mask] = max(len(word), mp.get(mask, 0))
return max((mp[x] * mp[y] for x in mp for ... | maximum-product-of-word-lengths | [Python3] "brute-force"ish | ye15 | 2 | 127 | maximum product of word lengths | 318 | 0.601 | Medium | 5,470 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2087664/simple-and-clean-code-in-python | class Solution:
def maxProduct(self, words: List[str]) -> int:
maxVal = 0
for comb in combinations(words,2):
for char in comb[0]:
if char in comb[1]:
break
else:
maxVal = max(len(comb[0])*len(comb[1]),maxVal)
return ... | maximum-product-of-word-lengths | simple and clean code in python | thunder-007 | 1 | 86 | maximum product of word lengths | 318 | 0.601 | Medium | 5,471 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085362/Python-brute-force-O(N2)-using-sets-and-bitmasks | class Solution:
def maxProduct(self, words: List[str]) -> int:
result = 0
words_set = [set(word) for word in words]
for i in range(len(words)-1):
for j in range(i + 1, len(words)):
if (len(words[i]) * len(words[j]) > result and
not (words_set[i... | maximum-product-of-word-lengths | Python, brute force O(N^2) using sets and bitmasks | blue_sky5 | 1 | 97 | maximum product of word lengths | 318 | 0.601 | Medium | 5,472 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085362/Python-brute-force-O(N2)-using-sets-and-bitmasks | class Solution:
def maxProduct(self, words: List[str]) -> int:
def bitmask(word):
mask = 0
for c in word:
mask |= 1 << ord(c) - ord('a')
return mask
result = 0
bitmasks = [bitmask(word) for ... | maximum-product-of-word-lengths | Python, brute force O(N^2) using sets and bitmasks | blue_sky5 | 1 | 97 | maximum product of word lengths | 318 | 0.601 | Medium | 5,473 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1724311/Python3-Basic-Solution | class Solution:
def maxProduct(self, words: List[str]) -> int:
m = 0
word = sorted(words,key = len)[::-1]
for i in range(len(words)):
for j in range(i,len(words)):
if(i==j):
continue
if(set(words[i]).intersection(set(words[j])) ... | maximum-product-of-word-lengths | Python3 Basic Solution | priyanshi23 | 1 | 110 | maximum product of word lengths | 318 | 0.601 | Medium | 5,474 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1492634/python3-O(n2)-Elegant-solution | class Solution:
def maxProduct(self, words: List[str]) -> int:
def check(a,b):
for char in a:
if char in b:
return False
return True
max_ln = 0
for indx,word in enumerate(words):
for nxt_word in words[i... | maximum-product-of-word-lengths | [python3] O(n^2) Elegant solution | _jorjis | 1 | 109 | maximum product of word lengths | 318 | 0.601 | Medium | 5,475 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/1235015/Python-3-Solution-using-set | class Solution:
def maxProduct(self, words: List[str]) -> int:
m = 0
for x in words:
for i in words:
if not set(x)&set(i):
k =len(x) * len(i)
if k > m:
m = k
return(m) | maximum-product-of-word-lengths | [Python 3] Solution using set | SushilG96 | 1 | 102 | maximum product of word lengths | 318 | 0.601 | Medium | 5,476 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2662717/Python-solution | class Solution:
def maxProduct(self, words: List[str]) -> int:
sets = [set()] * len(words)
ans = 0
for i in range(len(words)):
sets[i] = set(list(words[i]))
for i in range(len(words)):
for j in range(i+1,len(words)):
if len... | maximum-product-of-word-lengths | Python solution | maomao1010 | 0 | 15 | maximum product of word lengths | 318 | 0.601 | Medium | 5,477 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2562189/Python-simple-solution | class Solution:
def maxProduct(self, words: list[str]) -> int:
ans, d = 0, {k: set(k) for k in words}
for i in range(len(words) - 1):
for j in range(i + 1, len(words)):
w1, w2 = words[i], words[j]
if not d[w1] & d[w2]:
ans = max(ans... | maximum-product-of-word-lengths | Python simple solution | Mark_computer | 0 | 95 | maximum product of word lengths | 318 | 0.601 | Medium | 5,478 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2503130/easy-python-solution | class Solution:
def maxProduct(self, words: List[str]) -> int:
letter_dict = {}
for word in words :
letter_dict[word] = set([i for i in word])
max_num = 0
for i in range(len(words)) :
for j in range(i+1, len(words)) :
if len(words[i]) * len(... | maximum-product-of-word-lengths | easy python solution | sghorai | 0 | 57 | maximum product of word lengths | 318 | 0.601 | Medium | 5,479 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2439854/460ms-faster-than-88-easy-python-code | class Solution:
def maxProduct(self, words: List[str]) -> int:
n=len(words)
wordset=[set(i) for i in words]
product=[len(words[i])*len(words[j]) for i in range(n) for j in range(i,n) if wordset[i].isdisjoint(wordset[j])]
if len(product)==0:
return 0
return max(pro... | maximum-product-of-word-lengths | 460ms, faster than 88%, easy python code | ayushigupta2409 | 0 | 79 | maximum product of word lengths | 318 | 0.601 | Medium | 5,480 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2092136/Python3-or-using-sets-or-faster-than-73.61-of-Python3-online-submissions-for-Maximum-Product-of-Word | class Solution:
def maxProduct(self, words: List[str]) -> int:
stateLst = []
maxi = 0
n = len(words)
for i in range(n):
stateLst.append(self.findStateOfStr(words[i]))
for i in range(n):
for j in range(i+1, n):
if stateLst[i] &a... | maximum-product-of-word-lengths | ✅Python3 | using sets | faster than 73.61% of Python3 online submissions for Maximum Product of Word | prankurgupta18 | 0 | 79 | maximum product of word lengths | 318 | 0.601 | Medium | 5,481 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2092130/571-ms-faster-than-73.61-of-Python3-online-submissions-for-Maximum-Product-of-Word-Lengths. | class Solution:
def maxProduct(self, words: List[str]) -> int:
stateLst = []
maxi = 0
n = len(words)
for i in range(n):
stateLst.append(self.findStateOfStr(words[i]))
for i in range(n):
for j in range(i+1, n):
if stateLst[i] &a... | maximum-product-of-word-lengths | 571 ms, faster than 73.61% of Python3 online submissions for Maximum Product of Word Lengths. | prankurgupta18 | 0 | 36 | maximum product of word lengths | 318 | 0.601 | Medium | 5,482 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2089303/Python3-Easy-Approaches-Using-For-Loop-and-Break-(Explained) | class Solution:
def maxProduct(self, words: List[str]) -> int:
max_val = 0
status = False
newWords = []
for word in words:
newWords.append("".join(set(word)))
for i in range (len(words)):
for j in range (i+1, len(words)):
for letter1 in newWords[i]:
for letter2 in ne... | maximum-product-of-word-lengths | [Python3] Easy Approaches Using For Loop and Break (Explained) | hjpdy | 0 | 14 | maximum product of word lengths | 318 | 0.601 | Medium | 5,483 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2088641/Python-using-set()-and-hashmap | class Solution:
def maxProduct(self, words: List[str]) -> int:
words_set = [set(w) for w in words]
res = 0
dic = {i: v for i, v in enumerate(words_set)}
for i in range(len(words) - 1):
for j in range(i + 1, len(words)):
if len(dic[i].intersection(dic[j])) ... | maximum-product-of-word-lengths | Python using set() and hashmap | Kennyyhhu | 0 | 38 | maximum product of word lengths | 318 | 0.601 | Medium | 5,484 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2087649/Python-O(N2)-optimized-with-unique-bitmasks | class Solution:
def maxProduct(self, words: List[str]) -> int:
def bitmask(word):
mask = 0
for c in word:
mask |= 1 << ord(c) - 97 # 97 == ord('a')
return mask
bm2len = {}
for word in words:
bm... | maximum-product-of-word-lengths | Python, O(N^2) optimized with unique bitmasks | blue_sky5 | 0 | 13 | maximum product of word lengths | 318 | 0.601 | Medium | 5,485 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2087320/Python3-solution-or-Made-using-for-while-loops-and-if-else | class Solution:
def maxProduct(self, words: List[str]) -> int:
alph = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
prods = []
for i in range(len(words)):
k=i
while k<len(words)-1:
k+=1
... | maximum-product-of-word-lengths | Python3 solution | Made using for, while loops and if else | rogan35 | 0 | 34 | maximum product of word lengths | 318 | 0.601 | Medium | 5,486 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2086863/Python3-Solution-with-using-bit-manipulation | class Solution:
def maxProduct(self, words: List[str]) -> int:
d = {}
for word in words:
mask = 0
for c in word:
mask |= 1 << (ord(c) - ord('a'))
d[word] = mask
res = 0
for i in range(len(words) - 1):
... | maximum-product-of-word-lengths | [Python3] Solution with using bit-manipulation | maosipov11 | 0 | 10 | maximum product of word lengths | 318 | 0.601 | Medium | 5,487 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2086617/java-python-bit-manipulation | class Solution:
def maxProduct(self, words: List[str]) -> int:
table = [0]*len(words)
ans = 0
for i in range(len(words)) :
for j in range(len(words[i])) :
table[i] |= 1<<(ord(words[i][j]) - 97)
for i in range(len(words)) :
for j in range(i, len(words)) :
if (ta... | maximum-product-of-word-lengths | java, python - bit manipulation | ZX007java | 0 | 18 | maximum product of word lengths | 318 | 0.601 | Medium | 5,488 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2086536/Python | class Solution:
def maxProduct(self, ws: List[str]) -> int:
d = [set() for j in range(len(ws))]
for i in range(len(ws)):
for val in ws[i]:
d[i].add(val)
ws[i] = len(ws[i])
ans = 0
for i in range(len(ws)):
for j in range(i+1,len(ws))... | maximum-product-of-word-lengths | Python | Shivamk09 | 0 | 25 | maximum product of word lengths | 318 | 0.601 | Medium | 5,489 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2086446/Simple-brute-force-approach | class Solution:
def maxProduct(self, words: List[str]) -> int:
def check(a, b):
for i in a:
if i in b:
return False
return True
res = 0
for i in range(len(words)):
for j in range(len(words)):
if c... | maximum-product-of-word-lengths | Simple brute force approach | ankurbhambri | 0 | 22 | maximum product of word lengths | 318 | 0.601 | Medium | 5,490 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2086230/Python-Simple-Solution | class Solution:
def maxProduct(self, words: List[str]) -> int:
# initialize the variable's
max_ans, n = 0, len(words)
# generate all possible pairs via 2 for loop's
for i in range(n):
for j in range(i):
# check the set intersection. If there's no ... | maximum-product-of-word-lengths | Python Simple Solution | Nk0311 | 0 | 12 | maximum product of word lengths | 318 | 0.601 | Medium | 5,491 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2086143/Easiest-Approach-Python | class Solution:
def maxProduct(self, words: List[str]) -> int:
def checker(s1, s2):
flag = True
for i in s1:
if i in s2:
flag = False
return flag
max_prod = 0
... | maximum-product-of-word-lengths | Easiest Approach Python | Abhi-Jit | 0 | 24 | maximum product of word lengths | 318 | 0.601 | Medium | 5,492 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085838/Python-Python-Solution-Using-Brute-Force-Approach-or-O(n*n) | class Solution:
def maxProduct(self, words: List[str]) -> int:
def check(s1,s2):
for i in s1:
if i in s2:
return False
return True
result = 0
for i in range(len(words)-1):
for j in range(i+1,len(words)):
if check(words[i],words[j]) == True:
result = max(result,len(words[i])*len(w... | maximum-product-of-word-lengths | [ Python ] ✅✅ Python Solution Using Brute Force Approach | O(n*n) ✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 29 | maximum product of word lengths | 318 | 0.601 | Medium | 5,493 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085503/Python-Easy-Solution-(4-Lines) | class Solution:
def maxProduct(self, words: List[str]) -> int:
res = [0]
for i, word in enumerate(words):
for j in range(i+1, len(words)):
if all(char not in set(words[j]) for char in set(word)):
res.append(len(word)*len(words[j]))
return max(r... | maximum-product-of-word-lengths | Python Easy Solution (4 Lines) | pe-mn | 0 | 24 | maximum product of word lengths | 318 | 0.601 | Medium | 5,494 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085503/Python-Easy-Solution-(4-Lines) | class Solution:
def maxProduct(self, words: List[str]) -> int:
res = [0]
for i, word in enumerate(words):
for j in range(i+1, len(words)):
check = True
for char in set(word):
if char in set(words[j]):
check = Fal... | maximum-product-of-word-lengths | Python Easy Solution (4 Lines) | pe-mn | 0 | 24 | maximum product of word lengths | 318 | 0.601 | Medium | 5,495 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085498/Python-O(n2)-oror-Two-easy-approaches | class Solution:
def maxProduct(self, words: List[str]) -> int:
d=defaultdict(int)
for w in words:
bitw=0
for c in w:
bitw |=(1<<ord(c)-97)
d[w]=bitw
def common(s,t):
if d[s]&d[t]:return True
return False
... | maximum-product-of-word-lengths | Python O(n2) || Two easy approaches | aditya1292 | 0 | 29 | maximum product of word lengths | 318 | 0.601 | Medium | 5,496 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085498/Python-O(n2)-oror-Two-easy-approaches | class Solution:
def maxProduct(self, words: List[str]) -> int:
d=defaultdict(set)
for w in words:
d[w]=set(w)
def common(s,t):
if d[s]&d[t]:return True
return False
mx=0
for i in words:
for j in words:
... | maximum-product-of-word-lengths | Python O(n2) || Two easy approaches | aditya1292 | 0 | 29 | maximum product of word lengths | 318 | 0.601 | Medium | 5,497 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2085359/Easy-to-Understand-Solution-with-no-bit-manipulation | class Solution:
def maxProduct(self, words: List[str]) -> int:
def inter(s1,s2):
set1 = set(s1)
set2 = set(s2)
z = set1.intersection(set2)
if not z:
return True
return False
counter = 0
for i in range(0,len(words)):
... | maximum-product-of-word-lengths | Easy to Understand Solution with no bit manipulation | masamune-prog | 0 | 16 | maximum product of word lengths | 318 | 0.601 | Medium | 5,498 |
https://leetcode.com/problems/maximum-product-of-word-lengths/discuss/2071456/Python-Solution | class Solution:
def maxProduct(self, words: List[str]) -> int:
m = 0
wLen = len(words)
for i in range(wLen):
for j in range(wLen):
if all (k not in words[j] for k in words[i]):
m = max(m, len(words[i])*len(words[j]))
return m | maximum-product-of-word-lengths | Python Solution | vgholami | 0 | 21 | maximum product of word lengths | 318 | 0.601 | Medium | 5,499 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.