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/push-dominoes/discuss/2629342/Python-Solution-or-O(n)-or-Comments | class Solution:
def pushDominoes(self, dominoes: str) -> str:
n=len(dominoes)
left=[0]*n
right=[0]*n
# traverse from left side
# keep count of 'R' occurennces
count=0
for i in range(n):
if dominoes[i]=='L' or dominoes[i]=='R':
... | push-dominoes | Python Solution | O(n) | Comments | Siddharth_singh | 1 | 38 | push dominoes | 838 | 0.57 | Medium | 13,600 |
https://leetcode.com/problems/push-dominoes/discuss/2394442/Python-or-BFS | class Solution:
def pushDominoes(self, dom: str) -> str:
from collections import deque
n = len(dom)
d = set()
q = deque()
arr = [0 for i in range(n)]
for i in range(n):
if dom[i] == "L":
arr[i] = -1
d.add(i)
... | push-dominoes | Python | BFS | Shivamk09 | 1 | 114 | push dominoes | 838 | 0.57 | Medium | 13,601 |
https://leetcode.com/problems/push-dominoes/discuss/1262345/precomputation-oror-easy-understanding-oror-python | class Solution:
def pushDominoes(self, d: str) -> str:
n= len(d)
right =[9999999999999]*n
left = [9999999999999]*n
ne=-1
for i in range(n):
if d[i]=='R':
ne=i
if d[i]=='L':
... | push-dominoes | precomputation || easy understanding || python | chikushen99 | 1 | 192 | push dominoes | 838 | 0.57 | Medium | 13,602 |
https://leetcode.com/problems/push-dominoes/discuss/2748858/Python3-Commented-One-Pass-Solution | class Solution:
def pushDominoes(self, dominoes: str) -> str:
# go through the dominos and update
# once we hit a second domino
# the amount of doinos between
# two pushed once is
# idx - prev[] - 1
#
# the one missing there is the
# current pushed d... | push-dominoes | [Python3] - Commented, One-Pass Solution | Lucew | 0 | 3 | push dominoes | 838 | 0.57 | Medium | 13,603 |
https://leetcode.com/problems/push-dominoes/discuss/2640150/Runtime%3A-549-ms-faster-than-57.25-Memory-Usage%3A-16.9-MB-less-than-53.33 | class Solution:
def pushDominoes(self, dominoes: str) -> str:
condensed = []
index = 0
currDir = dominoes[0]
while index < len(dominoes) and dominoes[index] == currDir:
index += 1
count = index
if index == len(do... | push-dominoes | Runtime: 549 ms, faster than 57.25%; Memory Usage: 16.9 MB, less than 53.33% | GizDave | 0 | 3 | push dominoes | 838 | 0.57 | Medium | 13,604 |
https://leetcode.com/problems/push-dominoes/discuss/2632025/Python-two-for-loops.-Time%3A-O(N)-Space%3A-O(N) | class Solution:
def pushDominoes(self, dominoes: str) -> str:
result = [0] * len(dominoes)
left_idx = -math.inf
for idx, d in enumerate(dominoes):
if d == 'R':
left_idx = idx
elif d == 'L':
left_idx = -math.inf
... | push-dominoes | Python, two for-loops. Time: O(N), Space: O(N) | blue_sky5 | 0 | 9 | push dominoes | 838 | 0.57 | Medium | 13,605 |
https://leetcode.com/problems/push-dominoes/discuss/2631471/BFS-Solution-or-Python3 | class Solution:
# O(n) time,
# O(n) space,
# Approach: BFS, deque, hashtable
def pushDominoes(self, dominoes: str) -> str:
n = len(dominoes)
dominos = list(dominoes)
qu = deque()
for index, domino in enumerate(dominos):
if domino == '.': c... | push-dominoes | BFS Solution | Python3 | destifo | 0 | 12 | push dominoes | 838 | 0.57 | Medium | 13,606 |
https://leetcode.com/problems/push-dominoes/discuss/2631321/Faster-76-Memory-99.-Two-pointers-with-explanation!-O(n) | class Solution:
def pushDominoes(self, dominoes: str) -> str:
# Adding L and R letters, because we don't want to our logic be ruined by start of dot or etc.
# L at the beginning won't damage the result, since it is at the start and there is nothing before that.
# Same applies for the R
dominoes = "L... | push-dominoes | Faster 76%, Memory 99%. Two pointers with explanation! O(n) | milsolve | 0 | 14 | push dominoes | 838 | 0.57 | Medium | 13,607 |
https://leetcode.com/problems/push-dominoes/discuss/2631030/Python-3-a-different-approach-using-two-pointers | class Solution:
def pushDominoes(self, d: str) -> str:
n = len(d)
ans = [0]*n
flg = False
cnt = 0
for i in range(n):
if(d[i] == 'L'):
flg = False
continue
if(d[i] == 'R'):
flg = True
cnt ... | push-dominoes | Python 3 a different approach using two pointers | user2800NJ | 0 | 8 | push dominoes | 838 | 0.57 | Medium | 13,608 |
https://leetcode.com/problems/push-dominoes/discuss/2630874/Python-3-Clear-O(N)-with-Explanation | class Solution:
def pushDominoes(self, dominoes: str) -> str:
n = len(dominoes)
r_dist = [0 if d == 'R' else float('inf') for d in dominoes]
l_dist = [0 if d == 'L' else float('inf') for d in dominoes]
ri = float('inf')
for i, d in enumerate(dominoes):
if d == 'R'... | push-dominoes | Python 3, Clear O(N) with Explanation | Brent_Pappas | 0 | 10 | push dominoes | 838 | 0.57 | Medium | 13,609 |
https://leetcode.com/problems/push-dominoes/discuss/2630433/GolangPython-O(N)-time-or-O(N)-space | class Solution:
def pushDominoes(self, dominoes: str) -> str:
dominoes = list(dominoes)
prev_letter = None
prev_idx = -1
for i in range(len(dominoes)):
item = dominoes[i]
if item == "L" and prev_letter != "R":
for j in range(i-1,prev_idx,-1):
... | push-dominoes | Golang/Python O(N) time | O(N) space | vtalantsev | 0 | 9 | push dominoes | 838 | 0.57 | Medium | 13,610 |
https://leetcode.com/problems/push-dominoes/discuss/2630289/Python-Solution-using-deque | class Solution:
def pushDominoes(self, dominoes: str) -> str:
dom = list(dominoes)
q = collections.deque()
for i,d in enumerate(dom):
if d!=".":
q.append((i,d))
while q:
i,d = q.popleft()
if d == "L":
... | push-dominoes | Python Solution using deque | Namangarg98 | 0 | 10 | push dominoes | 838 | 0.57 | Medium | 13,611 |
https://leetcode.com/problems/push-dominoes/discuss/2630273/Python3-or-Replace-or-Fast-and-Simple | class Solution:
def pushDominoes(self, dominoes: str) -> str:
temp = ''
while dominoes != temp:
temp = dominoes
dominoes = dominoes.replace('R.L', 'ooo')
dominoes = dominoes.replace('R.', 'RR')
dominoes = dominoes.replace('.L', ... | push-dominoes | Python3 | Replace | Fast & Simple | joshua_mur | 0 | 5 | push dominoes | 838 | 0.57 | Medium | 13,612 |
https://leetcode.com/problems/push-dominoes/discuss/2629759/Easy-to-understand-or-No-DP-or-Brute-force-or-O(N)-solution | class Solution:
def pushDominoes(self, dominoes: str) -> str:
dominoesList,totalDominoes=list(dominoes), len(dominoes);
i = 0;
while i<totalDominoes:
if dominoesList[i] == 'L': #if L comes very fast then from L index to previous one becomes L. ie, ........L case
... | push-dominoes | Easy to understand | No DP | Brute force | O(N) solution | AshikeRN | 0 | 17 | push dominoes | 838 | 0.57 | Medium | 13,613 |
https://leetcode.com/problems/push-dominoes/discuss/2629723/Simple-dp-approachor-line-by-line-self-explanation | class Solution:
def pushDominoes(self, dominoes: str) -> str:
n=len(dominoes)
dp_R=[None for x in range(0,n)] ## one for right falling
dp_L=[None for x in range(0,n)] ##other one for left falling
for i in range(0,n):
if dominoes[i]=="." and (i-1>=0 and (dominoes[i-1]=="R"... | push-dominoes | Simple dp approach| line by line self explanation | Mom94 | 0 | 12 | push dominoes | 838 | 0.57 | Medium | 13,614 |
https://leetcode.com/problems/push-dominoes/discuss/2629419/Python-or-Only-if-else-and-loops-or-easy-solution | class Solution:
def pushDominoes(self, dom: str) -> str:
str1 = "."
i=0
count=0
while i<len(dom):
if dom[i]=='.':
count +=1
elif dom[i]=='L':
if str1[-1]=='R':
if count%2 == 0:
for j i... | push-dominoes | Python | Only if-else and loops | easy solution | Yash_A | 0 | 18 | push dominoes | 838 | 0.57 | Medium | 13,615 |
https://leetcode.com/problems/push-dominoes/discuss/2629289/Python3-Rotten-Oranges-type-approach-Queue | class Solution:
def pushDominoes(self, dominoes: str) -> str:
q = collections.deque()
timeArray = [-1]*len(dominoes)
dominosArr = list(dominoes)
n = len(dominoes)
for i, val in enumerate(dominosArr):
if val!='.':
q.append(i)
... | push-dominoes | Python3 - Rotten Oranges type approach - Queue | invisiblecoder | 0 | 13 | push dominoes | 838 | 0.57 | Medium | 13,616 |
https://leetcode.com/problems/push-dominoes/discuss/2629281/Python-Easy-to-understand-O(N)-solution | class Solution:
def pushDominoes(self, dominoes: str) -> str:
n = len(dominoes)
output = list(dominoes)
distances = [[float('inf'),float('inf')] for _ in range(n)]
# determine each domino's distance to nearest R domino
prev = float('inf')
for i,d in enumerate... | push-dominoes | [Python] Easy to understand O(N) solution | fomiee | 0 | 13 | push dominoes | 838 | 0.57 | Medium | 13,617 |
https://leetcode.com/problems/push-dominoes/discuss/2629253/If-else-ladder-with-monotonic-queue-in-Python-Solution | class Solution:
# Classic If Else Ladder + Monotonic Stack
def pushDominoes(self, dominoes: str) -> str:
d = [i for i in dominoes]
n = len(dominoes)
mono = []
res = ''
for i in range(n):
# Handle R...L
if mono and mono[0] == 'R' and d[i] == 'L':
... | push-dominoes | If else ladder with monotonic queue in Python Solution | shiv-codes | 0 | 9 | push dominoes | 838 | 0.57 | Medium | 13,618 |
https://leetcode.com/problems/push-dominoes/discuss/2629253/If-else-ladder-with-monotonic-queue-in-Python-Solution | class Solution:
# Classic If Else ladder + monotonic queue
def pushDominoes(self, dominoes: str) -> str:
d = list(dominoes)
mono, res = [], []
n = len(d)
for i in range(n):
if d[i] == 'L':
if mono and mono[0] == 'R':
print(mono)
... | push-dominoes | If else ladder with monotonic queue in Python Solution | shiv-codes | 0 | 9 | push dominoes | 838 | 0.57 | Medium | 13,619 |
https://leetcode.com/problems/push-dominoes/discuss/2629083/Python-Accepted | class Solution:
def pushDominoes(self, d: str) -> str:
right = [0 for i in range(0,len(d))]
left = [0 for i in range(0,len(d))]
prev = None
for i in range(len(d)):
if d[i]=='R':
right[i]=None
prev = i
elif d[i]=='L':
... | push-dominoes | Python Accepted ✅ | Khacker | 0 | 26 | push dominoes | 838 | 0.57 | Medium | 13,620 |
https://leetcode.com/problems/push-dominoes/discuss/2629001/python3-Iteration-sol-for-reference | class Solution:
def pushDominoes(self, dominoes: str) -> str:
D = len(dominoes)
posr = [0 for _ in range(D)]
FORCE = 10**5
r = 0
for d in range(D):
if dominoes[d] == "R":
r = FORCE
elif dominoes[d] == "L":
r = ... | push-dominoes | [python3] Iteration sol for reference | vadhri_venkat | 0 | 5 | push dominoes | 838 | 0.57 | Medium | 13,621 |
https://leetcode.com/problems/push-dominoes/discuss/2628738/O(n)-using-bfs-and-simulation-with-two-sets | class Solution:
def pushDominoes(self, dominoes: str) -> str:
info = {0: '.', -1: 'L', 1: 'R'}
n = len(dominoes)
status = [0] * n
q = []
for i in range(n):
if dominoes[i]=='L':
q.append((i, -1))
elif dominoes[i]=='R':
... | push-dominoes | O(n) using bfs and simulation with two sets | dntai | 0 | 21 | push dominoes | 838 | 0.57 | Medium | 13,622 |
https://leetcode.com/problems/push-dominoes/discuss/1879797/Python-easy-to-read-and-understand-or-Brute-Force | class Solution:
def pushDominoes(self, dominoes: str) -> str:
q = []
dom = list(dominoes)
n = len(dom)
for i, pos in enumerate(dom):
if pos == "L" or pos == "R":
q.append((i, pos))
while q:
i, pos = q.pop(0)
if pos == "L":... | push-dominoes | Python easy to read and understand | Brute-Force | sanial2001 | 0 | 51 | push dominoes | 838 | 0.57 | Medium | 13,623 |
https://leetcode.com/problems/push-dominoes/discuss/1354784/3-pass-simple-greater-calculate-the-time-when-each-domino-is-hit-by-each-force | class Solution:
def pushDominoes(self, doms: str) -> str:
n = len(doms)
doms = list(doms)
R, L = [0]*n, [0]*n
# at which second R force comes and L force comes
for i, d in enumerate(doms):
if d == 'R':
R[i] = 1
elif i and d ==... | push-dominoes | 3 pass simple -> calculate the time when each domino is hit by each force | yozaam | 0 | 59 | push dominoes | 838 | 0.57 | Medium | 13,624 |
https://leetcode.com/problems/push-dominoes/discuss/1353938/Python3-greedy | class Solution:
def pushDominoes(self, dominoes: str) -> str:
mp = [0]*len(dominoes)
ii = len(dominoes)
for i in reversed(range(len(dominoes))):
if dominoes[i] != ".": ii = i
mp[i] = ii
ans = []
ii = -1
for i, x in enumerate(do... | push-dominoes | [Python3] greedy | ye15 | 0 | 48 | push dominoes | 838 | 0.57 | Medium | 13,625 |
https://leetcode.com/problems/similar-string-groups/discuss/2698654/My-Python-Union-Find-Solution | class Solution:
def numSimilarGroups(self, strs: List[str]) -> int:
N = len(strs)
parent = [i for i in range(N)]
depth = [1 for _ in range(N)]
def find(idx):
if idx != parent[idx]:
return find(parent[idx])
return idx
def union... | similar-string-groups | My Python Union Find Solution | MonQiQi | 1 | 99 | similar string groups | 839 | 0.478 | Hard | 13,626 |
https://leetcode.com/problems/similar-string-groups/discuss/1364678/Python-Connected-Components-using-BFS | class Solution:
def numSimilarGroups(self, strs: List[str]) -> int:
def isSimilar(x, y):
if x == y:
return True
x = [i for i in x]
y = [i for i in y]
# Save the index where x[i] != y[i]
idx = []
for i in range(len(x)):
... | similar-string-groups | [Python] Connected Components using BFS | mizan-ali | 1 | 180 | similar string groups | 839 | 0.478 | Hard | 13,627 |
https://leetcode.com/problems/similar-string-groups/discuss/2845385/python-union-find | class Solution:
def numSimilarGroups(self, strs: List[str]) -> int:
def checksimilar(a, b):
cnt = 0
for a_, b_ in zip(a, b):
if a_ != b_:
cnt += 1
return cnt <= 2
u = [i for i in range(len(strs))]
def find_root(i):
... | similar-string-groups | python union find | xsdnmg | 0 | 2 | similar string groups | 839 | 0.478 | Hard | 13,628 |
https://leetcode.com/problems/similar-string-groups/discuss/2745972/96-fast-python-sol | class Solution:
def numSimilarGroups(self, strs: List[str]) -> int:
l=len(strs)
self.rank=[1 for i in range(l)]
group=[i for i in range(l)]
p=len(strs[0])
def issimilar(i,j):
ct=0
for a,b in zip(i,j):
ct+=(a!=b)
... | similar-string-groups | 96% fast python sol | RjRahul003 | 0 | 5 | similar string groups | 839 | 0.478 | Hard | 13,629 |
https://leetcode.com/problems/similar-string-groups/discuss/2081702/Python-Graph-%2B-BFS | class Solution:
def numSimilarGroups(self, strs: List[str]) -> int:
def similar(word1, word2):
diff = []
for a,b in zip(word1, word2):
if a != b:
diff.append((a,b))
if diff and len(diff) > 2:
return False... | similar-string-groups | Python Graph + BFS | remy1991 | 0 | 32 | similar string groups | 839 | 0.478 | Hard | 13,630 |
https://leetcode.com/problems/magic-squares-in-grid/discuss/381223/Two-Solutions-in-Python-3-(beats-~99)-(two-lines) | class Solution:
def numMagicSquaresInside(self, G: List[List[int]]) -> int:
M, N, S, t = len(G)-2, len(G[0])-2, {(8,1,6,3,5,7,4,9,2),(6,1,8,7,5,3,2,9,4),(2,7,6,9,5,1,4,3,8),(6,7,2,1,5,9,8,3,4)}, range(3)
return sum((lambda x: x in S or x[::-1] in S)(tuple(sum([G[i+k][j:j+3] for k in t],[]))) for i,j in it... | magic-squares-in-grid | Two Solutions in Python 3 (beats ~99%) (two lines) | junaidmansuri | 1 | 624 | magic squares in grid | 840 | 0.385 | Medium | 13,631 |
https://leetcode.com/problems/magic-squares-in-grid/discuss/381223/Two-Solutions-in-Python-3-(beats-~99)-(two-lines) | class Solution:
def numMagicSquaresInside(self, G: List[List[int]]) -> int:
M, N, S, t, s = len(G), len(G[0]), set(range(1,10)), range(3), 0
for i in range(M-2):
for j in range(N-2):
g = [G[i+k][j:j+3] for k in t]
if set(sum(g,[])) != S or g[1][1] != 5: continue
if any(sum(g[k])... | magic-squares-in-grid | Two Solutions in Python 3 (beats ~99%) (two lines) | junaidmansuri | 1 | 624 | magic squares in grid | 840 | 0.385 | Medium | 13,632 |
https://leetcode.com/problems/magic-squares-in-grid/discuss/1419104/Straightforward-98-speed | class Solution:
digits = {1, 2, 3, 4, 5, 6, 7, 8, 9}
@classmethod
def magic_3_3(cls, square: List[List[int]]) -> bool:
if set(sum(square, [])) != Solution.digits:
return False
sum_row0 = sum(square[0])
for r in range(1, 3):
if sum(square[r]) != sum_row0:
... | magic-squares-in-grid | Straightforward, 98% speed | EvgenySH | 0 | 317 | magic squares in grid | 840 | 0.385 | Medium | 13,633 |
https://leetcode.com/problems/magic-squares-in-grid/discuss/938258/Python3-beating-99.32 | class Solution:
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0]) # dimension
def fn(i, j):
"""Return True if grid[i-1:i+2][j-1:j+2] is a magic squre."""
seen = set()
row, col = [0]*3, [0]*3 # row sum & co... | magic-squares-in-grid | [Python3] beating 99.32% | ye15 | -1 | 142 | magic squares in grid | 840 | 0.385 | Medium | 13,634 |
https://leetcode.com/problems/keys-and-rooms/discuss/1116836/Python3-Soln-greater-Keys-and-Rooms-stack-implementation | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited_rooms = set()
stack = [0] # for rooms that we need to visit and we start from room [0]
while stack:
room = stack.pop()
visited_rooms.add(room)
for key in rooms[r... | keys-and-rooms | [Python3] Soln -> Keys and Rooms [stack implementation] | avEraGeC0der | 12 | 620 | keys and rooms | 841 | 0.702 | Medium | 13,635 |
https://leetcode.com/problems/keys-and-rooms/discuss/2292352/Python3-DFS | class Solution:
def visitAll(self,rooms,index,visited):
if index not in visited:
visited.add(index)
for i in rooms[index]:
self.visitAll(rooms,i,visited)
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = set()
self.vis... | keys-and-rooms | 📌 Python3 DFS | Dark_wolf_jss | 2 | 34 | keys and rooms | 841 | 0.702 | Medium | 13,636 |
https://leetcode.com/problems/keys-and-rooms/discuss/1756839/Python-or-Simple-DFS-%2B-BFS-or-Explained-w-Complexity | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
n = len(rooms)
seen = [False] * n
stack = [0] # room 0 is unlocked
while stack:
room = stack.pop()
if not seen[room]: # if not previously visited
seen[room] = True
... | keys-and-rooms | Python | Simple DFS + BFS | Explained w/ Complexity | leetbeet73 | 2 | 65 | keys and rooms | 841 | 0.702 | Medium | 13,637 |
https://leetcode.com/problems/keys-and-rooms/discuss/2159415/Python3-Runtime%3A-96ms-48.98-memory%3A-14.4mb-85.04 | class Solution:
# Runtime: 96ms 48.98% memory: 14.4mb 85.04%
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
seen = set()
stack = [0]
seen.add(stack[-1])
while stack:
cur = stack.pop()
for neigh in rooms[cur]:
if not ne... | keys-and-rooms | Python3 Runtime: 96ms 48.98% memory: 14.4mb 85.04% | arshergon | 1 | 50 | keys and rooms | 841 | 0.702 | Medium | 13,638 |
https://leetcode.com/problems/keys-and-rooms/discuss/938340/Python3-dfs-O(N) | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
seen = [False]*len(rooms)
stack = [0]
while stack:
n = stack.pop()
if not seen[n]:
seen[n] = True
stack.extend(rooms[n])
return all(seen) | keys-and-rooms | [Python3] dfs O(N) | ye15 | 1 | 66 | keys and rooms | 841 | 0.702 | Medium | 13,639 |
https://leetcode.com/problems/keys-and-rooms/discuss/858375/Python3-BFS-Iterative | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
if len(rooms) == 0: return True
visited = {0}
queue = deque([0])
while queue:
cur = queue.popleft()
if cur > len(rooms): continue
for key in rooms[cur]:
if ... | keys-and-rooms | [Python3] BFS Iterative | nachiketsd | 1 | 46 | keys and rooms | 841 | 0.702 | Medium | 13,640 |
https://leetcode.com/problems/keys-and-rooms/discuss/2847364/Easiest-ever | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
seen = set()
queue = deque()
queue.append(0)
seen.add(0)
while queue:
current_room = queue.popleft()
for room_keys in rooms[current_room]:
if room_keys not in ... | keys-and-rooms | Easiest ever | shriyansnaik | 0 | 1 | keys and rooms | 841 | 0.702 | Medium | 13,641 |
https://leetcode.com/problems/keys-and-rooms/discuss/2833972/Easy-python-solution-using-BFS | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
vis = [0] * len(rooms)
q = collections.deque()
q.append(rooms[0])
vis[0] = 1
while q:
for _ in range(len(q)):
keys = q.popleft()
for key in keys:
... | keys-and-rooms | Easy python solution using BFS | i-haque | 0 | 2 | keys and rooms | 841 | 0.702 | Medium | 13,642 |
https://leetcode.com/problems/keys-and-rooms/discuss/2817635/BFS-or-Python-Solution | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
N = len(rooms)
visited = []
queue = deque([0])
while queue:
curr_room = queue.popleft()
if curr_room not in visited:
visited.append(curr_room)
if len(vis... | keys-and-rooms | BFS | Python Solution | gautham0505 | 0 | 3 | keys and rooms | 841 | 0.702 | Medium | 13,643 |
https://leetcode.com/problems/keys-and-rooms/discuss/2787973/Python-oror-DFS-implementation-oror-No-recursion-needed. | class Solution:
def canVisitAllRooms(self, rooms: list[list[int]]) -> bool:
stack =[rooms[0]]
visit = set()
visit.add(0)
while stack:
nums = stack.pop()
for i in nums:
if i not in visit:
stack.append(rooms[i])
... | keys-and-rooms | Python || DFS implementation || No recursion needed. | khoai345678 | 0 | 3 | keys and rooms | 841 | 0.702 | Medium | 13,644 |
https://leetcode.com/problems/keys-and-rooms/discuss/2769605/Python-solution-or-BFS | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
q = deque()
visited = [0]
for i in range(len(rooms[0])):
q.append(rooms[0][i])
while q:
key = q.popleft()
if key not in visited:
... | keys-and-rooms | Python solution | BFS | maomao1010 | 0 | 3 | keys and rooms | 841 | 0.702 | Medium | 13,645 |
https://leetcode.com/problems/keys-and-rooms/discuss/2764878/DFS-with-set-instead-of-stack-(beats-95) | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = set([0])
keys = set(rooms[0])
while keys:
key = keys.pop()
visited.add(key)
for new_key in rooms[key]:
if new_key not in visited:
... | keys-and-rooms | DFS with set instead of stack (beats 95%) | ivan-luchko | 0 | 1 | keys and rooms | 841 | 0.702 | Medium | 13,646 |
https://leetcode.com/problems/keys-and-rooms/discuss/2567513/Clean-Fast-Python3-or-BFS | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
q, visited = deque([0]), {0}
while q:
cur = q.pop()
for nxt in rooms[cur]:
if nxt not in visited:
visited.add(nxt)
q.appendleft(nxt)
ret... | keys-and-rooms | Clean, Fast Python3 | BFS | ryangrayson | 0 | 14 | keys and rooms | 841 | 0.702 | Medium | 13,647 |
https://leetcode.com/problems/keys-and-rooms/discuss/2500490/Easy-python-DFS-solution | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
graph = defaultdict(lambda: [])
all_nodes = set()
for index, val in enumerate(rooms):
all_nodes.add(index)
graph[index] = val
visited = set()
def travel(graph, start):
... | keys-and-rooms | Easy python DFS solution | prameshbajra | 0 | 21 | keys and rooms | 841 | 0.702 | Medium | 13,648 |
https://leetcode.com/problems/keys-and-rooms/discuss/2421252/Keys-and-Rooms-oror-Python3-oror-Stack | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
seen = [False] * len(rooms)
stack = [0]
seen[0] = True
while(len(stack)> 0):
el = stack.pop()
seen[el] = True
for key in rooms[el]:
if see... | keys-and-rooms | Keys and Rooms || Python3 || Stack | vanshika_2507 | 0 | 11 | keys and rooms | 841 | 0.702 | Medium | 13,649 |
https://leetcode.com/problems/keys-and-rooms/discuss/2357335/Python3-or-Efficient-Python3-Solution-using-BFS-%2B-Queue | class Solution:
#Time-Complexity: O(n + n^2) -> O(n^2)
#Space-Complexity: O(n + n + n) -> O(n)
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
q = collections.deque()
number_of_rooms = len(rooms)
#if we visited every room, our visited set will match wanted_set!
... | keys-and-rooms | Python3 | Efficient Python3 Solution using BFS + Queue | JOON1234 | 0 | 17 | keys and rooms | 841 | 0.702 | Medium | 13,650 |
https://leetcode.com/problems/keys-and-rooms/discuss/2285234/Simple-DFS-Python | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = set()
stack = [0]
while stack:
vertex = stack.pop()
visited.add(vertex)
for key in rooms[vertex]:
if key not in visited:
stack.append(... | keys-and-rooms | Simple DFS Python | baz-gaul | 0 | 6 | keys and rooms | 841 | 0.702 | Medium | 13,651 |
https://leetcode.com/problems/keys-and-rooms/discuss/2237876/Easy-DFS-Approach-oror-Clean-Code | class Solution:
def dfs(self, rooms, graph, idx, visited):
visited[idx] = 1
for neighbour in graph[idx]:
if visited[neighbour] == 0:
visited = self.dfs(rooms, graph, neighbour, visited)
return visited
def canVisitAllRooms(self, rooms: List[List[i... | keys-and-rooms | Easy DFS Approach || Clean Code | Vaibhav7860 | 0 | 36 | keys and rooms | 841 | 0.702 | Medium | 13,652 |
https://leetcode.com/problems/keys-and-rooms/discuss/2202418/Python-3-or-BFS-to-verify-if-all-room-is-accessible | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
seen, q = set(), deque([0])
while q:
r = q.popleft()
seen.add(r)
q += [key for key in rooms[r] if key not in seen]
return len(seen) == len(rooms) | keys-and-rooms | Python 3 | BFS to verify if all room is accessible | Ploypaphat | 0 | 19 | keys and rooms | 841 | 0.702 | Medium | 13,653 |
https://leetcode.com/problems/keys-and-rooms/discuss/2175988/Python-or-BFS | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
access = [True] + [False] * (len(rooms) - 1)
print(access)
q = []
q.extend(rooms[0])
while q:
room_no = q.pop(0)
if access[room_no]:
continue
... | keys-and-rooms | Python | BFS | tejeshreddy111 | 0 | 13 | keys and rooms | 841 | 0.702 | Medium | 13,654 |
https://leetcode.com/problems/keys-and-rooms/discuss/2049594/Python-3-greater-BFS-99-faster | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
if not rooms:
return 1
return self.helperBFS(rooms)
def helperBFS(self, rooms):
visited = [False] * len(rooms)
queue = collections.deque([0])
visited[0] = True
wh... | keys-and-rooms | Python 3 -> BFS 99% faster | mybuddy29 | 0 | 28 | keys and rooms | 841 | 0.702 | Medium | 13,655 |
https://leetcode.com/problems/keys-and-rooms/discuss/2036810/easy-and-efficient-2-python-solutions | class Solution:
def canVisitAllRooms(self, graph: List[List[int]]) -> bool:
def dfs(node) :
if node not in seen :
seen.add(node)
for v in graph[node] :
dfs(v)
seen = set()
dfs(0)
return False if len(seen) < len... | keys-and-rooms | easy and efficient 2 python solutions | runtime-terror | 0 | 41 | keys and rooms | 841 | 0.702 | Medium | 13,656 |
https://leetcode.com/problems/keys-and-rooms/discuss/2036810/easy-and-efficient-2-python-solutions | class Solution:
def canVisitAllRooms(self, graph: List[List[int]]) -> bool:
seen = set()
q = deque([0])
while q :
node = q.popleft()
seen.add(node)
for v in graph[node] :
if v not in seen :
q.append(v)
return Fal... | keys-and-rooms | easy and efficient 2 python solutions | runtime-terror | 0 | 41 | keys and rooms | 841 | 0.702 | Medium | 13,657 |
https://leetcode.com/problems/keys-and-rooms/discuss/1916999/Python3-or-Queue-or-BFS-or-Easy-to-understand-or-Faster-than-90 | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
n, q = len(rooms) - 1, [0]
vis = [False] * (n + 1)
while q:
temp = q.pop(0)
while not vis[temp]:
vis[temp] = True
for i in rooms[temp]:
if ... | keys-and-rooms | Python3 | Queue | BFS | Easy to understand | Faster than 90% | milannzz | 0 | 15 | keys and rooms | 841 | 0.702 | Medium | 13,658 |
https://leetcode.com/problems/keys-and-rooms/discuss/1890681/Easy-to-understand-BFS-approach | class Solution:
from collections import deque
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
if rooms==[]:
return True
if rooms[0]==[]:
return False
que = deque()
visited = set()
visited.add(0)
for i in... | keys-and-rooms | Easy to understand BFS approach | gamitejpratapsingh998 | 0 | 27 | keys and rooms | 841 | 0.702 | Medium | 13,659 |
https://leetcode.com/problems/keys-and-rooms/discuss/1868676/Python-BFS-Approach | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
access = [False] * len(rooms)
access[0] = True
queue = []
queue.extend(rooms[0])
while queue:
for i in range(len(queue)):
room_no = queue.pop(0)
... | keys-and-rooms | [Python] BFS Approach | tejeshreddy111 | 0 | 13 | keys and rooms | 841 | 0.702 | Medium | 13,660 |
https://leetcode.com/problems/keys-and-rooms/discuss/1846732/Python-l-Iterative-BFS-using-Queue | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
n = len(rooms)
graph = defaultdict(list)
for i in range(n):
for keys in rooms[i]:
graph[i].append(keys)
Q = deque([0])
visited = set()
while Q:
vertex = Q.popleft()
if vertex in visited: continue
visited.add(verte... | keys-and-rooms | Python l Iterative BFS using Queue | morpheusdurden | 0 | 18 | keys and rooms | 841 | 0.702 | Medium | 13,661 |
https://leetcode.com/problems/keys-and-rooms/discuss/1815668/Python-dfs-Easy-to-understand | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
# you have to start somewhere (room 0 in this case) so the key to that room is always collected
def backtrack(currentRoomKey=0, keysCollected=set([0])):
# We know how many rooms there are, s... | keys-and-rooms | Python dfs - Easy to understand | Rush_P | 0 | 26 | keys and rooms | 841 | 0.702 | Medium | 13,662 |
https://leetcode.com/problems/keys-and-rooms/discuss/1690654/Easy-to-Understand-and-Fast-Python-Solution | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
n = len(rooms)
roomsToCheck = [0]
visitedCount = 0
visitedArr = [0] * n
while roomsToCheck:
room = roomsToCheck.pop()
... | keys-and-rooms | Easy to Understand and Fast Python Solution | josejassojr | 0 | 38 | keys and rooms | 841 | 0.702 | Medium | 13,663 |
https://leetcode.com/problems/keys-and-rooms/discuss/1659070/BFS-Python-faster-than-70 | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
graph={}
for i,val in enumerate(rooms):
graph[i]=val
print(graph)
visited=[]
queue=[]
def bfs(node):
visited.append(node)
queue.append(node)
... | keys-and-rooms | BFS Python faster than 70% | naren_nadig | 0 | 23 | keys and rooms | 841 | 0.702 | Medium | 13,664 |
https://leetcode.com/problems/keys-and-rooms/discuss/1607775/Python3-Intuitive-BFS-Solution-for-Beginners | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = set()
q = collections.deque()
start = rooms[0]
if len(start) > 0:
q.append(start)
visited.add(0)
# if the room 0 has no key at all, return false directly
e... | keys-and-rooms | Python3 Intuitive BFS Solution for Beginners | Hauptwaffenamt | 0 | 40 | keys and rooms | 841 | 0.702 | Medium | 13,665 |
https://leetcode.com/problems/keys-and-rooms/discuss/1580929/Python3-Solution | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = [False]*len(rooms)
def dfs(roomNo):
if visited[roomNo] : return
visited[roomNo] = True
for i in rooms[roomNo]: dfs(i)
dfs(0)
return all(visited) | keys-and-rooms | Python3 Solution | satyam2001 | 0 | 61 | keys and rooms | 841 | 0.702 | Medium | 13,666 |
https://leetcode.com/problems/keys-and-rooms/discuss/1558943/Python3-solution-comments-or-easy-to-read | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
def dfs(rooms, arr, reachable, index): #arr = current room
reachable.add(index) # current index | room where we are at
for i in range(len(arr)):
if arr[i] not in reachable:
... | keys-and-rooms | Python3 solution comments | easy to read | FlorinnC1 | 0 | 38 | keys and rooms | 841 | 0.702 | Medium | 13,667 |
https://leetcode.com/problems/keys-and-rooms/discuss/1440207/Simple-Python-recursive-dfs | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
def dfs(cur_room):
for key in rooms[cur_room]:
if not visited[key]:
visited[key] = True
dfs(key)
visited = [True]+[False]*(len(rooms)-1)
df... | keys-and-rooms | Simple Python recursive dfs | Charlesl0129 | 0 | 59 | keys and rooms | 841 | 0.702 | Medium | 13,668 |
https://leetcode.com/problems/keys-and-rooms/discuss/1301846/Python3-simple-solution | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = [0]
keys = rooms[0]
while keys:
x = keys.pop(0)
if x not in visited:
visited.append(x)
else:
continue
for i in rooms[x]:
... | keys-and-rooms | Python3 simple solution | EklavyaJoshi | 0 | 54 | keys and rooms | 841 | 0.702 | Medium | 13,669 |
https://leetcode.com/problems/keys-and-rooms/discuss/1299064/Python-solution-for-record-purpose | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = set()
visited.add(0)
stack = [0]
while stack:
i = stack.pop()
for key in rooms[i]:
if key not in visited:
visited.add(key)
... | keys-and-rooms | Python solution for record purpose | konnomiya | 0 | 19 | keys and rooms | 841 | 0.702 | Medium | 13,670 |
https://leetcode.com/problems/keys-and-rooms/discuss/1169857/Python-DFS-using-Set. | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited = set()
def dfs(room):
if room in visited:
return
visited.add(room)
for r in rooms[room]:
dfs(r)
dfs(0)
return len(visited) == len(r... | keys-and-rooms | Python DFS using Set. | reyna_main | 0 | 76 | keys and rooms | 841 | 0.702 | Medium | 13,671 |
https://leetcode.com/problems/keys-and-rooms/discuss/1149776/Python3-solution-(using-stack) | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
seen: List[bool] = [False] * len(rooms)
seen[0] = True
keys: List[int] = [*rooms[0]]
while keys:
cur_key: int = keys.pop()
seen[cur_key] = True
... | keys-and-rooms | Python3 solution (using stack) | alexforcode | 0 | 23 | keys and rooms | 841 | 0.702 | Medium | 13,672 |
https://leetcode.com/problems/keys-and-rooms/discuss/1117910/Python-Recursive-Solution-Keys-and-Rooms | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
def helper(current_room, visited):
for key in current_room:
if key not in visited:
visited.append(key)
helper(rooms[key], visited)
return visit... | keys-and-rooms | Python Recursive Solution - Keys and Rooms | ronald-luo | 0 | 38 | keys and rooms | 841 | 0.702 | Medium | 13,673 |
https://leetcode.com/problems/keys-and-rooms/discuss/1117318/python3-dfs | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
count = 1
keys = rooms[0]
n = len(rooms)
visited = {0: True}
while keys:
key = keys.pop(0)
if key not in visited:
visited[key] = True
c... | keys-and-rooms | python3 dfs | loharvikas13 | 0 | 25 | keys and rooms | 841 | 0.702 | Medium | 13,674 |
https://leetcode.com/problems/keys-and-rooms/discuss/1100704/Easy-Python3-Solution | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
discovered = [0]
for i in discovered:
for j in rooms[i]:
if j not in discovered:
discovered.append(j)
if len(discovered) == len(rooms):
return True
... | keys-and-rooms | Easy Python3 Solution | yash2709 | 0 | 39 | keys and rooms | 841 | 0.702 | Medium | 13,675 |
https://leetcode.com/problems/keys-and-rooms/discuss/1049291/Yet-Another-Simple-Python-Solution | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
n = len(rooms)
visited = [0]*n
def dfs(a_room):
visited[a_room] = 1
for a_key in rooms[a_room]:
if visited[a_key]==0:
dfs(a_key)
d... | keys-and-rooms | Yet Another Simple Python Solution | SaSha59 | 0 | 26 | keys and rooms | 841 | 0.702 | Medium | 13,676 |
https://leetcode.com/problems/keys-and-rooms/discuss/1035865/Python-beats-98-bfs-and-set | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
q=[]
q.append(0)
if len(rooms)==1:
return True
s=set()
s.add(0)
while q:
cur = q.pop(0)
for i in rooms[cur]:
if i not in s:
... | keys-and-rooms | Python beats 98% bfs and set | gauravgoyalll | 0 | 66 | keys and rooms | 841 | 0.702 | Medium | 13,677 |
https://leetcode.com/problems/keys-and-rooms/discuss/650510/Intuitive-approach-by-keep-visited-room-and-list-of-room-to-visit | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
visited_room_set = set()
''' Set to keep visited room'''
next_room_to_visit = [0]
''' List to hold list of room to visit from next round'''
# 1) Visit room and obtain key for next round of ro... | keys-and-rooms | Intuitive approach by keep visited room and list of room to visit | puremonkey2001 | 0 | 20 | keys and rooms | 841 | 0.702 | Medium | 13,678 |
https://leetcode.com/problems/keys-and-rooms/discuss/650510/Intuitive-approach-by-keep-visited-room-and-list-of-room-to-visit | class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
A, B = [0], []
visited_room_set = set()
while A:
B = set([k for r in A for k in rooms[r] if k not in visited_room_set])
visited_room_set.update(A)
A, B = B, []
... | keys-and-rooms | Intuitive approach by keep visited room and list of room to visit | puremonkey2001 | 0 | 20 | keys and rooms | 841 | 0.702 | Medium | 13,679 |
https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/1579510/PYTHON-BACKTRACKING-or-THREE-PROBLEMS-ONE-SOLUTION | class Solution:
def splitIntoFibonacci(self, num: str) -> List[int]:
def dfs(i):
if i>=len(num):
return len(ans)>2
n = 0
for j in range(i, len(num)):
n = n*10 + int(num[j])
if n>2**31: # if number exceeds the ran... | split-array-into-fibonacci-sequence | PYTHON BACKTRACKING | THREE PROBLEMS ONE SOLUTION | hX_ | 1 | 150 | split array into fibonacci sequence | 842 | 0.383 | Medium | 13,680 |
https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/1579510/PYTHON-BACKTRACKING-or-THREE-PROBLEMS-ONE-SOLUTION | class Solution:
def isAdditiveNumber(self, num: str) -> List[int]:
def dfs(i, ans):
if i>=len(num):
return len(ans)>2
n = 0
for j in range(i, len(num)):
n = n*10 + int(num[j])
if len(ans)<2 or (ans[-1]+ans[-2]==n... | split-array-into-fibonacci-sequence | PYTHON BACKTRACKING | THREE PROBLEMS ONE SOLUTION | hX_ | 1 | 150 | split array into fibonacci sequence | 842 | 0.383 | Medium | 13,681 |
https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/1579510/PYTHON-BACKTRACKING-or-THREE-PROBLEMS-ONE-SOLUTION | class Solution:
def splitString(self, s: str) -> bool:
def dfs(i, ans):
if i>=len(s):
return len(ans)>1
n = 0
for j in range(i, len(s)):
n = n*10 + int(s[j])
if len(ans)<1 or (ans[-1]-1==n):
a... | split-array-into-fibonacci-sequence | PYTHON BACKTRACKING | THREE PROBLEMS ONE SOLUTION | hX_ | 1 | 150 | split array into fibonacci sequence | 842 | 0.383 | Medium | 13,682 |
https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/986705/Python3-efficient-brute-force | class Solution:
def splitIntoFibonacci(self, S: str) -> List[int]:
for i in range(1, min(11, len(S))): # 2**31 limit
if S[0] == "0" and i > 1: break
for j in range(i+1, min(i+11, len(S))): # 2**31 limit
if S[i] == "0" and j-i > 1: break
x, y = int(... | split-array-into-fibonacci-sequence | [Python3] efficient brute-force | ye15 | 1 | 106 | split array into fibonacci sequence | 842 | 0.383 | Medium | 13,683 |
https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/1486859/Python-3-or-Simulation-or-Explanation | class Solution:
def splitIntoFibonacci(self, num: str) -> List[int]:
two_31 = 2 ** 31
n = len(num)
def fibo(a, b, j):
nonlocal n
cur = []
while j < n:
a, b = b, a+b
if b > two_31: return []
b_str = str(b)
... | split-array-into-fibonacci-sequence | Python 3 | Simulation | Explanation | idontknoooo | 0 | 255 | split array into fibonacci sequence | 842 | 0.383 | Medium | 13,684 |
https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/425478/Python-Backtrack-28ms-beats-99.26-easy-understanding | class Solution:
def splitIntoFibonacci(self, S: str) -> List[int]:
res=[]
current=[]
def backtrack(cursor,current): #``cursor'' represents the current scanning cursor, ``current'' represents the current partial result
if len(current)>=3 and cursor==len(S): # reach a re... | split-array-into-fibonacci-sequence | Python Backtrack 28ms beats 99.26%, easy-understanding | wangzi100 | 0 | 177 | split array into fibonacci sequence | 842 | 0.383 | Medium | 13,685 |
https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/353022/Solution-in-Python-3 | class Solution:
def splitIntoFibonacci(self, S: str) -> List[int]:
L, T, t = len(S), "", []
for i in range(1,L-2):
for j in range(1,L-i-1):
if (i > 1 and S[0] == '0') or (j > 1 and S[i] == '0'): continue
a, b = int(S[:i]), int(S[i:i+j])
T, t = S[:i+j], [a,b]
... | split-array-into-fibonacci-sequence | Solution in Python 3 | junaidmansuri | 0 | 312 | split array into fibonacci sequence | 842 | 0.383 | Medium | 13,686 |
https://leetcode.com/problems/guess-the-word/discuss/2385099/Python-Solution-with-narrowed-candidates-and-blacklist | class Solution:
def findSecretWord(self, words: List[str], master: 'Master') -> None:
k = 1 # for tracing the number of loops
matches = 0
blacklists = [[] for i in range(6)]
while matches != 6:
n = len(words)
r = random.randint(0, n - 1)
... | guess-the-word | [Python] Solution with narrowed candidates and blacklist | bbshark | 2 | 203 | guess the word | 843 | 0.418 | Hard | 13,687 |
https://leetcode.com/problems/guess-the-word/discuss/1552899/Reduce-by-Hamming-distance.-28-ms-faster-than-91.22-and-14.2-MB-less-than-92.67.-Python-3. | class Solution:
def findSecretWord(self, wordlist: List[str], master: 'Master') -> None:
def hamming_distance(w1: str, w2: str) -> int:
return sum(1 for k in range(6) if w1[k] != w2[k])
current_guess = wordlist[0]
curr_distance = 6 - Master.guess(master, current_guess)
w... | guess-the-word | Reduce by Hamming distance. 28 ms, faster than 91.22% & 14.2 MB, less than 92.67%. Python 3. | timofeybelov | 2 | 525 | guess the word | 843 | 0.418 | Hard | 13,688 |
https://leetcode.com/problems/guess-the-word/discuss/2448974/python-minimax | class Solution:
def findSecretWord(self, words: List[str], master: 'Master') -> None:
def find_hits(wd1, wd2):
return sum(1 for i in range(6) if wd1[i] == wd2[i])
def next_candidate_mini_max():
minimax = ['', sys.maxsize]
for wd1 in words:
... | guess-the-word | python minimax | sinha_meenu | 0 | 218 | guess the word | 843 | 0.418 | Hard | 13,689 |
https://leetcode.com/problems/guess-the-word/discuss/1369019/Python3-shuffle | class Solution:
def findSecretWord(self, wordlist: List[str], master: 'Master') -> None:
shuffle(wordlist) # statistical guarantee to pass
for _ in range(10):
if wordlist:
w = wordlist.pop()
m = master.guess(w)
wordlist = [ww for ww i... | guess-the-word | [Python3] shuffle | ye15 | 0 | 294 | guess the word | 843 | 0.418 | Hard | 13,690 |
https://leetcode.com/problems/guess-the-word/discuss/1610311/Python3-Easy-Solution | class Solution:
def findSecretWord(self, wordlist: List[str], master: 'Master') -> None:
master.guess(master._Master__secret) | guess-the-word | Python3 Easy Solution | description | -2 | 341 | guess the word | 843 | 0.418 | Hard | 13,691 |
https://leetcode.com/problems/backspace-string-compare/discuss/381659/Three-Solutions-in-Python-3-(With-and-Without-Deque-and-Two-Pointer) | class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
a, A = [collections.deque(), collections.deque()], [S,T]
for i in range(2):
for j in A[i]:
if j != '#': a[i].append(j)
elif a[i]: a[i].pop()
return a[0] == a[1] | backspace-string-compare | Three Solutions in Python 3 (With and Without Deque and Two-Pointer) | junaidmansuri | 16 | 2,600 | backspace string compare | 844 | 0.48 | Easy | 13,692 |
https://leetcode.com/problems/backspace-string-compare/discuss/381659/Three-Solutions-in-Python-3-(With-and-Without-Deque-and-Two-Pointer) | class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
s, t = [], []
for i in S: s = s + [i] if i != '#' else s[:-1]
for i in T: t = t + [i] if i != '#' else t[:-1]
return s == t | backspace-string-compare | Three Solutions in Python 3 (With and Without Deque and Two-Pointer) | junaidmansuri | 16 | 2,600 | backspace string compare | 844 | 0.48 | Easy | 13,693 |
https://leetcode.com/problems/backspace-string-compare/discuss/381659/Three-Solutions-in-Python-3-(With-and-Without-Deque-and-Two-Pointer) | class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
a, A = [[],[],0,0], [S,T]
for i in range(2):
for j in A[i][::-1]:
if j != '#':
if a[i+2] == 0: a[i].append(j)
else: a[i+2] -= 1
else: a[i+2] += 1
return a[0] == a[1]
- Junaid Mansuri
(Leet... | backspace-string-compare | Three Solutions in Python 3 (With and Without Deque and Two-Pointer) | junaidmansuri | 16 | 2,600 | backspace string compare | 844 | 0.48 | Easy | 13,694 |
https://leetcode.com/problems/backspace-string-compare/discuss/2727888/Python's-Simple-and-Easy-to-Understand-Solutionor-O(n)-Solution-or-99-Faster | class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
s_backspaced = []
t_backspaced = []
for i in range(len(s)):
if s[i] == '#':
if s_backspaced:
s_backspaced.pop()
else:
s_backspaced.append(s... | backspace-string-compare | ✔️ Python's Simple and Easy to Understand Solution| O(n) Solution | 99% Faster 🔥 | pniraj657 | 11 | 639 | backspace string compare | 844 | 0.48 | Easy | 13,695 |
https://leetcode.com/problems/backspace-string-compare/discuss/570675/PythonJSJavaC%2B%2B-O(-n-)-sol-by-stack.-w-Comment | class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
stack_s, stack_t = [], []
# --------------------------------------
def final_string( stk, string ):
for char in string:
if char != '#':
# push ... | backspace-string-compare | Python/JS/Java/C++ O( n ) sol by stack. [w/ Comment] | brianchiang_tw | 9 | 1,100 | backspace string compare | 844 | 0.48 | Easy | 13,696 |
https://leetcode.com/problems/backspace-string-compare/discuss/1997156/Python-Clean-and-Simple! | class Solution:
def backspaceCompare(self, s, t):
return self.parse(s) == self.parse(t)
def parse(self, x):
res = []
for c in x:
if c != "#":
res.append(c)
else:
if res: res.pop()
return res | backspace-string-compare | Python - Clean and Simple! | domthedeveloper | 7 | 758 | backspace string compare | 844 | 0.48 | Easy | 13,697 |
https://leetcode.com/problems/backspace-string-compare/discuss/1997156/Python-Clean-and-Simple! | class Solution:
def backspaceCompare(self, s, t):
i, j = len(s), len(t)
while i >= 0 and j >= 0:
delete = 1
while delete: i -= 1; delete += 1 if i >= 0 and s[i] == '#' else -1
delete = 1
while delete: j -= 1; delete += 1 if j >= 0... | backspace-string-compare | Python - Clean and Simple! | domthedeveloper | 7 | 758 | backspace string compare | 844 | 0.48 | Easy | 13,698 |
https://leetcode.com/problems/backspace-string-compare/discuss/1997849/Simple-stack-implementation-in-python-with-error-handling | class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
stack1=[]
stack2=[]
for i in range(len(s)):
try:
if s[i]=="#":
stack1.pop()
else:
stack1.append(s[i])
except:
co... | backspace-string-compare | Simple stack implementation in python with error handling | amannarayansingh10 | 3 | 204 | backspace string compare | 844 | 0.48 | Easy | 13,699 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.