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/is-graph-bipartite/discuss/2269192/Clear-simple-Python-solution-using-DFS-traversal
class Solution: isBipartite = True; colors = [] visited = [] def __init(self): self.isBipartite = isBipartite; self.colors = colors self.visited = visited def isBipartite(self, graph: List[List[int]]) -> bool: n = len(graph) self.isBipartite = True s...
is-graph-bipartite
Clear simple Python solution using DFS traversal
leqinancy
0
9
is graph bipartite
785
0.527
Medium
12,800
https://leetcode.com/problems/is-graph-bipartite/discuss/1992330/Python3-Solution-with-using-dfs
class Solution: def dfs(self, graph, color, source): for neigb in graph[source]: if neigb in color: if color[neigb] == color[source]: return False else: color[neigb] = 1 - color[source] if not self.dfs(g...
is-graph-bipartite
[Python3] Solution with using dfs
maosipov11
0
8
is graph bipartite
785
0.527
Medium
12,801
https://leetcode.com/problems/is-graph-bipartite/discuss/1992000/Python-3-Solution-or-BFS-or-Clean-Code
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: # -1 = no colour | 0 = first colour | 1 = second colour colours = [-1] * len(graph) q = deque() def checkBipartite(): # BFS while q: node, currColour = q.popleft...
is-graph-bipartite
Python 3 Solution | BFS | Clean Code
Raja03
0
11
is graph bipartite
785
0.527
Medium
12,802
https://leetcode.com/problems/is-graph-bipartite/discuss/1991032/Python-3-DFS
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: visited = [9 for i in range(len(graph))] res = [True] def dfs(node,color): if visited[node]==9: visited[node] = color ...
is-graph-bipartite
Python 3 DFS
Brillianttyagi
0
23
is graph bipartite
785
0.527
Medium
12,803
https://leetcode.com/problems/is-graph-bipartite/discuss/1990705/Python-3-BFS-Solution
class Solution: def isBipartite(self, graph: list[list[int]]) -> bool: vis = [False for n in range(0, len(graph))] while sum(vis) != len(graph): # Since graph isn't required to be connected this process needs to be repeated ind = vis.index(False) # Find the first entry in th...
is-graph-bipartite
[Python 3] BFS Solution
arsrbt
0
8
is graph bipartite
785
0.527
Medium
12,804
https://leetcode.com/problems/is-graph-bipartite/discuss/1990552/Python-Dictionary-Solution
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: dict_graph = collections.defaultdict(list) for i, v in enumerate(graph) : if v : dict_graph[i] = v dict_tmp = dict_graph while dict_tmp : groupA = set() ...
is-graph-bipartite
[ Python ] Dictionary Solution
crazypuppy
0
34
is graph bipartite
785
0.527
Medium
12,805
https://leetcode.com/problems/is-graph-bipartite/discuss/1875232/Python3-DFS-solution
class Solution: ok = True colors = [] visited = [] def __init(self): self.ok = ok self.colors = colors self.visited = visited def isBipartite(self, graph: List[List[int]]) -> bool: n = len(graph) # bool array storing color(true/false) for each node ...
is-graph-bipartite
[Python3] DFS solution
leqinancy
0
16
is graph bipartite
785
0.527
Medium
12,806
https://leetcode.com/problems/is-graph-bipartite/discuss/1806072/Python-or-DFS-or-Notice-Separate-Nodes-Situation
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: def traverse(graph, s): nonlocal visited, colored, res # if the res has already been set to False, end backtracking if not res: return # Mark this node as visit...
is-graph-bipartite
Python | DFS | Notice Separate Nodes Situation
Fayeyf
0
37
is graph bipartite
785
0.527
Medium
12,807
https://leetcode.com/problems/is-graph-bipartite/discuss/1743166/EASY-or-COLORING-or-DFS-or-PYTHON3
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: # 1 red # -1 blue # 0 uncolored N = len(graph) color = [0]*N def valid_color(node, color_code): for neighbor in graph[node]: if color[neighbor] == color_code: ...
is-graph-bipartite
EASY | COLORING | DFS | PYTHON3
SN009006
0
49
is graph bipartite
785
0.527
Medium
12,808
https://leetcode.com/problems/is-graph-bipartite/discuss/1696188/785-Bipartite-Graph-with-DFS
class Solution: def isBipartite(self, graph): visited = [False] * len(graph); color = visited[:] bipartite = [True] for u in range(len(graph)): if not visited[u]: self.dfs(graph, u, visited, color, bipartite) return bipartite[0] def dfs(self, graph, u, visited, color, bipartite): if not biparti...
is-graph-bipartite
785 Bipartite Graph with DFS
zwang198
0
61
is graph bipartite
785
0.527
Medium
12,809
https://leetcode.com/problems/is-graph-bipartite/discuss/1694953/Python-DFS-coloring
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: def dfs(node, color): if node in seen: return seen[node] != color color ^= 1 seen[node] = color for neighbor in graph[node]: ...
is-graph-bipartite
Python DFS coloring
blue_sky5
0
42
is graph bipartite
785
0.527
Medium
12,810
https://leetcode.com/problems/is-graph-bipartite/discuss/1544587/Python-simple-dfs-solution-with-coloring
class Solution: def isBipartite(self, g: List[List[int]]) -> bool: graph = defaultdict(set) color = {} n = 0 for i in range(len(g)): v = g[i] for x in v: n = max(n, x) graph[i].add(x) unseen =...
is-graph-bipartite
Python simple dfs solution with coloring
byuns9334
0
133
is graph bipartite
785
0.527
Medium
12,811
https://leetcode.com/problems/is-graph-bipartite/discuss/1168736/python-fastest-dfs-with-two-colors-set!
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: connections: dict[int, list[int]] = {index: nodelist for index, nodelist in enumerate(graph)} result: list[bool] = [True] two_colors: list[set] = [set(), set()] visited: dict[int, bool] = {vertex: False for vertex in connections} level: i...
is-graph-bipartite
python fastest dfs with two colors set!
rahul_sawhney
0
90
is graph bipartite
785
0.527
Medium
12,812
https://leetcode.com/problems/is-graph-bipartite/discuss/1031608/Python-simple-DFS-solution-with-comments
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: colors = [0]*len(graph) # 0: no color, 1: red, -1: green def dfs(node: int, node_color: int) -> bool: # node: current node, node_color: current node's color if colors[node] != 0: ...
is-graph-bipartite
Python simple DFS solution with comments
cj1989
0
259
is graph bipartite
785
0.527
Medium
12,813
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/2121258/Explained-Easiest-Python-Solution
class Solution: def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: if len(arr) > 2: res = [] # list for storing the list: [prime fraction of arr[i]/arr[j], arr[i], arr[j]] for i in range(len(arr)): for j in range(i + 1, len(arr)): # creating and adding the sublist to res ...
k-th-smallest-prime-fraction
[Explained] Easiest Python Solution
the_sky_high
2
180
k th smallest prime fraction
786
0.509
Medium
12,814
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/2744456/Binary-Search-%2B-Sliding-Window-(Beats-91.21)
class Solution: def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: N = len(arr) def count_less(v): """1. the number of fractions < v 2. the largest fraction l/r that is < v""" li = 0 cnt, l, r = 0, arr[0], arr[-1] for ...
k-th-smallest-prime-fraction
Binary Search + Sliding Window (Beats 91.21%)
GregHuang
1
60
k th smallest prime fraction
786
0.509
Medium
12,815
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/2848235/Python-Collect-(num1-num2-num1num2)-then-sort-on-fraction-ASC
class Solution: def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: ''' I want to go from Input: arr = [1,2,3,5], k = 3 ; Output: [2,5] Each [(num1, num2, fraction), ...] To: [(1, 5, 1/5), (1, 3, 1/3), (2, 5, 2/5), (1, 2, 1/2), (3, 5, 3/5), (2, 3, 2/3)] ...
k-th-smallest-prime-fraction
[Python] Collect (num1, num2, num1/num2), then sort on fraction ASC
graceiscoding
0
1
k th smallest prime fraction
786
0.509
Medium
12,816
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/2599568/Python3-or-Solved-Using-Sorting-and-Trying-Every-Possible-Pairings
class Solution: #Time-Complexity: O(n^2 + n^2log(n^2)) -> O(n^2*log(n^2)) ->O(n^2*log(n)) #Space-Complexity: O(n^2) def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: array = [] for i in range(0, len(arr)-1): numerator = arr[i] for j in range(i+1...
k-th-smallest-prime-fraction
Python3 | Solved Using Sorting and Trying Every Possible Pairings
JOON1234
0
19
k th smallest prime fraction
786
0.509
Medium
12,817
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/1669752/Python-SubOptimal-but-Easy-to-understand
class Solution: def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: minHeap=[] n = len(arr) x=y=0 for i in range(n): for j in range(i+1,n): if arr[j] !=0: heapq.heappush(minHeap, (arr[i]/arr[j], (arr[i],arr[j]))) ...
k-th-smallest-prime-fraction
[Python] SubOptimal but Easy to understand
JimmyJammy1
0
89
k th smallest prime fraction
786
0.509
Medium
12,818
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/1305847/Python3-priority-queue
class Solution: def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: pq = [(arr[i]/arr[-1], i, -1) for i in range(len(arr)-1)] for _ in range(k): _, i, j = heappop(pq) if i - j + 1 < len(arr): heappush(pq, (arr[i]/arr[j-1], i, j-1)) return [arr[i]...
k-th-smallest-prime-fraction
[Python3] priority queue
ye15
0
90
k th smallest prime fraction
786
0.509
Medium
12,819
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2066105/Python-Easy-Solution-using-Dijkstra's-Algorithm
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: #Make graph adj_list = {i:[] for i in range(n)} for frm, to, price in flights: adj_list[frm].append((to, price)) best_visited = [2**31]*n # Initializ...
cheapest-flights-within-k-stops
Python Easy Solution using Dijkstra's Algorithm
samirpaul1
6
445
cheapest flights within k stops
787
0.359
Medium
12,820
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/790296/Readable-Python-(Djikstra)
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int: graph = {} for u in range(n): graph[u] = [] for u,v,w in flights: graph[u].append((v,w)) heap = [(0,-K,src)] while heap: (...
cheapest-flights-within-k-stops
Readable Python (Djikstra)
2kvai777
6
911
cheapest flights within k stops
787
0.359
Medium
12,821
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2831572/Pythonordpbfs
class Solution: def dp(self, cur, price, step): if cur == self.dst: return price if step == 0: return inf if self.memo[cur][step]: return self.memo[cur][step] res = inf for item in self.graph[cur]: neighbor, cost = item[0], item[1] re...
cheapest-flights-within-k-stops
Python|dp/bfs
lucy_sea
0
8
cheapest flights within k stops
787
0.359
Medium
12,822
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2831030/BFS
class Solution: def findCheapestPrice(self, n: int, edges: List[List[int]], src: int, dst: int, k: int) -> int: adj = [[] for _ in range(n)] m = len(edges) for i in range(m): u, v, w = edges[i] adj[u].append((v, w)) @lru_cache(None) def dp(u, k): ...
cheapest-flights-within-k-stops
BFS
lillllllllly
0
5
cheapest flights within k stops
787
0.359
Medium
12,823
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2807007/Python-(Simple-Dynamic-Programming)
class Solution: def findCheapestPrice(self, n, flights, src, dst, k): dp = [[float("inf")]*(k+2) for _ in range(n)] dp[src][0] = 0 for col in range(1,k+2): for row in range(n): dp[row][col] = dp[row][col-1] for s,e,v in flights: dp[e][...
cheapest-flights-within-k-stops
Python (Simple Dynamic Programming)
rnotappl
0
11
cheapest flights within k stops
787
0.359
Medium
12,824
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2740287/dictionary-solution-python
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int: # Build the adjacency matrix adj_matrix = [[0 for _ in range(n)] for _ in range(n)] for s, d, w in flights: adj_matrix[s][d] = w # Short...
cheapest-flights-within-k-stops
dictionary solution python
yhu415
0
2
cheapest flights within k stops
787
0.359
Medium
12,825
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2710124/Simple-Djikstra
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: graph = defaultdict(list) for flight in flights: s,d,price = flight graph[s].append((d,price)) prices = [float("inf")]*n prices[src] = 0 ...
cheapest-flights-within-k-stops
Simple Djikstra
shriyansnaik
0
12
cheapest flights within k stops
787
0.359
Medium
12,826
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2672100/9-line-python-bellman-ford-beats-57
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: dis = [float('inf')]*n dis[src]=0 for i in range(k+1): backup = dis[:] for a,b,c in flights: dis[b] = min(dis[b],backup[a]+c) retu...
cheapest-flights-within-k-stops
9 line python bellman-ford beats 57%
Ttanlog
0
4
cheapest flights within k stops
787
0.359
Medium
12,827
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2607312/Python3-DFS-w-Cache-or-O(k-*-(V-%2B-E))
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: @cache def dfs(cur, stops): if stops > k: return float('inf') cheapest = float('inf') for nxt, price in adj[cur]: ...
cheapest-flights-within-k-stops
Python3 DFS w/ Cache | O(k * (V + E))
ryangrayson
0
32
cheapest flights within k stops
787
0.359
Medium
12,828
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2345657/python-easy-fast
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: prices = [float("inf")] * n prices[src] = 0 for i in range(k + 1): tempPrices = prices.copy() for s,d,p in flights: ...
cheapest-flights-within-k-stops
python easy fast
soumyadexter7
0
142
cheapest flights within k stops
787
0.359
Medium
12,829
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/2249651/HELP-NEEDED!-Why-does-DFS-does-not-work-in-this-solution
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: adj = [[] for _ in range(n)] costs = {} for flight_info in flights: c1 = flight_info[0] c2 = flight_info[1] cost = flight_in...
cheapest-flights-within-k-stops
HELP NEEDED! Why does DFS does not work in this solution?
gabhinav001
0
93
cheapest flights within k stops
787
0.359
Medium
12,830
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/1132908/Python-Simple-DP-O(n3)
class Solution: def findCheapestPrice(self, n: int, edges: List[List[int]], src: int, dst: int, k: int) -> int: adj = [[] for _ in range(n)] m = len(edges) for i in range(m): u, v, w = edges[i] adj[u].append((v, w)) @lru_cache(None) def dp(u, k): ...
cheapest-flights-within-k-stops
[Python] Simple DP O(n^3)
carloscerlira
0
109
cheapest flights within k stops
787
0.359
Medium
12,831
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/687236/Python3-solution
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int: graph = defaultdict(list) # adjacency list for u, v, w in flights: graph[u].append([v, w]) Q = deque([ [ src, 0] ]) # current node, cum cost min_cost = float...
cheapest-flights-within-k-stops
Python3 solution
dalechoi
0
66
cheapest flights within k stops
787
0.359
Medium
12,832
https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/1042290/Bellman-Ford-Solution-in-C%2B%2B-and-Python3
class Solution: def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int: dist_price = [float('inf') for _ in range(n)] dist_price[src]=0 for source,dest,cost in flights: if src==source: dist_price[dest] = cost ...
cheapest-flights-within-k-stops
Bellman Ford Solution in C++ and Python3
aparna_g
-4
338
cheapest flights within k stops
787
0.359
Medium
12,833
https://leetcode.com/problems/rotated-digits/discuss/1205605/Python3-simple-solution-using-two-approaches
class Solution: def rotatedDigits(self, N: int) -> int: count = 0 for x in range(1, N+1): x = str(x) if '3' in x or '4' in x or '7' in x: continue if '2' in x or '5' in x or '6' in x or '9' in x: count+=1 return count
rotated-digits
Python3 simple solution using two approaches
EklavyaJoshi
4
207
rotated digits
788
0.568
Medium
12,834
https://leetcode.com/problems/rotated-digits/discuss/1205605/Python3-simple-solution-using-two-approaches
class Solution: def rotatedDigits(self, N: int) -> int: d = {'0':'0','1':'1','2':'5','5':'2','6':'9','8':'8','9':'6'} count = 0 for i in range(1,N+1): x = '' flag = True for j in str(i): if j not in d.keys(): flag = Fals...
rotated-digits
Python3 simple solution using two approaches
EklavyaJoshi
4
207
rotated digits
788
0.568
Medium
12,835
https://leetcode.com/problems/rotated-digits/discuss/551837/Python3-solution-using-a-string-conversion
class Solution: def rotatedDigits(self, N: int) -> int: quantity = 0 for num in range(1, N+1): tally = str(num) if any([True if x in '347' else False for x in tally]): continue if all([True if x in '018' else False for x in tally]): ...
rotated-digits
Python3 solution, using a string conversion
altareen
2
191
rotated digits
788
0.568
Medium
12,836
https://leetcode.com/problems/rotated-digits/discuss/343982/Solution-in-Python-3-(beats-~100)-O(log-n)-(Combinatoric-Solution)-(Not-Brute-Force)
class Solution: def rotatedDigits(self, N: int) -> int: N, t, c = str(N), 0, 1 L, a, b = len(N) - 1, [1,2,3,3,3,4,5,5,6,7], [1,2,2,2,2,2,2,2,3,3] for i in range(L): if N[i] == '0': continue t += a[int(N[i])-1]*7**(L-i) - c*b[int(N[i])-1]*3**(L-i) if N[i] in '347': return t ...
rotated-digits
Solution in Python 3 (beats ~100%) O(log n) (Combinatoric Solution) (Not Brute Force)
junaidmansuri
2
671
rotated digits
788
0.568
Medium
12,837
https://leetcode.com/problems/rotated-digits/discuss/2063497/Python-straightforward-solution
class Solution: def rotatedDigits(self, n: int) -> int: ans = 0 for i in range(1, n+1): p = '' if '3' in str(i) or '4' in str(i) or '7' in str(i): continue for j in str(i): if j == '0': p += '0' e...
rotated-digits
Python straightforward solution
StikS32
1
161
rotated digits
788
0.568
Medium
12,838
https://leetcode.com/problems/rotated-digits/discuss/454860/Python3%3A-20ms-(99.86-faster)-12.7MB-(100-memory)
class Solution: def rotatedDigits(self, N: int) -> int: smallSet = {0,1,8} bigSet = {2,5,6,9} smallNum = [0,0,1,1,1,2,3,3,3,4][N % 10] bigNum = [1,2,3,3,3,4,5,5,6,7][N % 10] N = N // 10 smInc, bgInc = 4, 7 while N: x = N % 10 N = N // 1...
rotated-digits
Python3: 20ms (99.86% faster) 12.7MB (100% memory)
andnik
1
326
rotated digits
788
0.568
Medium
12,839
https://leetcode.com/problems/rotated-digits/discuss/325045/Python-solution-using-dictionary
class Solution: def rotatedDigits(self, N: int) -> int: count=0 d={0:0,1:1,2:5,3:-1,4:-1,5:2,6:9,7:-1,8:8,9:6} for i in range(1,N+1): l=list(str(i)) res=[] for j in l: if d[int(j)]!=-1: res.append(str(d[int(j)])) ...
rotated-digits
Python solution using dictionary
ketan35
1
189
rotated digits
788
0.568
Medium
12,840
https://leetcode.com/problems/rotated-digits/discuss/2810782/Python-(Simple-Dynamic-Programming)
class Solution: def dfs(self,x): dict1, x, str1 = {"0":"0","1":"1","8":"8","2":"5","5":"2","6":"9","9":"6"}, str(x), "" for i in x: if i not in dict1: return False else: str1 += dict1[i] return str1 != x def rotatedDigits(self, n...
rotated-digits
Python (Simple Dynamic Programming)
rnotappl
0
5
rotated digits
788
0.568
Medium
12,841
https://leetcode.com/problems/rotated-digits/discuss/1463538/Brute-force-and-walrus
class Solution: not_allowed = {"3", "4", "7"} mirrored = {"0", "1", "8"} def rotatedDigits(self, n: int) -> int: return n - sum(int(bool((s := set(str(i))) &amp; Solution.not_allowed) or s.issubset(Solution.mirrored)) for i in range(1, n + 1))
rotated-digits
Brute force and walrus
EvgenySH
0
42
rotated digits
788
0.568
Medium
12,842
https://leetcode.com/problems/rotated-digits/discuss/1328227/Python3-dollarolution
class Solution: def rotatedDigits(self, n: int) -> int: v, l = ['0','1','8','2','5','6','9'], [] c = 0 for i in range(2,n+1): x = 1 l = [] y = str(i) for j in y: if j not in v: x = 0 brea...
rotated-digits
Python3 $olution
AakRay
0
171
rotated digits
788
0.568
Medium
12,843
https://leetcode.com/problems/rotated-digits/discuss/408795/Python-Simple-Solution
class Solution: def rotatedDigits(self, N: int) -> int: numb = set(['6','9','2','5', '1', '0', '8']) cnt = 0 for i in range(1,N+1): t = set(str(i)) if t-numb==set(): if t-set(['8', '0', '1'])==set(): pass else: cnt+=1 return cnt
rotated-digits
Python Simple Solution
saffi
0
502
rotated digits
788
0.568
Medium
12,844
https://leetcode.com/problems/escape-the-ghosts/discuss/1477363/Python-3-or-Manhattan-Distance-Math-or-Explanation
class Solution: def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: t_x, t_y = target m_x, m_y = abs(t_x), abs(t_y) for x, y in ghosts: manhattan = abs(t_x - x) + abs(t_y - y) if manhattan <= m_x + m_y: return False retu...
escape-the-ghosts
Python 3 | Manhattan Distance, Math | Explanation
idontknoooo
1
117
escape the ghosts
789
0.607
Medium
12,845
https://leetcode.com/problems/escape-the-ghosts/discuss/930127/Python3-brainteaser
class Solution: def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: xx, yy = target return all(abs(x-xx) + abs(y-yy) > abs(xx) + abs(yy) for x, y in ghosts)
escape-the-ghosts
[Python3] brainteaser
ye15
1
86
escape the ghosts
789
0.607
Medium
12,846
https://leetcode.com/problems/escape-the-ghosts/discuss/1427939/Python3-simple-O(N)-time-and-O(1)-space-solution
class Solution: def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: for i in ghosts: if abs(i[0]-target[0]) + abs(i[1]-target[1]) <= abs(target[0])+abs(target[1]): return False return True
escape-the-ghosts
Python3 simple O(N) time and O(1) space solution
EklavyaJoshi
0
55
escape the ghosts
789
0.607
Medium
12,847
https://leetcode.com/problems/escape-the-ghosts/discuss/1085358/python-2-line-easy-faster-than-80
class Solution: def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: l = [abs(ghost[0]-target[0])+abs(ghost[1]-target[1]) for ghost in ghosts] return min(l)>abs(target[0])+abs(target[1])
escape-the-ghosts
python 2-line easy faster than 80%
zzj8222090
0
105
escape the ghosts
789
0.607
Medium
12,848
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/1620809/PythonJAVACC%2B%2B-DP-oror-Image-Visualized-Explanation-oror-100-Faster-oror-O(N)
class Solution(object): def numTilings(self, n): dp = [1, 2, 5] + [0] * n for i in range(3, n): dp[i] = (dp[i - 1] * 2 + dp[i - 3]) % 1000000007 return dp[n - 1]
domino-and-tromino-tiling
✅ [Python/JAVA/C/C++] DP || Image Visualized Explanation || 100% Faster || O(N)
linfq
84
2,800
domino and tromino tiling
790
0.484
Medium
12,849
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/1620809/PythonJAVACC%2B%2B-DP-oror-Image-Visualized-Explanation-oror-100-Faster-oror-O(N)
class Solution(object): def numTilings(self, n): dp, dpa = [1, 2] + [0] * n, [1] * n for i in range(2, n): dp[i] = (dp[i - 1] + dp[i - 2] + dpa[i - 1] * 2) % 1000000007 dpa[i] = (dp[i - 2] + dpa[i - 1]) % 1000000007 return dp[n - 1]
domino-and-tromino-tiling
✅ [Python/JAVA/C/C++] DP || Image Visualized Explanation || 100% Faster || O(N)
linfq
84
2,800
domino and tromino tiling
790
0.484
Medium
12,850
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/1620640/Python-dynamic-programming-in-4-lines-O(N)-time-and-O(1)-space
class Solution: def numTilings(self, n: int) -> int: full_0, full_1, incomp_1 = 1, 2, 2 for i in range(2, n): full_0, full_1, incomp_1 = full_1, full_0 + full_1 + incomp_1, 2 * full_0 + incomp_1 return full_1 % (10 ** 9 + 7) if n >= 2 else 1
domino-and-tromino-tiling
Python dynamic programming in 4 lines, O(N) time and O(1) space
kryuki
8
567
domino and tromino tiling
790
0.484
Medium
12,851
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/1620640/Python-dynamic-programming-in-4-lines-O(N)-time-and-O(1)-space
class Solution: def numTilings(self, n: int) -> int: #edge case if n == 1: return 1 mod = 10 ** 9 + 7 dp_full = [0 for _ in range(n)] dp_incomp = [0 for _ in range(n)] dp_full[0] = 1 dp_full[1] = 2 dp_incomp[1] = 2 ...
domino-and-tromino-tiling
Python dynamic programming in 4 lines, O(N) time and O(1) space
kryuki
8
567
domino and tromino tiling
790
0.484
Medium
12,852
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/1621616/Python3-recursion-%2B-memo-(-dp-on-broken-profile-)
class Solution: def __init__(self): self.table = {(0,0):1, (0,3):1} self.mod = 1000000007 def func(self, pos, state): if pos < 0: return 0 elif (pos, state) not in self.table: if state == 0: self.table[(pos, state)] = self.func(pos-1, 3) ...
domino-and-tromino-tiling
Python3 recursion + memo ( dp on broken profile )
aditya04848
0
19
domino and tromino tiling
790
0.484
Medium
12,853
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/1621456/Simple-Fast-Python-Solution
class Solution: def numTilings(self, n: int) -> int: a, b, c = 0, 1, 1 i = 1 while i < n: a, b, c = a+b, c, a*2 + b + c i += 1 return c % (10**9 + 7)
domino-and-tromino-tiling
Simple Fast Python Solution
VicV13
0
61
domino and tromino tiling
790
0.484
Medium
12,854
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/930186/Python3-top-down-and-bottom-up-dp
class Solution: def numTilings(self, N: int) -> int: @cache def fn(n): """Return number of ways to tile board.""" if n < 0: return 0 if n <= 1: return 1 return (2*fn(n-1) + fn(n-3)) % 1_000_000_007 return fn(N)
domino-and-tromino-tiling
[Python3] top-down & bottom-up dp
ye15
0
130
domino and tromino tiling
790
0.484
Medium
12,855
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/930186/Python3-top-down-and-bottom-up-dp
class Solution: def numTilings(self, N: int) -> int: ans = [1]*(N+1) prefix = 2 for i in range(2, N+1): ans[i] = 2*prefix - ans[i-1] - ans[i-2] prefix += ans[i] return ans[-1] % 1_000_000_007
domino-and-tromino-tiling
[Python3] top-down & bottom-up dp
ye15
0
130
domino and tromino tiling
790
0.484
Medium
12,856
https://leetcode.com/problems/domino-and-tromino-tiling/discuss/930186/Python3-top-down-and-bottom-up-dp
class Solution: def numTilings(self, N: int) -> int: f0, f1, f2 = 0, 1, 1 for i in range(N-1): f0, f1, f2 = f1, f2, (2*f2 + f0) % 1_000_000_007 return f2
domino-and-tromino-tiling
[Python3] top-down & bottom-up dp
ye15
0
130
domino and tromino tiling
790
0.484
Medium
12,857
https://leetcode.com/problems/custom-sort-string/discuss/2237060/Simple-yet-interview-friendly-or-Faster-than-99.97-or-Custom-Sorting-in-Python
class Solution: def customSortString(self, order: str, s: str) -> str: rank = [26]*26 for i in range(len(order)): rank[ord(order[i]) - ord('a')] = i return "".join(sorted(list(s), key= lambda x: rank[ord(x) - ord('a')]))
custom-sort-string
✅ Simple yet interview friendly | Faster than 99.97% | Custom Sorting in Python
reinkarnation
2
62
custom sort string
791
0.693
Medium
12,858
https://leetcode.com/problems/custom-sort-string/discuss/2237060/Simple-yet-interview-friendly-or-Faster-than-99.97-or-Custom-Sorting-in-Python
class Solution: def customSortString(self, order: str, s: str) -> str: def serialOrder(x): return rank[ord(x) - ord('a')] rank = [26]*26 for i in range(len(order)): rank[ord(order[i]) - ord('a')] = i print(rank) ...
custom-sort-string
✅ Simple yet interview friendly | Faster than 99.97% | Custom Sorting in Python
reinkarnation
2
62
custom sort string
791
0.693
Medium
12,859
https://leetcode.com/problems/custom-sort-string/discuss/1813444/Python-easy-to-read-and-understand
class Solution: def customSortString(self, order: str, s: str) -> str: ans = "" for ch in order: cnt = s.count(ch) for i in range(cnt): ans += ch for ch in s: if ch not in order: ans += ch return an...
custom-sort-string
Python easy to read and understand
sanial2001
1
82
custom sort string
791
0.693
Medium
12,860
https://leetcode.com/problems/custom-sort-string/discuss/1735581/Python-99.8-Less-Memory-38.1-Faster-easy-to-understand
class Solution: def customSortString(self, order: str, s: str) -> str: #output string sout = '' count = 0 #iterate though the order for sortletter in order: #check if sortletter is in s, add to output var count = s.count(sortletter) ...
custom-sort-string
Python 99.8% Less Memory, 38.1 Faster - easy to understand
ovidaure
1
127
custom sort string
791
0.693
Medium
12,861
https://leetcode.com/problems/custom-sort-string/discuss/1191458/Python3-simple-solution-beats-99-users
class Solution: def customSortString(self, S: str, T: str) -> str: x = '' t = {} for i in T: if i not in S: x += i t[i] = t.get(i,0) + 1 for i in S: if i in T: x += i*t[i] return x
custom-sort-string
Python3 simple solution beats 99% users
EklavyaJoshi
1
60
custom sort string
791
0.693
Medium
12,862
https://leetcode.com/problems/custom-sort-string/discuss/2808426/using-built-in-sorted-and-hashmap
class Solution: def customSortString(self, order: str, s: str) -> str: # Q does not say we cannot use built-in sort # len(s) = n, len(order) = m # Space Complexity: O(m+n) # Time Complexity: O(nlogn) # average case for built in Python Tim Sort order_map = {o: i for i...
custom-sort-string
using built in `sorted` and hashmap
curiosity_kids
0
2
custom sort string
791
0.693
Medium
12,863
https://leetcode.com/problems/custom-sort-string/discuss/2796144/Beats-91.2-2-Liner
class Solution: def customSortString(self, order: str, s: str) -> str: cmap=collections.Counter(s) return "".join(i*cmap[i] for i in order if i in s) + "".join(i for i in s if i not in order)
custom-sort-string
Beats 91.2% - 2 Liner
avinash_konduri
0
2
custom sort string
791
0.693
Medium
12,864
https://leetcode.com/problems/custom-sort-string/discuss/2796143/Beats-91.2-2-Liner
class Solution: def customSortString(self, order: str, s: str) -> str: cmap=collections.Counter(s) return "".join(i*cmap[i] for i in order if i in s) + "".join(i for i in s if i not in order)
custom-sort-string
Beats 91.2% - 2 Liner
avinash_konduri
0
2
custom sort string
791
0.693
Medium
12,865
https://leetcode.com/problems/custom-sort-string/discuss/2778186/Clean-and-Concise-python-1-line-beats-69-memory
class Solution: def customSortString(self, order: str, s: str) -> str: return "".join(sorted(s, key=(lambda x: order.index(x) if x in order else 100)))
custom-sort-string
Clean and Concise python 1 line beats 69% memory
AryaDot
0
2
custom sort string
791
0.693
Medium
12,866
https://leetcode.com/problems/custom-sort-string/discuss/2614831/python-easy-solution
class Solution: def customSortString(self, order: str, s: str) -> str: str1="" for i in s: if i not in order: str1+=i res="" d = Counter(s) for i in order: if i in d: res+=i*d[i] return res+str1
custom-sort-string
python easy solution
anshsharma17
0
13
custom sort string
791
0.693
Medium
12,867
https://leetcode.com/problems/custom-sort-string/discuss/2608411/Python-or-3-Liner-Solution-or-Easy-to-understand-or-Detailed-Solution
class Solution: def customSortString(self, order: str, s: str) -> str: # Assign each character a value in order and store it in hash map. orderMap = {c: i for i, c in enumerate(order)} # On the basis of order hash map, create the array of character map. If the char is not there in o...
custom-sort-string
Python | 3 Liner Solution | Easy to understand | Detailed Solution
Aexki
0
16
custom sort string
791
0.693
Medium
12,868
https://leetcode.com/problems/custom-sort-string/discuss/2471050/easy-python-solution
class Solution: def customSortString(self, order: str, s: str) -> str: orderChar = [i for i in order] sChar = [i for i in s] notPresent = [] ans = '' for i in orderChar : if i in sChar : for times in range(sChar.count(i)) : an...
custom-sort-string
easy python solution
sghorai
0
20
custom sort string
791
0.693
Medium
12,869
https://leetcode.com/problems/custom-sort-string/discuss/2293310/Python3-or-using-Counter
class Solution: def customSortString(self, order: str, s: str) -> str: count_s, res = Counter(s), "" for char in order: if char in count_s: res += (char * count_s[char]) del count_s[char] return res + "".join([(k*v) for k, v in count_s...
custom-sort-string
Python3 | using Counter
Ploypaphat
0
36
custom sort string
791
0.693
Medium
12,870
https://leetcode.com/problems/custom-sort-string/discuss/2186078/python-solution-with-explanation
class Solution: def customSortString(self, order: str, s: str) -> str: """ Example: Input: order = "cba", s = "abcdeab" Output: "cbbaade" 1. Create a map of string s 2. Iterate over the string order and add similar characters from the map to keep the ans strings characters in correct order and set...
custom-sort-string
python solution with explanation
yash921
0
20
custom sort string
791
0.693
Medium
12,871
https://leetcode.com/problems/custom-sort-string/discuss/1779999/Python-really-simple-using-hashmap
class Solution: def customSortString(self, order: str, s: str) -> str: o = collections.defaultdict(lambda: 0) for i, c in enumerate(order): o[c] = i; return ''.join(sorted(s, key = lambda c: o[c]))
custom-sort-string
Python really simple using hashmap
kaichamp101
0
80
custom sort string
791
0.693
Medium
12,872
https://leetcode.com/problems/custom-sort-string/discuss/1684699/Simple-Python3-beats-87.41
class Solution: def customSortString(self, order: str, s: str) -> str: ranking = {v:i for i, v in enumerate(order)} rest = '' ans = '' for x in s: if x not in ranking: rest += x continue ans += x ans = sorted(an...
custom-sort-string
Simple Python3 beats 87.41%
mclovin286
0
53
custom sort string
791
0.693
Medium
12,873
https://leetcode.com/problems/custom-sort-string/discuss/1569065/Python-hashmap
class Solution: def customSortString(self, order: str, s: str) -> str: order_map = collections.defaultdict(lambda: -1) for i, c in enumerate(order): order_map[c] = i return ''.join(sorted(s, key=lambda x: order_map[x]))
custom-sort-string
Python hashmap
dereky4
0
161
custom sort string
791
0.693
Medium
12,874
https://leetcode.com/problems/custom-sort-string/discuss/1559181/Python3-2-liners
class Solution: def customSortString(self, order: str, s: str) -> str: order_dict = {c: i + 1 for i, c in enumerate(order)} return ''.join(sorted(s, key=lambda x: order_dict.get(x, 0)))
custom-sort-string
Python3 2 liners
needforspeed
0
51
custom sort string
791
0.693
Medium
12,875
https://leetcode.com/problems/custom-sort-string/discuss/1559181/Python3-2-liners
class Solution: def customSortString(self, order: str, s: str) -> str: order_dict = {c: i + 1 for i, c in enumerate(order)} return ''.join(sorted(s, key=lambda x: order_dict.get(x, len(order_dict))))
custom-sort-string
Python3 2 liners
needforspeed
0
51
custom sort string
791
0.693
Medium
12,876
https://leetcode.com/problems/custom-sort-string/discuss/1531759/Python3-Time%3A-O(s%2Bo)-and-Space%3A-O(s)
class Solution: def customSortString(self, order: str, s: str) -> str: # "cba", "abcd" => cbad # "cbafg", "abcd" => "cbad" # create dict for s # iterate thru order and append it to new string if it exists in dict # append left over characters from dict # Time: O(s+o) ...
custom-sort-string
[Python3] Time: O(s+o) & Space: O(s)
jae2021
0
67
custom sort string
791
0.693
Medium
12,877
https://leetcode.com/problems/custom-sort-string/discuss/1257901/Python3-Straight-Forward-Method-easy-to-understand
class Solution: def customSortString(self, order: str, strs: str) -> str: res = [''] * len(strs) not_appear = -1 for s in strs: if s in order: res[order.index(s)] += s else: res[not_appear] = s not_appear -= 1 re...
custom-sort-string
Python3 Straight Forward Method, easy to understand
georgeqz
0
89
custom sort string
791
0.693
Medium
12,878
https://leetcode.com/problems/custom-sort-string/discuss/1087673/Python-One-Liner-Sort-and-Lambda
class Solution: def customSortString(self, S: str, T: str) -> str: return ''.join(sorted(T,key=lambda k:[S.index(c) if c in S else len(S) for c in k]))
custom-sort-string
Python One Liner - Sort & Lambda
ashishpawar517
0
48
custom sort string
791
0.693
Medium
12,879
https://leetcode.com/problems/custom-sort-string/discuss/930194/Python3-custom-sorting
class Solution: def customSortString(self, S: str, T: str) -> str: mp = {c: i for i, c in enumerate(S)} return "".join(sorted(T, key=lambda x: mp.get(x, 26)))
custom-sort-string
[Python3] custom sorting
ye15
0
62
custom sort string
791
0.693
Medium
12,880
https://leetcode.com/problems/custom-sort-string/discuss/930194/Python3-custom-sorting
class Solution: def customSortString(self, order: str, str: str) -> str: freq = {} for c in str: freq[c] = 1 + freq.get(c, 0) ans = [] for c in order: if c in freq: ans.append(c * freq.pop(c)) return "".join(ans) + "".join(k*v for k, v in freq.items())
custom-sort-string
[Python3] custom sorting
ye15
0
62
custom sort string
791
0.693
Medium
12,881
https://leetcode.com/problems/custom-sort-string/discuss/594065/Python-Super-Easy-Runtime-24ms-Complexity-O(n)
class Solution: def customSortString(self, S: str, T: str) -> str: m=len(S) n=len(T) sl=S.split() l=[""]*n left=[] d={} for i in range(n): d[T[i]]=0 for i in range(n): d[T[i]]+=1 ...
custom-sort-string
Python Super Easy Runtime-24ms Complexity- O(n)
Ayu-99
0
47
custom sort string
791
0.693
Medium
12,882
https://leetcode.com/problems/custom-sort-string/discuss/455460/Python-3-(one-line)
class Solution: def customSortString(self, S: str, T: str) -> str: return ''.join(sorted(T, key = lambda x: {c:i for i,c in enumerate(S)}.get(x,0))) - Junaid Mansuri - Chicago, IL
custom-sort-string
Python 3 (one line)
junaidmansuri
0
117
custom sort string
791
0.693
Medium
12,883
https://leetcode.com/problems/custom-sort-string/discuss/403513/Python-one-liner
class Solution: def customSortString(self, S: str, T: str) -> str: return "".join([x*(T.count(x)) for x in list(S)]+[x for x in T if x not in S])
custom-sort-string
Python one liner
saffi
0
112
custom sort string
791
0.693
Medium
12,884
https://leetcode.com/problems/custom-sort-string/discuss/315615/Python3-straightforward-and-concise-solution-beats-80
class Solution: def customSortString(self, S: str, T: str) -> str: m={} for i in S: m.setdefault(i,len(m)) tem=[] tem2=[] for i in T: if i in S: tem.append(i) else: tem2.append(i) tem=sorted(tem,key=lambda x:m[x]) return ''.join(tem)+''.join(tem2)
custom-sort-string
Python3 straightforward and concise solution beats 80%
jasperjoe
0
50
custom sort string
791
0.693
Medium
12,885
https://leetcode.com/problems/custom-sort-string/discuss/304618/Python-O(S%2BT)-solution
class Solution: def customSortString(self, S: str, T: str) -> str: letter_count = collections.Counter(T) other_letters = set(string.ascii_lowercase) - set(S) order = S + ''.join(other_letters) result = [] for letter in order: result.extend([letter * letter_count[l...
custom-sort-string
Python O(S+T) solution
FooBarFooBarFooBar
0
126
custom sort string
791
0.693
Medium
12,886
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1289476/Easy-Approach-oror-Well-explained-oror-95-faster
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: def is_sub(word): index=-1 for ch in word: index=s.find(ch,index+1) if index==-1: return False return True c=0 for word in words: if is_sub(word): ...
number-of-matching-subsequences
📌 Easy-Approach || Well-explained || 95% faster 🐍
abhi9Rai
27
1,200
number of matching subsequences
792
0.519
Medium
12,887
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2077197/Python3-oror-cache-w-explanation-oror-TM%3A-99.995
class Solution: # The plan is to iterate through the words and, for each word w, move # letter by letter of w though the string s if possible to determine # whether w is a subsequence of s. If so, we add to ans. # # We use a functio...
number-of-matching-subsequences
Python3 || cache, w explanation || T/M: 99.9%/95%
warrenruud
4
134
number of matching subsequences
792
0.519
Medium
12,888
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1734042/Python-or-HashMap-or-Counter-or-faster-that-90
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: alpha = defaultdict(list) for w in words: alpha[w[0]].append(w) counter = 0 for c in s: old_bucket = alpha[c] alpha[c] = [] for w in old_bucket: ...
number-of-matching-subsequences
Python | HashMap | Counter | faster that 90%
holdenkold
4
340
number of matching subsequences
792
0.519
Medium
12,889
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1289470/Number-of-Matching-Subsequences-Python-Easy
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: def issub(x, y): it = iter(y) return all(c in it for c in x) c=0 wordsset = set(words) for i in wordsset: if issub(i,s): c = c+words.count(i) retu...
number-of-matching-subsequences
Number of Matching Subsequences Python Easy
user8744WJ
4
342
number of matching subsequences
792
0.519
Medium
12,890
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1712999/Python-solution-Faster-than-99.32-of-python-Submissions
class Solution: def check(self,original,new,index): for ch in new: index= original.find(ch,index) if index==-1:return False index+=1 return True def numMatchingSubseq(self, s: str, words: List[str]) -> int: ans=0 for w in words:ans+=self.check(...
number-of-matching-subsequences
Python solution Faster than 99.32% of python Submissions
reaper_27
2
186
number of matching subsequences
792
0.519
Medium
12,891
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2310050/Python3-oror-Fast-97-3-Approaches-oror-simple-oror-Explained
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: """ # OPTIMAL Approach # create wordMap = {a: [a, acd, ace], b: [bb] ...} # on each iter it becomes {a:[], b: [b], c: [cd, ce] ...} and small # Time Complexity: O(n) + O(m) """ ...
number-of-matching-subsequences
Python3 || Fast 97% 3 Approaches || simple || Explained
Dewang_Patil
1
102
number of matching subsequences
792
0.519
Medium
12,892
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2308688/Python-Solution
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: result = len(words) for word in words: index = -1 for w in word: index = s.find(w, index + 1) if index == -1: result -= 1 brea...
number-of-matching-subsequences
Python Solution
hgalytoby
1
81
number of matching subsequences
792
0.519
Medium
12,893
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2307644/Python3-Preprocessing-Next-Characters
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: # radix R = 26 def char_to_int(ch: chr) -> int: return ord(ch) - ord('a') # preprocessng recent_ind = [len(s)] * R next_char = [()] * len(s) # next_char[i][j] g...
number-of-matching-subsequences
[Python3] Preprocessing Next Characters
jeffreyhu8
1
11
number of matching subsequences
792
0.519
Medium
12,894
https://leetcode.com/problems/number-of-matching-subsequences/discuss/2306885/Python-T%3A1001-ms-oror-Mem%3A17.4MB-oror-Commented-oror-Easy-to-understand-oror-HashMap
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: dict = {} # fill dictionary for all letters with empty list for c in 'abcdefghijklmnopqrstuvwxyz': dict[c] = [] # fill lists occurance-indices in super string for i, c in enu...
number-of-matching-subsequences
[Python] T:1001 ms || Mem:17.4MB || Commented || Easy to understand || HashMap
Buntynara
1
115
number of matching subsequences
792
0.519
Medium
12,895
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1428238/PythonPython3-Simple-readable-solution-using-find-method
class Solution: def mactchChars(self, s: str, word: str): # For each char in a word for char in word: # Find the current char in the string index = s.find(char) # If char not found return false if index == -1: ...
number-of-matching-subsequences
[Python/Python3] Simple readable solution using find method
ssshukla26
1
212
number of matching subsequences
792
0.519
Medium
12,896
https://leetcode.com/problems/number-of-matching-subsequences/discuss/1103585/PythonPython3-Number-of-Matching-Subsequences
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: cnt = 0 final_count = Counter(words) for word in set(words): it = iter(s) if all(letter in it for letter in word): cnt += final_count[word] ...
number-of-matching-subsequences
[Python/Python3] Number of Matching Subsequences
newborncoder
1
395
number of matching subsequences
792
0.519
Medium
12,897
https://leetcode.com/problems/number-of-matching-subsequences/discuss/932263/Python3-two-approaches
class Solution: def numMatchingSubseq(self, S: str, words: List[str]) -> int: mp = {} for i, w in enumerate(words): mp.setdefault(w[0], []).append((i, 0)) ans = 0 for c in S: for i, k in mp.pop(c, []): if k+1 == len(words[i]): ans += 1 ...
number-of-matching-subsequences
[Python3] two approaches
ye15
1
198
number of matching subsequences
792
0.519
Medium
12,898
https://leetcode.com/problems/number-of-matching-subsequences/discuss/932263/Python3-two-approaches
class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: loc = {} for i, ch in enumerate(s): loc.setdefault(ch, []).append(i) ans = 0 for word in words: x = 0 for ch in word: i = bisect_left(loc.get(c...
number-of-matching-subsequences
[Python3] two approaches
ye15
1
198
number of matching subsequences
792
0.519
Medium
12,899