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/number-of-matching-subsequences/discuss/932263/Python3-two-approaches | class Solution:
def numMatchingSubseq(self, S: str, words: List[str]) -> int:
def fn(word):
"""Return True if word is subsequence of S."""
k = 0
for ch in word:
k = S.find(ch, k) + 1
if not k: return False
return Tru... | number-of-matching-subsequences | [Python3] two approaches | ye15 | 1 | 198 | number of matching subsequences | 792 | 0.519 | Medium | 12,900 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2612921/Simple-Python-solution-with-Hashmap-and-pointers | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
index = dict()
for i in range(len(s)):
if (s[i] not in index):
index[s[i]] = []
index[s[i]].append(i)
res = 0
for word in words:
# pointer on wor... | number-of-matching-subsequences | Simple Python solution with Hashmap and pointers | leqinancy | 0 | 8 | number of matching subsequences | 792 | 0.519 | Medium | 12,901 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2605158/Python-or-Binary-Search-or-O(mlogn) | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
res = 0
pos = defaultdict(list)
for i, c in enumerate(s):
pos[c].append(i)
for word in words:
i, j = 0, 0
for w in word:
indexes = pos[w... | number-of-matching-subsequences | Python | Binary Search | O(mlogn) | Kiyomi_ | 0 | 21 | number of matching subsequences | 792 | 0.519 | Medium | 12,902 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2333683/Easy-Python-Solution-from-IsSubsequence-Problem-Enhancement | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
i = 0
j = 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
i+=1
j+=1
return 1 if i == len(s) else 0
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
... | number-of-matching-subsequences | Easy Python Solution from IsSubsequence Problem Enhancement | shubhamnagota | 0 | 64 | number of matching subsequences | 792 | 0.519 | Medium | 12,903 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2315451/Python3-For-Loops-90-Run-Time | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
rt = 0
for word in words: # to go through each word and check if it matches
prev = -1 # to record index of last letter, -1 for the 1st letter
m = len(word)
for ii in range(m):... | number-of-matching-subsequences | Python3 For Loops, 90% Run Time | wwwhhh1988 | 0 | 9 | number of matching subsequences | 792 | 0.519 | Medium | 12,904 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2312309/Python-Binary-Search-2-Approach | class Solution(object):
def numMatchingSubseq(self, s, words):
wordMap = collections.defaultdict(list)
for i,ch in enumerate(s):
wordMap[ch].append(i)
def is_lcs(word):
start = 0
for ch in word:
arr = wordMap[ch]
i... | number-of-matching-subsequences | Python Binary Search 2 Approach | Abhi_009 | 0 | 18 | number of matching subsequences | 792 | 0.519 | Medium | 12,905 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2312054/Python3-Easy-to-Understand-or-Faster-than-98 | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
return sum((self.isSub(s, word) for word in words))
def isSub(self, s: str, word: str) -> int:
ind = 0
for char in word:
ind = s.find(char, ind) + 1
if ind == 0:
ret... | number-of-matching-subsequences | ✅Python3 Easy to Understand | Faster than 98% | thesauravs | 0 | 3 | number of matching subsequences | 792 | 0.519 | Medium | 12,906 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2308085/feel-top-votes-hard-to-understand-so-I-figure-out-this-stupid-but-easy-to-understand-method | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
# count of match
count = 0
#build a hash map for s index
d = defaultdict(list)
for i, a in enumerate(s):
d[a].append(i)
for sub in words:
curind = -1# current index
... | number-of-matching-subsequences | feel top votes hard to understand, so I figure out this stupid but easy to understand method | geng0021 | 0 | 13 | number of matching subsequences | 792 | 0.519 | Medium | 12,907 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2307869/100-C%2B%2B-Java-and-Python-Optimal-Solution | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
root = {}
def insert(word: str) -> None:
node = root
for c in word:
if c not in node:
node[c] = {'count': 0}
node = node[c]
node['count'] += 1
for word in words:
insert(word)
... | number-of-matching-subsequences | ✔️ 100% - C++, Java and Python Optimal Solution | Theashishgavade | 0 | 46 | number of matching subsequences | 792 | 0.519 | Medium | 12,908 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2307651/Python-solutions-%3A-Trie-%2B-DFS-and-Hashmap | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
def isSubSequence(source,word):
wordIndex = 0
for chrSoruce in source:
if word[wordIndex] == chrSoruce:
wordIndex+=1
if wordIndex == len(word):
... | number-of-matching-subsequences | Python solutions : Trie + DFS and Hashmap | manojkumarmanusai | 0 | 97 | number of matching subsequences | 792 | 0.519 | Medium | 12,909 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2307542/GolangPython-Solutions | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
visited = {}
counter = 0
for word in words:
if word in visited:
if visited[word] is True:
counter+=1
continue
idx = 0
for lett... | number-of-matching-subsequences | Golang/Python Solutions | vtalantsev | 0 | 27 | number of matching subsequences | 792 | 0.519 | Medium | 12,910 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2306766/Python-Simple-Python-Solution-Using-Dictionary-(-HashMap-) | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
d = {}
for index in range(len(s)):
if s[index] not in d:
d[s[index]] = [index]
else:
d[s[index]].append(index)
result = []
for word in words:
check = 0
current_index = -1
for char in word:
if char n... | number-of-matching-subsequences | [ Python ] ✅✅ Simple Python Solution Using Dictionary ( HashMap ) 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 83 | number of matching subsequences | 792 | 0.519 | Medium | 12,911 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2083517/Clean-Intuitive-Python-with-Heads-Hashmap | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
heads = defaultdict(list)
for word in words:
heads[word[0]].append(word)
ans = 0
for c in s:
strs = heads[c]
heads[c] = []
for part in strs:
... | number-of-matching-subsequences | Clean, Intuitive Python with Heads Hashmap | boris17 | 0 | 120 | number of matching subsequences | 792 | 0.519 | Medium | 12,912 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2027394/binary-search-and-hash-table | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
def binary_search(positions, target):
l = 0
r = len(positions) - 1
while l <= r:
mid = (l + r) // 2
if positions[mid] < target:
l = mid + 1
... | number-of-matching-subsequences | binary search and hash table | Mujojo | 0 | 88 | number of matching subsequences | 792 | 0.519 | Medium | 12,913 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1848857/Python3-Simple-python-code-with-index-faster-than-97 | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
def isSubsequence(s, word):
prev_idx = 0
for i in range(len(word)):
try:
tmp = s.index(word[i], prev_idx)
prev_idx = tmp + 1
... | number-of-matching-subsequences | [Python3] Simple python code with index - faster than 97% | user7555GI | 0 | 262 | number of matching subsequences | 792 | 0.519 | Medium | 12,914 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1813350/Python-easy-to-read-and-understand-or-is-subsequence | class Solution:
def isSubsequence(self, s: str, t: str):
if len(s) == 0:
return True
i, j = 0, 0
while i < len(t):
if t[i] == s[j]:
j = j+1
if j == len(s):
return 1
i = i+1
return 0
def n... | number-of-matching-subsequences | Python easy to read and understand | is-subsequence | sanial2001 | 0 | 173 | number of matching subsequences | 792 | 0.519 | Medium | 12,915 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1496822/Building-Intuition-Step-by-Step-greater-Brute-Force-to-Accepted-Solution | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
#A brute force thinking - Generate all the subsequences of s and store them in an array
#find how many of them occurs in the given array "words", count them and return them
#But as we know, for a string of... | number-of-matching-subsequences | Building Intuition Step by Step --> Brute Force to Accepted Solution | aarushsharmaa | 0 | 147 | number of matching subsequences | 792 | 0.519 | Medium | 12,916 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1290408/python3-Binary-search-and-index-hopping-sol-for-reference. | class Solution:
def numMatchingSubseq(self, s: str, words) -> int:
cnt = 0
for w in words:
iter = 0
for c in w:
next_idx = s[iter:].find(c)
if next_idx < 0:
iter = -1
break
iter += next_id... | number-of-matching-subsequences | [python3] Binary search & index hopping sol for reference. | vadhri_venkat | 0 | 74 | number of matching subsequences | 792 | 0.519 | Medium | 12,917 |
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1290408/python3-Binary-search-and-index-hopping-sol-for-reference. | class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
def isSubSeq(s1, s2):
iter = 0
for index, val in enumerate(s1):
if val == s2[iter]:
iter += 1
if iter == len(s2):
... | number-of-matching-subsequences | [python3] Binary search & index hopping sol for reference. | vadhri_venkat | 0 | 74 | number of matching subsequences | 792 | 0.519 | Medium | 12,918 |
https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/discuss/1306028/Python3-binary-search | class Solution:
def preimageSizeFZF(self, k: int) -> int:
lo, hi = 0, 1 << 32
while lo <= hi:
mid = lo + hi >> 1
x, y = mid, 0
while x:
x //= 5
y += x
if y < k: lo = mid + 1
elif y > k: hi = mid - 1
... | preimage-size-of-factorial-zeroes-function | [Python3] binary search | ye15 | 1 | 69 | preimage size of factorial zeroes function | 793 | 0.428 | Hard | 12,919 |
https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/discuss/1713153/PYTHON-FASTEST-SOLUTION-TILL-NOW-Faster-than-100-of-Python-Submissions | class Solution:
def findzeroes(self,num):
# This part takes log(n) time
tmp=0
val=5
while val<=num:
tmp+=num//val
val*=5
return tmp
def preimageSizeFZF(self, k: int) -> int:
if k==0:return 5
high=5
while True:
t... | preimage-size-of-factorial-zeroes-function | PYTHON FASTEST SOLUTION TILL NOW Faster than 100% of Python Submissions | reaper_27 | 0 | 104 | preimage size of factorial zeroes function | 793 | 0.428 | Hard | 12,920 |
https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/discuss/1529910/Binary-search-80-speed | class Solution:
def preimageSizeFZF(self, k: int) -> int:
if k < 5:
return 5
elif k == 5:
return 0
left, right = 4, 5 * k
while left < right:
middle = (left + right) // 2
zeros = sum(middle // pow(5, p)
for p in... | preimage-size-of-factorial-zeroes-function | Binary search, 80% speed | EvgenySH | 0 | 112 | preimage size of factorial zeroes function | 793 | 0.428 | Hard | 12,921 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/2269469/Python3-oror-int-array-7-lines-w-explanation-oror-TM%3A-98-49 | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
# The two criteria for a valid board are:
# 1) num of Xs - num of Os is 0 or 1
# 2) X is not a winner if the # o... | valid-tic-tac-toe-state | Python3 || int array, 7 lines, w/ explanation || T/M: 98%/ 49% | warrenruud | 4 | 221 | valid tic tac toe state | 794 | 0.351 | Medium | 12,922 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1838675/Modular-and-Extensible-Python-Solution-Beats-90 | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
n = 3
rows = [0] * n
cols = [0] * n
diag = antidiag = balance = 0
def win(v):
if v in rows or v in cols or v in [diag, antidiag]: return True
return False
for... | valid-tic-tac-toe-state | Modular and Extensible Python Solution - Beats 90% | totoslg | 4 | 162 | valid tic tac toe state | 794 | 0.351 | Medium | 12,923 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1119359/Straightforward-Game-Rules | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
row, col = len(board), len(board[0])
xCount, oCount = 0, 0
def checkStatusWinner(board):
status = ''
if board[0][2] == board[1][1] == board[2][0] !=' ':
status = board[0][2]
... | valid-tic-tac-toe-state | Straightforward Game Rules | anshulkapoor018 | 2 | 303 | valid tic tac toe state | 794 | 0.351 | Medium | 12,924 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1927661/Python-logical-and-self-explanatory | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
Xs = Os = dia1 = dia2 = 0
row = [0] * 3
col = [0] * 3
for r in range(3):
for c in range(3):
if board[r][c] == 'X':
Xs += 1
row[r] += 1
... | valid-tic-tac-toe-state | Python logical and self-explanatory | gulugulugulugulu | 1 | 106 | valid tic tac toe state | 794 | 0.351 | Medium | 12,925 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1363242/24ms-(faster-than-97)-Python3-Solution | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
X,O = self.countSymbols(board)
if O>X:
#Os can't be greater than Xs
return False
elif abs(X-O)>1:
#Difference can only be 1
return False
elif X>O:
#X can't have more moves ... | valid-tic-tac-toe-state | 24ms (faster than 97%) Python3 Solution | jayshukla0034 | 1 | 219 | valid tic tac toe state | 794 | 0.351 | Medium | 12,926 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/2842387/Edge-case-galore | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
# create function to determine number of wins for a player
# keep track of number of tiles for each player
# validate different edge cases for how players are allowed to win
# time O(m * n) space O(1)
def fi... | valid-tic-tac-toe-state | Edge case galore | andrewnerdimo | 0 | 1 | valid tic tac toe state | 794 | 0.351 | Medium | 12,927 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1892966/Python-straightforward-game-rule | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
rows = [0,0,0]
cols = [0,0,0]
n_x = 0
n_o = 0
diag = [0,0]
for i in range(3):
for j in range(3):
if board[i][j] == "O":
rows[i] -= 1
... | valid-tic-tac-toe-state | Python straightforward game rule | ganyue246 | 0 | 106 | valid tic tac toe state | 794 | 0.351 | Medium | 12,928 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1663163/python-simple-solution | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
os, xs = 0, 0
for i in range(3):
for j in range(3):
if board[i][j] == 'O':
os += 1
elif board[i][j] == 'X':
xs += 1
... | valid-tic-tac-toe-state | python simple solution | byuns9334 | 0 | 269 | valid tic tac toe state | 794 | 0.351 | Medium | 12,929 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1360248/Simple-Python-solution-with-explanation | class Solution(object):
def __init__(self):
self.winIndexes =[[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]
self.board = []
def validTicTacToe(self, board):
"""
:type board: List[str]
:rtype: bool
"""
countX, countO = 0,... | valid-tic-tac-toe-state | Simple Python solution with explanation | songire | 0 | 115 | valid tic tac toe state | 794 | 0.351 | Medium | 12,930 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/1105218/Python-or-with-comments | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
def iswinner(player):
for r in range(len(board)):
if board[r][0] == board[r][1] == board[r][2] == player:
return True
for c in range(len(board)):
if board[0][c] ==... | valid-tic-tac-toe-state | Python | with comments | timotheeechalamet | 0 | 171 | valid tic tac toe state | 794 | 0.351 | Medium | 12,931 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/964518/Apply-Game-Rules-or-24ms-or-Well-fomred-and-Well-commented | class Solution:
def validTicTacToe(self, board):
s = str(board)
x_cnt = s.count('X')
o_cnt = s.count('O')
# X is at most one step ahead
if x_cnt == o_cnt or x_cnt == o_cnt + 1:
pass
else:
return False
# get all information
x_w... | valid-tic-tac-toe-state | Apply Game Rules | 24ms | Well fomred and Well commented | steve-jokes | 0 | 126 | valid tic tac toe state | 794 | 0.351 | Medium | 12,932 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/931176/Python3-valid-condition | class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
anti = diag = diff = 0
freq = [0]*8
mp = {"O": -1, " ": 0, "X": 1} # increment & decrement
for i in range(3):
for j in range(3):
x = mp[board[i][j]]
diff += x
... | valid-tic-tac-toe-state | [Python3] valid condition | ye15 | 0 | 137 | valid tic tac toe state | 794 | 0.351 | Medium | 12,933 |
https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/362216/Solution-in-Python-3-(beats-100)-(three-lines) | class Solution:
def validTicTacToe(self, b: List[str]) -> bool:
T, w = {'XXX':0, 'OOO':0}, "".join(b).count('X') - "".join(b).count('O')
for i in [0,1,2]: T[b[0][i]+b[1][i]+b[2][i]], T[b[i][0]+b[i][1]+b[i][2]], T[b[0][2*i//2]+b[1][1]+b[2][2*(1-i//2)]] = 1, 1, 1
return False if (w not in [0,1]) or (T[... | valid-tic-tac-toe-state | Solution in Python 3 (beats 100%) (three lines) | junaidmansuri | -9 | 407 | valid tic tac toe state | 794 | 0.351 | Medium | 12,934 |
https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/discuss/2304108/Python-or-Two-pointer-technique-or-Easy-solution | class Solution:
def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
start,end = -1, -1
res = 0
for i in range(len(nums)):
if nums[i] > right:
start = end = i
continue
if nums[i] >= left:
... | number-of-subarrays-with-bounded-maximum | Python | Two pointer technique | Easy solution | __Asrar | 2 | 79 | number of subarrays with bounded maximum | 795 | 0.527 | Medium | 12,935 |
https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/discuss/931418/Python3-Kadane-ish-algo | class Solution:
def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
ans = inc = cnt = 0
for x in nums:
if x < left: cnt += 1
elif left <= x <= right: inc = cnt = cnt + 1
else: inc = cnt = 0
ans += inc
return ans | number-of-subarrays-with-bounded-maximum | [Python3] Kadane-ish algo | ye15 | 1 | 90 | number of subarrays with bounded maximum | 795 | 0.527 | Medium | 12,936 |
https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/discuss/931418/Python3-Kadane-ish-algo | class Solution:
def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
queue = deque()
ans, ii = 0, -1
for i in range(len(nums)+1):
if i == len(nums) or nums[i] > right:
while queue:
k = queue.popleft()
... | number-of-subarrays-with-bounded-maximum | [Python3] Kadane-ish algo | ye15 | 1 | 90 | number of subarrays with bounded maximum | 795 | 0.527 | Medium | 12,937 |
https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/discuss/1875782/Python-easy-to-read-and-understand-or-two-pointers | class Solution:
def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
ans, prev = 0, 0
i, j = 0, 0
n = len(nums)
for j in range(n):
if left <= nums[j] <= right:
prev = (j-i+1)
ans += prev
elif ... | number-of-subarrays-with-bounded-maximum | Python easy to read and understand | two-pointers | sanial2001 | 0 | 76 | number of subarrays with bounded maximum | 795 | 0.527 | Medium | 12,938 |
https://leetcode.com/problems/rotate-string/discuss/2369025/or-python3-or-ONE-LINE-or-FASTER-THAN-99-or | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
return len(s) == len(goal) and s in goal+goal | rotate-string | ✅ | python3 | ONE LINE | FASTER THAN 99% | 🔥💪 | sahelriaz | 18 | 628 | rotate string | 796 | 0.542 | Easy | 12,939 |
https://leetcode.com/problems/rotate-string/discuss/356624/Solution-in-Python-3-(one-line) | class Solution:
def rotateString(self, A: str, B: str) -> bool:
return (A in B*2) and (len(A) == len(B))
- Junaid Mansuri
(LeetCode ID)@hotmail.com | rotate-string | Solution in Python 3 (one line) | junaidmansuri | 5 | 971 | rotate string | 796 | 0.542 | Easy | 12,940 |
https://leetcode.com/problems/rotate-string/discuss/1116435/Python-3-Pretty-simple-solution | class Solution:
def rotateString(self, A: str, B: str) -> bool:
if len(A) != len(B):
return False
if A == B:
return True
# Copy the original string to know when we've rotated back to the origin
original_A = A
while A:
# Each iteration ro... | rotate-string | Python 3 - Pretty simple solution | edgyporcupine | 2 | 291 | rotate string | 796 | 0.542 | Easy | 12,941 |
https://leetcode.com/problems/rotate-string/discuss/1638385/Python-Fast-and-Easy-(bEATS-85) | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if len(s) != len(goal) or set(s) != set(goal): return False
goal += ''.join(goal)
return s in goal | rotate-string | Python Fast and Easy (bEATS 85%) | ElyasGoli | 1 | 165 | rotate string | 796 | 0.542 | Easy | 12,942 |
https://leetcode.com/problems/rotate-string/discuss/1337272/Python-One-Line-easiest! | class Solution(object):
def rotateString(self, A, B):
return len(A)==len(B) and B in A+A | rotate-string | Python One Line, easiest! | aishwaryanathanii | 1 | 130 | rotate string | 796 | 0.542 | Easy | 12,943 |
https://leetcode.com/problems/rotate-string/discuss/2751571/Simple-Python-Solution | class Solution:
def rotateString(self, A: str, B: str) -> bool:
if len(A) != len(B):
return False
if A == B:
return True
for _ in range(len(B) - 1):
B = B[1:] + B[0]
if A == B:
return True
return False | rotate-string | Simple Python Solution | dnvavinash | 0 | 3 | rotate string | 796 | 0.542 | Easy | 12,944 |
https://leetcode.com/problems/rotate-string/discuss/2742772/Python-Easy | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
for i in range(len(s)):
tmp = s[i + 1:] + s[:i +1]
if (tmp == goal):
return True
return False | rotate-string | Python Easy | lucasschnee | 0 | 7 | rotate string | 796 | 0.542 | Easy | 12,945 |
https://leetcode.com/problems/rotate-string/discuss/2718455/using-python | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
t=list(s)
for i in range(len(s)):
p=s[1:]+t[i]
if p== goal:
return True
print(p)
s=p
return False | rotate-string | using python | sindhu_300 | 0 | 6 | rotate string | 796 | 0.542 | Easy | 12,946 |
https://leetcode.com/problems/rotate-string/discuss/2703682/python-time-complexity-O(n)-not-one-line-code | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if len(s) != len(goal):
return False
temp_string = s*2
count = 0
for i in range(len(temp_string)):
next_temp_string = temp_string[i+1] if i+1 <= (len(temp_string)-1) else ""
next_go... | rotate-string | python time complexity O(n), not one line code | haswanth_reddy | 0 | 9 | rotate string | 796 | 0.542 | Easy | 12,947 |
https://leetcode.com/problems/rotate-string/discuss/2504940/Python-Solution | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
for i in range(0,len(s)):
str1=s[i+1:]+s[:i+1]
if str1==goal:
return True | rotate-string | Python Solution | deepanshu704281 | 0 | 13 | rotate string | 796 | 0.542 | Easy | 12,948 |
https://leetcode.com/problems/rotate-string/discuss/2334964/Python-easy-solution-for-beginners-using-slicing | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
for i in range(len(s)):
if s[i+1:] + s[:i+1] == goal:
return True
return False | rotate-string | Python easy solution for beginners using slicing | alishak1999 | 0 | 74 | rotate string | 796 | 0.542 | Easy | 12,949 |
https://leetcode.com/problems/rotate-string/discuss/2280975/Simple-easy-to-understand-Python-100-fast | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if len(s) != len(goal):
return False
idx = []
for i in range(len(s)):
if s[i] == goal[0] and s[i:]+s[:i] == goal:
return True
... | rotate-string | Simple easy to understand Python 100% fast | harsh30199 | 0 | 62 | rotate string | 796 | 0.542 | Easy | 12,950 |
https://leetcode.com/problems/rotate-string/discuss/2215375/Easy-Python-Code | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
tr = s
rot = s[1:]+s[:1]
print(rot)
while rot != tr:
if rot == goal:
return 1
rot = rot[1:]+rot[:1]
return 0 | rotate-string | Easy Python Code | AnUp_00900 | 0 | 33 | rotate string | 796 | 0.542 | Easy | 12,951 |
https://leetcode.com/problems/rotate-string/discuss/2142597/Python-Simple-KMP-Solution | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if len(s) != len(goal):
return False
txt = s + s
n, m = len(txt), len(goal)
lps = self.build_lps(goal)
i, j = 0, 0
while i < n:
if txt[i] == goal[j] :
... | rotate-string | Python - Simple KMP Solution | ErickMwazonga | 0 | 53 | rotate string | 796 | 0.542 | Easy | 12,952 |
https://leetcode.com/problems/rotate-string/discuss/2098138/Simple-index-based-left-shift-97.08-faster-no-extra-space | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if s == goal:
return True
for i in range(1, len(s)):
s = s[1:] + s[0]
if s == goal:
return True
return False | rotate-string | Simple index based left shift 97.08 % faster no extra space | ankurbhambri | 0 | 52 | rotate string | 796 | 0.542 | Easy | 12,953 |
https://leetcode.com/problems/rotate-string/discuss/2031364/Python-solution | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
for i in range(len(s)):
if s[i:]+s[:i] == goal:
return True
else:
return False | rotate-string | Python solution | StikS32 | 0 | 51 | rotate string | 796 | 0.542 | Easy | 12,954 |
https://leetcode.com/problems/rotate-string/discuss/1766572/91.37-faster-runtime-or-99.62-faster-memory-or-python-3-or-O(n) | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
i = 0
while(i<len(s)):
goal = goal[-1]+goal[:-1]
if s == goal:
return True
i += 1
return False | rotate-string | 91.37% faster runtime | 99.62% faster memory | python 3 | O(n) | Coding_Tan3 | 0 | 118 | rotate string | 796 | 0.542 | Easy | 12,955 |
https://leetcode.com/problems/rotate-string/discuss/1723653/Python-Easy-Solution | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
for i in range(len(s)):
if s[i] == goal[0]: # check if goal's first letter is in s
if s[i:]+s[:i] == goal: # cut string till i from front and add in the end
return True
return False | rotate-string | Python Easy Solution | priyanshu_leet | 0 | 146 | rotate string | 796 | 0.542 | Easy | 12,956 |
https://leetcode.com/problems/rotate-string/discuss/1569040/Python-Solution-or-Faster-Than-99.37-or-Memory-Usage-Beats-92.14-or | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
s=list(s)
for i in range(len(s)):
s.insert(0, s.pop(-1))
if "".join(s)==goal:
return(True)
break
return(False) | rotate-string | Python Solution | Faster Than 99.37% | Memory Usage Beats 92.14% | | Captain_Leo | 0 | 80 | rotate string | 796 | 0.542 | Easy | 12,957 |
https://leetcode.com/problems/rotate-string/discuss/1478129/Python-3-solution | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
for i in range(len(s)):
x = s[0]
s = s.replace(s[0], '', 1) + x
if s == goal:
return True
return False | rotate-string | Python 3 solution | shubhamPanchal | 0 | 43 | rotate string | 796 | 0.542 | Easy | 12,958 |
https://leetcode.com/problems/rotate-string/discuss/1428122/Python3-Unique-Solution-Faster-Than-93.68-Memory-Less-Than-91.83 | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if len(s) != len(goal):
return False
l = len(goal)
d, i = defaultdict(set), 0
while i < l:
d[goal[i]].add(goal[(i + 1) % l])
i += 1
i = 0
while i < l:
... | rotate-string | Python3 Unique Solution, Faster Than 93.68%, Memory Less Than 91.83% | Hejita | 0 | 50 | rotate string | 796 | 0.542 | Easy | 12,959 |
https://leetcode.com/problems/rotate-string/discuss/1225413/Python3-simple-solution-faster-than-94-users | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if s == goal:
return True
for i,j in enumerate(s):
if j == goal[0]:
if (s[i:] + s[:i]) == goal:return True
return False | rotate-string | Python3 simple solution faster than 94% users | EklavyaJoshi | 0 | 47 | rotate string | 796 | 0.542 | Easy | 12,960 |
https://leetcode.com/problems/rotate-string/discuss/1208636/Python-Easy-to-Understand-Faster-than-99.87 | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if len(s)!=len(goal):
return False
s=s*2
if goal in s:
return True
return False | rotate-string | Python Easy to Understand Faster than 99.87% | mk_mohtashim | 0 | 186 | rotate string | 796 | 0.542 | Easy | 12,961 |
https://leetcode.com/problems/rotate-string/discuss/1136343/simple-and-easy-python | class Solution:
def rotateString(self, A: str, B: str) -> bool:
if A==B:
return True
for i in range(len(A)):
if B == A[i:]+A[:i]:
return True
return False | rotate-string | simple and easy python | pheobhe | 0 | 37 | rotate string | 796 | 0.542 | Easy | 12,962 |
https://leetcode.com/problems/rotate-string/discuss/1093976/Python-solution | class Solution:
def rotateString(self, A: str, B: str) -> bool:
if not A and not B:
return True
a = A
for char in A:
a = a[1:] + char
if a == B:
return True
return False | rotate-string | Python solution | i2V0T3VOAqCfe | 0 | 102 | rotate string | 796 | 0.542 | Easy | 12,963 |
https://leetcode.com/problems/rotate-string/discuss/579557/python | class Solution:
def rotateString(self, A: str, B: str) -> bool:
if len(A) != len(B):
return False
if A == B:
return True
for i in range(len(B)):
if (A[i :] + A[0 : i]) == B:
return True
return False | rotate-string | python | CodeRot | 0 | 96 | rotate string | 796 | 0.542 | Easy | 12,964 |
https://leetcode.com/problems/rotate-string/discuss/1328251/Python3-dollarolution | class Solution:
def rotateString(self, s: str, goal: str) -> bool:
if len(s) == len(goal):
x = 2*s
return goal in x
return False | rotate-string | Python3 $olution | AakRay | -1 | 89 | rotate string | 796 | 0.542 | Easy | 12,965 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/752799/Python-simple-BFS-solution-explained | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
q = [[0]]
result = []
target = len(graph) - 1
while q:
temp = q.pop(0)
if temp[-1] == target:
... | all-paths-from-source-to-target | Python simple BFS solution explained | spec_he123 | 6 | 787 | all paths from source to target | 797 | 0.815 | Medium | 12,966 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/298345/Python-faster-than-100-76-ms | class Solution(object):
def __init__(self):
self.memo = {}
def allPathsSourceTarget(self, graph):
"""
:type graph: List[List[int]]
:rtype: List[List[int]]
"""
self.memo = {len(graph)-1:[[len(graph)-1]]}
def calc(N):
if N in self.memo:
... | all-paths-from-source-to-target | Python - faster than 100%, 76 ms | il_buono | 3 | 865 | all paths from source to target | 797 | 0.815 | Medium | 12,967 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/1734565/Python-ll-Iterative-DFS-Using-Stack | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
n = len(graph)
stack = [[0]]
paths = []
while stack:
path = stack.pop()
vertex = path[-1]
if vertex == n-1:
paths.append(path.copy())
for nodes in graph[vertex]:
stack.append( path.copy()+[nodes])
... | all-paths-from-source-to-target | Python ll Iterative DFS Using Stack | morpheusdurden | 2 | 193 | all paths from source to target | 797 | 0.815 | Medium | 12,968 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/1600955/Extremely-clean-and-backtracking-based-DFS-in-Python | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
def dfs(path: List[int]):
if path[-1] == len(graph) - 1:
yield path
else:
for v in graph[path[-1]]:
yield from dfs(path + [v])
... | all-paths-from-source-to-target | Extremely clean and backtracking-based DFS in Python | mousun224 | 2 | 160 | all paths from source to target | 797 | 0.815 | Medium | 12,969 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/1600355/Python3-RECURSION-Explained | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
n = len(graph) - 1
res = []
def helper(cur, acc):
acc.append(cur)
if cur == n:
res.append(acc)
return
fo... | all-paths-from-source-to-target | [Python3] RECURSION, Explained | artod | 2 | 114 | all paths from source to target | 797 | 0.815 | Medium | 12,970 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/1814971/Python-3-DFS-or-Backtracking-Simple-Solution-or-Beats-97 | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
paths = []
visited = [False]*len(graph)
def DFS(graph, src, dest, path):
visited[src] = True
path.append(src)
if src == dest:
paths.appe... | all-paths-from-source-to-target | [Python 3] DFS | Backtracking Simple Solution | Beats 97% | hari19041 | 1 | 77 | all paths from source to target | 797 | 0.815 | Medium | 12,971 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/1422090/Python3-Simple-DFS-with-Backtracking-readable-solution-with-comments. | class Solution:
def dfs(self, graph: List[List[int]], is_visited: set, node: int, target: int,
stack: List, paths: List):
# Return if node already visited
if node in is_visited:
return
# Add node to visited set
is_visited.add(node)
... | all-paths-from-source-to-target | [Python3] Simple DFS with Backtracking, readable solution with comments. | ssshukla26 | 1 | 62 | all paths from source to target | 797 | 0.815 | Medium | 12,972 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/1092481/Python3-(99)-DFS-Memo | class Solution:
def __init__(self):
self.seen = None
self.memo = None
self.end = None
def dfs(self, graph, node):
self.seen[node] = True
if node == self.end:
self.memo[node].append([node])
else:
for nbor in graph[node... | all-paths-from-source-to-target | [Python3] (99%) DFS Memo | valige7091 | 1 | 177 | all paths from source to target | 797 | 0.815 | Medium | 12,973 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/1058204/Python-Backtracking | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
def dfs_with_backtracking(node, path):
seen.add(node) # add node to seen
if node == len(graph)-1: # our termination condition
result.append(path)
... | all-paths-from-source-to-target | Python Backtracking | dev-josh | 1 | 147 | all paths from source to target | 797 | 0.815 | Medium | 12,974 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/753537/Python3-dfs-with-memo | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
def fn(n):
"""Populate ans through dfs"""
stack.append(n)
if n == len(graph)-1: ans.append(stack.copy())
for nn in graph[n]: fn(nn)
stack.pop()
... | all-paths-from-source-to-target | [Python3] dfs with memo | ye15 | 1 | 138 | all paths from source to target | 797 | 0.815 | Medium | 12,975 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/753537/Python3-dfs-with-memo | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
ans = []
stack = [(0, [0])]
while stack:
x, path = stack.pop()
if x == len(graph)-1: ans.append(path)
else:
for xx in graph[x]: stack.append((xx, p... | all-paths-from-source-to-target | [Python3] dfs with memo | ye15 | 1 | 138 | all paths from source to target | 797 | 0.815 | Medium | 12,976 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/753537/Python3-dfs-with-memo | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
@cache
def fn(n):
"""Return path from given node to dst node."""
if n == len(graph)-1: return [[n]]
ans = []
for nn in graph[n]: ans.extend([[n] + x fo... | all-paths-from-source-to-target | [Python3] dfs with memo | ye15 | 1 | 138 | all paths from source to target | 797 | 0.815 | Medium | 12,977 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/753537/Python3-dfs-with-memo | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
indeg = [0]*len(graph)
for val in graph:
for x in val: indeg[x] += 1
queue = deque(i for i, x in enumerate(indeg) if x == 0)
dp = [[] for _ in range(len(graph))]
... | all-paths-from-source-to-target | [Python3] dfs with memo | ye15 | 1 | 138 | all paths from source to target | 797 | 0.815 | Medium | 12,978 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/667114/Short-Python-BFS-beats-99 | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
q = deque([[0]])
paths = []
while q:
curr = q.popleft()
if curr[-1] == len(graph) - 1:
paths.append(curr)
continue
for child... | all-paths-from-source-to-target | Short Python BFS - beats 99% | auwdish | 1 | 210 | all paths from source to target | 797 | 0.815 | Medium | 12,979 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2838410/Python-O(V-%2B-E)-solution-Accepted | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
queue = [[0]]
ans = []
# step = 1
while queue:
curr_path = queue.pop(0)
if curr_path[-1] == len(graph) - 1:
ans.append(curr_path)
conti... | all-paths-from-source-to-target | Python O(V + E) solution [Accepted] | lllchak | 0 | 5 | all paths from source to target | 797 | 0.815 | Medium | 12,980 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2831434/Simple-DFS-Python-Solution-(BEATS-80) | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
result = []
self.dfs(graph, 0, len(graph) - 1, [0], result)
return result
def dfs(self, graph, start, dest, path, result):
if start == dest:
result.append(path.copy()... | all-paths-from-source-to-target | Simple DFS Python Solution (BEATS 80%) | roygarcia | 0 | 3 | all paths from source to target | 797 | 0.815 | Medium | 12,981 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2819387/Python-DFS-solution | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
#edges cases:
if not graph:
return []
# build di-graph
d = {}
for i in range(len(graph)):
d[i] = graph[i]
# apply dfs
n = len(... | all-paths-from-source-to-target | Python DFS solution | taoxinyyyun | 0 | 5 | all paths from source to target | 797 | 0.815 | Medium | 12,982 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2818077/Easy-python-solution-using-DFS | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
res = []
def dfs(graph, src, temp, n):
temp.append(src)
if src == n:
res.append(temp)
for node in graph[src]:
dfs(graph, node, temp[:], n)
... | all-paths-from-source-to-target | Easy python solution using DFS | i-haque | 0 | 4 | all paths from source to target | 797 | 0.815 | Medium | 12,983 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2816314/Python-3-DFS-Solution | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
# With new list created for each path
def dfs(curr, path):
if curr == n - 1:
res.append(path)
for neighbor in graph[curr]:
dfs(neighbor, pat... | all-paths-from-source-to-target | Python 3 DFS Solution | Farawayy | 0 | 2 | all paths from source to target | 797 | 0.815 | Medium | 12,984 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2815744/Python3-Use-DFS-to-search-out-all-paths-in-a-DAG. | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
# Method: DFS. No cycle, so no need to use visited array.
res = []
cur_path = []
def dfs(x):
if x == len(graph) - 1:
res.append(cur_path.copy())
retu... | all-paths-from-source-to-target | [Python3] Use DFS to search out all paths in a DAG. | Cceline00 | 0 | 2 | all paths from source to target | 797 | 0.815 | Medium | 12,985 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2815167/on-path-backtrack-in-python | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
res = []
# visited, onpath = [],[]
def traverse(node, onpath):
# onpath.append(node)
if node == len(graph)-1:
res.append(onpath.copy())
return
... | all-paths-from-source-to-target | on path backtrack in python | ychhhen | 0 | 3 | all paths from source to target | 797 | 0.815 | Medium | 12,986 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2777888/Python-backtrack | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
result = []
def backtrack(i, cur):
nonlocal result
if i == len(graph) - 1:
result.append(list(cur))
return
for j in graph[i]:
... | all-paths-from-source-to-target | [Python] backtrack | i-hate-covid | 0 | 5 | all paths from source to target | 797 | 0.815 | Medium | 12,987 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2739208/Python-3-Solution-DFS | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
last_node = len(graph) - 1
ans = []
def dfs(graph, currentVertex, visited):
visited.append(currentVertex)
for vertex in graph[currentVertex]:
if vertex not in v... | all-paths-from-source-to-target | Python 3 Solution - DFS | sipi09 | 0 | 6 | all paths from source to target | 797 | 0.815 | Medium | 12,988 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2731420/pythonornormal-answer | class Solution:
def allPathsSourceTarget(self, graph):
n = len(graph)
ans = []
visited = set()
temp = [0]
# traverse the graph
for child in graph[0]:
self.dfs(child, temp[:], ans, graph, n)
return ans
... | all-paths-from-source-to-target | python|normal answer | lucy_sea | 0 | 3 | all paths from source to target | 797 | 0.815 | Medium | 12,989 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2711962/Python-solution-or-DFS | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
ans = []
def dfs(index, path):
if index == len(graph) - 1:
ans.append(path)
return
for i in range(len(graph[index])):
... | all-paths-from-source-to-target | Python solution | DFS | maomao1010 | 0 | 14 | all paths from source to target | 797 | 0.815 | Medium | 12,990 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2699655/Python3-Simple-Solution | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
dest = len(graph) - 1
res = []
def DFS(i, arr):
temp = arr.copy()
temp.append(i)
if dest == i:
res.append(temp)
... | all-paths-from-source-to-target | Python3 Simple Solution | mediocre-coder | 0 | 4 | all paths from source to target | 797 | 0.815 | Medium | 12,991 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2437879/All-Paths-from-source-to-target-oror-Python3-oror-DFS | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
ans = []
src = 0
target = len(graph) - 1
self.dfs(src, target, graph, [0], ans)
return ans
def dfs(self, src, target, graph, path, ans):
... | all-paths-from-source-to-target | All Paths from source to target || Python3 || DFS | vanshika_2507 | 0 | 10 | all paths from source to target | 797 | 0.815 | Medium | 12,992 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2428747/Python-Solution-or-Recursive-DFS-or-90-Faster-or-DAG-Path-Stack | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
def generate(node,path):
# if we have reached target node
if node == n-1:
# append current path as an element to finalAns
self.finalAns.append(path.copy())
... | all-paths-from-source-to-target | Python Solution | Recursive DFS | 90% Faster | DAG Path Stack | Gautam_ProMax | 0 | 49 | all paths from source to target | 797 | 0.815 | Medium | 12,993 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2416563/Short-and-fast-Backtracking-python-solution-beats-88 | class Solution(object):
def allPathsSourceTarget(self, graph):
l=len(graph)
def fn(i,d={}):
if i in d:
return d[i]
if i==l-1:
return [[l-1]]
res=[]
for j in graph[i]:
inter=fn(j)
... | all-paths-from-source-to-target | Short and fast Backtracking python solution beats 88% | babashankarsn | 0 | 25 | all paths from source to target | 797 | 0.815 | Medium | 12,994 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2352642/Python3-O(N)-Time-Solution-Solved-using-BFS-%2B-Queue | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
#I was originally going to try tackling BFS manner but DFS seemed more intuitive and
#easier to implement in more readable code!
#Runtime: In worst case, we have to process each and every nod... | all-paths-from-source-to-target | Python3 O(N) Time Solution Solved using BFS + Queue | JOON1234 | 0 | 35 | all paths from source to target | 797 | 0.815 | Medium | 12,995 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2352627/Python3-O(n)-Time-Solution-Using-DFS-%2B-Recursion | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
#I was originally going to try tackling BFS manner but DFS seemed more intuitive and
#easier to implement in more readable code!
#Runtime: O(n), where we have to visit every single node once ... | all-paths-from-source-to-target | Python3 O(n) Time Solution Using DFS + Recursion | JOON1234 | 0 | 20 | all paths from source to target | 797 | 0.815 | Medium | 12,996 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2343359/Python3-Backtracking-solution-(not-DFS) | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
res = []
self.explore(graph, graph[0], [0], res)
return res
def explore(self, graph, candidates, step, res):
if step[-1] == len(graph)-1:
res.append(list(step))
els... | all-paths-from-source-to-target | Python3 -- Backtracking solution (not DFS) | tahir3 | 0 | 71 | all paths from source to target | 797 | 0.815 | Medium | 12,997 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2329932/Beats-97-Beginner-Python-DFS | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
paths = []
target = len(graph) - 1
def dfs(currentNode=0, currentPath=[0]):
if currentNode == target:
nonlocal paths
paths.append(curren... | all-paths-from-source-to-target | Beats 97% - Beginner Python - DFS | 7yler | 0 | 71 | all paths from source to target | 797 | 0.815 | Medium | 12,998 |
https://leetcode.com/problems/all-paths-from-source-to-target/discuss/2265645/Clear-python-solution-with-DFS-traversal | class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
# all possible paths to end node
self.res = []
# current path to end node
path = []
self.traverse(graph, 0, path)
return self.res
def traverse(self, graph, s, path... | all-paths-from-source-to-target | Clear python solution with DFS traversal | leqinancy | 0 | 9 | all paths from source to target | 797 | 0.815 | Medium | 12,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.