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/count-number-of-special-subsequences/discuss/2589744/Clean-Simple-Python3-or-Iterative-DP-or-O(n)-Time-O(1)-Space
class Solution: def countSpecialSubsequences(self, nums: List[int]) -> int: dp0 = dp1 = dp2 = 0 for num in nums: if num == 2: dp2 += dp1 + dp2 elif num == 1: dp1 += dp0 + dp1 else: dp0 += dp0 + 1 return dp2 % (10**9 + 7)
count-number-of-special-subsequences
Clean, Simple Python3 | Iterative DP | O(n) Time, O(1) Space
ryangrayson
0
15
count number of special subsequences
1,955
0.513
Hard
27,500
https://leetcode.com/problems/count-number-of-special-subsequences/discuss/1505276/Python-DP-O(N)-easy-solution
class Solution: def countSpecialSubsequences(self, nums: List[int]) -> int: MOD = 10**9 + 7 count = collections.Counter() for n in nums: count[n] += (count[n] + count[n - 1]) % MOD + (not n) return count[2] % MOD
count-number-of-special-subsequences
[Python] DP O(N) easy solution
licpotis
0
87
count number of special subsequences
1,955
0.513
Hard
27,501
https://leetcode.com/problems/count-number-of-special-subsequences/discuss/1375488/Python3-dp-ish
class Solution: def countSpecialSubsequences(self, nums: List[int]) -> int: MOD = 1_000_000_007 s0 = s1 = s2 = 0 for x in nums: if x == 0: s0 = (1 + 2*s0) % MOD elif x == 1: s1 = (s0 + 2*s1) % MOD else: s2 = (s1 + 2*s2) % MOD return s2
count-number-of-special-subsequences
[Python3] dp-ish
ye15
0
29
count number of special subsequences
1,955
0.513
Hard
27,502
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2714159/Python-or-Easy-Solution
class Solution: def makeFancyString(self, s: str) -> str: stack = [] for letter in s: if len(stack) > 1 and letter == stack[-1] == stack[-2]: stack.pop() stack.append(letter) return ''.join(stack)
delete-characters-to-make-fancy-string
Python | Easy Solution✔
manayathgeorgejames
6
113
delete characters to make fancy string
1,957
0.567
Easy
27,503
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1389361/Python3-stack
class Solution: def makeFancyString(self, s: str) -> str: stack = [] for ch in s: if len(stack) > 1 and stack[-2] == stack[-1] == ch: continue stack.append(ch) return "".join(stack)
delete-characters-to-make-fancy-string
[Python3] stack
ye15
6
328
delete characters to make fancy string
1,957
0.567
Easy
27,504
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1389309/This-my-simple-solution
class Solution: def makeFancyString(self, s: str) -> str: if len(s) < 3: return s ans = '' ans += s[0] ans += s[1] for i in range(2,len(s)): if s[i] != ans[-1] or s[i] != ans[-2]: ans += s[i] return ans
delete-characters-to-make-fancy-string
This my simple solution
youbou
4
454
delete characters to make fancy string
1,957
0.567
Easy
27,505
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1455438/Python3-Simple-O(n)-solution-faster-than-94
class Solution: def makeFancyString(self, s: str) -> str: t = '' ct = 1 ans = '' for c in s: if c == t: ct += 1 else: ct = 1 if ct < 3: ans += c t = c return ans
delete-characters-to-make-fancy-string
[Python3] Simple O(n) solution, faster than 94%
terrencetang
2
255
delete characters to make fancy string
1,957
0.567
Easy
27,506
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2848150/Python-stack-easy-understandable-solution
class Solution: def makeFancyString(self, s: str) -> str: stack = [] count = 0 while count < len(s): if stack == []: stack.append(s[count]) count += 1 continue elif len(stack) >= 2: if s[count] == stack[-1] and s[count] == stack[-2]: stack.pop(-1) stack.append(s[count]) count += 1 return ''.join(stack)
delete-characters-to-make-fancy-string
Python stack easy understandable solution
youssefyzahran
0
1
delete characters to make fancy string
1,957
0.567
Easy
27,507
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2800707/Python-Easy-Solution
class Solution: def makeFancyString(self, s: str) -> str: s = list(s) f, sec = '', '' for index, i in enumerate(s): if f == i and sec == i: s[index] = '' else: sec = f f = i return ''.join(s) ```
delete-characters-to-make-fancy-string
Python Easy Solution
AliAlievMos
0
2
delete characters to make fancy string
1,957
0.567
Easy
27,508
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2372059/Python3-Fast-and-Easy-solution
class Solution: def makeFancyString(self, s: str) -> str: final=[] c=1 prev="" l=len(s) i=0 while i<l: if s[i]==prev: c+=1 if c<=2: final.append(s[i]) else: c=1 final.append(s[i]) prev=s[i] i+=1 return "".join(final)
delete-characters-to-make-fancy-string
[Python3] Fast & Easy solution
sunakshi132
0
70
delete characters to make fancy string
1,957
0.567
Easy
27,509
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2347688/Easy-solution-beating-95-submissions-by-memory-usage
class Solution: def makeFancyString(self, s: str) -> str: fancy_string = '' for letter in s: if fancy_string[-2:] != letter*2: fancy_string += letter else: pass return fancy_string
delete-characters-to-make-fancy-string
Easy solution beating 95% submissions by memory usage
IvanKorsakov
0
27
delete characters to make fancy string
1,957
0.567
Easy
27,510
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2288324/Python3-or-Stack-or-O(NorN)
class Solution: def makeFancyString(self, s: str) -> str: stack = [] curr_count = 1 deletion = 0 stack.append(s[0]) for i in range(1,len(s)): if stack[len(stack)-1] == s[i]: if curr_count == 2: continue else: stack.append(s[i]) curr_count += 1 elif stack[len(stack)-1] != s[i]: stack.append(s[i]) curr_count = 1 s = "".join(stack) return s
delete-characters-to-make-fancy-string
Python3 | Stack | O(N|N)
user7457RV
0
24
delete characters to make fancy string
1,957
0.567
Easy
27,511
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2065330/Concise-simple-straight-forward-python-solution
class Solution: def makeFancyString(self, s: str) -> str: res = s[:2] for i in range(2,len(s)): if s[i]*2 != s[i-2:i]: res+=s[i] return res
delete-characters-to-make-fancy-string
Concise simple straight-forward python solution
Lexcapital
0
31
delete characters to make fancy string
1,957
0.567
Easy
27,512
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2003958/Python-Clean-and-Simple!
class Solution: def makeFancyString(self, s): s = list(s) a, b = '*', '*' for i in range(len(s)-1,-1,-1): c = s[i] if a == b == c: del s[i] a, b = b, c return "".join(s)
delete-characters-to-make-fancy-string
Python - Clean and Simple!
domthedeveloper
0
88
delete characters to make fancy string
1,957
0.567
Easy
27,513
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/2003958/Python-Clean-and-Simple!
class Solution: def makeFancyString(self, s): x = "" a, b = '*', '*' for c in s: if not a == b == c: x += c a, b = b, c return x
delete-characters-to-make-fancy-string
Python - Clean and Simple!
domthedeveloper
0
88
delete characters to make fancy string
1,957
0.567
Easy
27,514
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1989025/Python-easy-solution-for-beginners
class Solution: def makeFancyString(self, s: str) -> str: if len(s) < 3: return s res = s[0] + s[1] for i in range(2, len(s)): if s[i] != res[-1] or s[i] != res[-2]: res += s[i] return res
delete-characters-to-make-fancy-string
Python easy solution for beginners
alishak1999
0
61
delete characters to make fancy string
1,957
0.567
Easy
27,515
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1967465/python-3-oror-simple-O(n)-solution
class Solution: def makeFancyString(self, s: str) -> str: consecutive = 1 s = list(s) for i in range(1, len(s)): if s[i] == s[i - 1]: consecutive += 1 if consecutive >= 3: s[i - 1] = '' else: consecutive = 1 return ''.join(s)
delete-characters-to-make-fancy-string
python 3 || simple O(n) solution
dereky4
0
47
delete characters to make fancy string
1,957
0.567
Easy
27,516
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1875390/Python-dollarolution
class Solution: def makeFancyString(self, s: str) -> str: l = s[0] count = 1 for i in s[1:]: if l[-1] == i: count += 1 else: count = 1 if count > 2: continue l += i return l
delete-characters-to-make-fancy-string
Python $olution
AakRay
0
31
delete characters to make fancy string
1,957
0.567
Easy
27,517
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1869196/Python3-or-Simple
class Solution: def makeFancyString(self, s: str) -> str: ans = "" l = 0 current_c = "" for c in s: # if current character == previous character if c == current_c: # only append the character if consecutive character length < 2 if l < 2: ans += c l +=1 else: current_c = c ans += c l = 1 return ans
delete-characters-to-make-fancy-string
Python3 | Simple
user0270as
0
35
delete characters to make fancy string
1,957
0.567
Easy
27,518
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1857937/Python-Solution-or-88-Lesser-Memory-or-O(n)-Solution-or-5-lines-of-code
class Solution: def makeFancyString(self, s: str) -> str: s = list(s) for i in range(1,len(s)-1): if (s[i-1] == s[i]) and (s[i+1] == s[i]): s[i-1] = "" return "".join(s)
delete-characters-to-make-fancy-string
✔Python Solution | 88% Lesser Memory | O(n) Solution | 5 lines of code
Coding_Tan3
0
41
delete characters to make fancy string
1,957
0.567
Easy
27,519
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1645081/Python-solution-not-the-fastest-but-kind-of-interesting
class Solution: def makeFancyString(self, s: str) -> str: ranges, r = [], [] for v in s: if v not in r: r = [] ranges.append(r) r[1:] = (v,) return ''.join(v for r in ranges for v in r)
delete-characters-to-make-fancy-string
Python solution, not the fastest, but kind of interesting
emwalker
0
54
delete characters to make fancy string
1,957
0.567
Easy
27,520
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1403427/PYTHON-or-simple-solution
class Solution: def makeFancyString(self, s: str) -> str: x = s[:2] for i in range(2,len(s)) : if x[-1] != s[i] or x[-2] != s[i] : x += s[i] return x
delete-characters-to-make-fancy-string
PYTHON | simple solution
rohitkhairnar
0
117
delete characters to make fancy string
1,957
0.567
Easy
27,521
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1396210/Java-%2B-Python3
class Solution: def makeFancyString(self, s: str) -> str: if len(s) < 3: return s idx = 2 res = s[0] + s[1] while idx < len(s): if s[idx] != res[-1] or s[idx] != res[-2]: res += s[idx] idx += 1 return res
delete-characters-to-make-fancy-string
Java + Python3
jlee9077
0
49
delete characters to make fancy string
1,957
0.567
Easy
27,522
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1392515/Python-oror-Very-Easy-Solution
class Solution: def makeFancyString(self, s: str) -> str: from itertools import groupby p = "" for i, j in groupby(s): q = len(list(j)) if q >= 3: p += i * 2 else: p += i * q return (p)
delete-characters-to-make-fancy-string
Python || Very Easy Solution
naveenrathore
0
103
delete characters to make fancy string
1,957
0.567
Easy
27,523
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1392271/Python-one-liner
class Solution: def makeFancyString(self, s): return s[:2] + ''.join( c2 for c0, c1, c2 in zip(s[:-2], s[1:-1], s[2:]) if not c0 == c1 == c2 )
delete-characters-to-make-fancy-string
Python one-liner
greengum
0
43
delete characters to make fancy string
1,957
0.567
Easy
27,524
https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/1426380/Python-create-a-new-array.-Time%3A-O(N)-Space%3A-O(N)
class Solution: def makeFancyString(self, s: str) -> str: fancy = [] for idx in range(len(s)): if idx < 2 or not s[idx] == s[idx-1] == s[idx-2]: fancy.append(s[idx]) return "".join(fancy)
delete-characters-to-make-fancy-string
Python, create a new array. Time: O(N), Space: O(N)
blue_sky5
-1
67
delete characters to make fancy string
1,957
0.567
Easy
27,525
https://leetcode.com/problems/check-if-move-is-legal/discuss/1389250/Python3-check-8-directions
class Solution: def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool: for di, dj in (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1): i, j = rMove+di, cMove+dj step = 0 while 0 <= i < 8 and 0 <= j < 8: if board[i][j] == color and step: return True if board[i][j] == "." or board[i][j] == color and not step: break i, j = i+di, j+dj step += 1 return False
check-if-move-is-legal
[Python3] check 8 directions
ye15
5
312
check if move is legal
1,958
0.445
Medium
27,526
https://leetcode.com/problems/check-if-move-is-legal/discuss/1450810/Step-in-all-legal-directions-96-speed
class Solution: def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool: directions = [False] * 8 moves = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)] opposite_color = "W" if color == "B" else "B" for d in range(8): r, c = rMove + moves[d][0], cMove + moves[d][1] if 0 <= r < 8 and 0 <= c < 8 and board[r][c] == opposite_color: directions[d] = True for step in range(2, 8): if not any(d for d in directions): return False for d in range(8): if directions[d]: r, c = rMove + step * moves[d][0], cMove + step * moves[d][1] if 0 <= r < 8 and 0 <= c < 8: if board[r][c] == color: return True elif board[r][c] == ".": directions[d] = False else: directions[d] = False return False
check-if-move-is-legal
Step in all legal directions, 96% speed
EvgenySH
1
101
check if move is legal
1,958
0.445
Medium
27,527
https://leetcode.com/problems/check-if-move-is-legal/discuss/2502150/Best-Python3-implementation-(Top-93.9)
class Solution: def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool: directions = [False] * 8 moves = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)] opposite_color = "W" if color == "B" else "B" for d in range(8): r, c = rMove + moves[d][0], cMove + moves[d][1] if 0 <= r < 8 and 0 <= c < 8 and board[r][c] == opposite_color: directions[d] = True for step in range(2, 8): if not any(d for d in directions): return False for d in range(8): if directions[d]: r, c = rMove + step * moves[d][0], cMove + step * moves[d][1] if 0 <= r < 8 and 0 <= c < 8: if board[r][c] == color: return True elif board[r][c] == ".": directions[d] = False else: directions[d] = False return False
check-if-move-is-legal
✔️ Best Python3 implementation (Top 93.9%)
Kagoot
0
10
check if move is legal
1,958
0.445
Medium
27,528
https://leetcode.com/problems/check-if-move-is-legal/discuss/1389381/Clean-Code-oror-Well-Explained-oror-100-Faster-oror-Simple
class Solution: def checkMove(self, board: List[List[str]], row: int, col: int, color: str) -> bool: rows, cols = 8, 8 offsets = ((-1, -1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, 0), (-1, 1)) if board[row][col] != '.' or row < 0 or row >= rows or col < 0 or col >= cols: return False for r_offset, c_offset in offsets: valid = False r, c = row, col while True: r, c = r + r_offset, c + c_offset if r < 0 or r >= rows or c < 0 or c >= cols: break elif board[r][c] == color: if valid: return True else: break elif board[r][c] == '.': break else: valid = True return False
check-if-move-is-legal
🐍 Clean-Code || Well-Explained || 100% Faster || Simple 📌
abhi9Rai
0
36
check if move is legal
1,958
0.445
Medium
27,529
https://leetcode.com/problems/minimum-total-space-wasted-with-k-resizing-operations/discuss/1389260/Python3-dp
class Solution: def minSpaceWastedKResizing(self, nums: List[int], k: int) -> int: @cache def fn(i, k): """Return min waste from i with k ops.""" if i == len(nums): return 0 if k < 0: return inf ans = inf rmx = rsm = 0 for j in range(i, len(nums)): rmx = max(rmx, nums[j]) rsm += nums[j] ans = min(ans, rmx*(j-i+1) - rsm + fn(j+1, k-1)) return ans return fn(0, k)
minimum-total-space-wasted-with-k-resizing-operations
[Python3] dp
ye15
14
987
minimum total space wasted with k resizing operations
1,959
0.42
Medium
27,530
https://leetcode.com/problems/minimum-total-space-wasted-with-k-resizing-operations/discuss/1703531/Python-DP-Faster-than-86
class Solution: def minSpaceWastedKResizing(self, nums: List[int], k: int) -> int: n = len(nums) if k == 0: mx = max(nums) return sum(list(map(lambda x: mx-x, nums))) if k >= n-1: return 0 dp = [[math.inf for i in range(n)] for j in range(k+1)] wasted = [[0 for i in range(n)] for j in range(n)] for i in range(0, n): prev_max = nums[i] for j in range(i, n): if prev_max >= nums[j]: wasted[i][j] = wasted[i][j-1] + prev_max - nums[j] else: diff = nums[j] - prev_max wasted[i][j] = diff * (j - i) + wasted[i][j-1] prev_max = nums[j] for i in range(n): dp[0][i] = wasted[0][i] for j in range(1, k+1): for i in range(j, n): for l in range(j, i+1): dp[j][i] = min(dp[j][i], dp[j-1][l-1] + wasted[l][i]) return dp[k][n-1]
minimum-total-space-wasted-with-k-resizing-operations
Python DP Faster than 86%
tantianx777
1
108
minimum total space wasted with k resizing operations
1,959
0.42
Medium
27,531
https://leetcode.com/problems/minimum-total-space-wasted-with-k-resizing-operations/discuss/1391539/python3-DP-with-memoization-beat-100-python-submissions
class Solution: def minSpaceWastedKResizing(self, A: List[int], K: int) -> int: def waste(i, j, h): sumI = sums[i-1] if i > 0 else 0 return (j-i+1)*h - sums[j] + sumI def dp(i, k): if i <= k: return 0 if k < 0: return MAX if (i, k) in memoize: return memoize[(i, k)] _max = A[i] r = MAX for j in range(i-1, -2, -1): r = min(r, dp(j, k-1) + waste(j+1, i, _max)) _max = max(_max, A[j]) memoize[(i, k)] = r return r sums = list(accumulate(A)) n = len(A) MAX = 10**6*200 memoize = {} return dp(n-1, K)
minimum-total-space-wasted-with-k-resizing-operations
[python3] DP with memoization, beat 100% python submissions
hieuvpm
1
136
minimum total space wasted with k resizing operations
1,959
0.42
Medium
27,532
https://leetcode.com/problems/minimum-total-space-wasted-with-k-resizing-operations/discuss/1780594/python3-DP-solution-for-newbies
class Solution: def minSpaceWastedKResizing(self, nums: List[int], k: int) -> int: MAX = 200e6 # if nums = [n1, n2, n3, ...] then sums = [n1, n1 + n2, n1 + n2+ n3, ...] sums = list(accumulate(nums)) d = {} def dynamicprogramming(i, k): # Break condition one: if the no. of resizes left is greater or equal to index, then we can just resize for all remaining indices, meaning the loss is zer0. if i <= k: return 0 # Break condition 2: if there are no resizes left elif k < 0: return MAX elif (i, k) in d.keys(): return d[(i, k)] loss = MAX curr_max = nums[i] for j in range(i - 1, -2, -1): loss = min(loss, dynamicprogramming(j, k-1) + waste(j + 1, i, curr_max)) d[(i, k)] = loss curr_max = max(curr_max, nums[j]) return loss def waste(i, j, curr_max): if i - 1 < 0: sumi = 0 else: sumi = sums[i - 1] return (j - i + 1)*curr_max - (sums[j] - sumi) return dynamicprogramming(len(nums) - 1, k)
minimum-total-space-wasted-with-k-resizing-operations
[python3] DP - solution for newbies
yuwei-1
0
133
minimum total space wasted with k resizing operations
1,959
0.42
Medium
27,533
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-substrings/discuss/1393832/Python3-Manacher
class Solution: def maxProduct(self, s: str) -> int: n = len(s) # Manacher's algo hlen = [0]*n # half-length center = right = 0 for i in range(n): if i < right: hlen[i] = min(right - i, hlen[2*center - i]) while 0 <= i-1-hlen[i] and i+1+hlen[i] < len(s) and s[i-1-hlen[i]] == s[i+1+hlen[i]]: hlen[i] += 1 if right < i+hlen[i]: center, right = i, i+hlen[i] prefix = [0]*n suffix = [0]*n for i in range(n): prefix[i+hlen[i]] = max(prefix[i+hlen[i]], 2*hlen[i]+1) suffix[i-hlen[i]] = max(suffix[i-hlen[i]], 2*hlen[i]+1) for i in range(1, n): prefix[~i] = max(prefix[~i], prefix[~i+1]-2) suffix[i] = max(suffix[i], suffix[i-1]-2) for i in range(1, n): prefix[i] = max(prefix[i-1], prefix[i]) suffix[~i] = max(suffix[~i], suffix[~i+1]) return max(prefix[i-1]*suffix[i] for i in range(1, n))
maximum-product-of-the-length-of-two-palindromic-substrings
[Python3] Manacher
ye15
3
200
maximum product of the length of two palindromic substrings
1,960
0.293
Hard
27,534
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1390199/Python3-move-along-s
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: i = 0 for word in words: if s[i:i+len(word)] != word: return False i += len(word) if i == len(s): return True return False
check-if-string-is-a-prefix-of-array
[Python3] move along s
ye15
22
1,200
check if string is a prefix of array
1,961
0.541
Easy
27,535
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1390509/Python-Easy
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: a = '' for i in words: a += i if a == s: return True if not s.startswith(a): break return False
check-if-string-is-a-prefix-of-array
Python Easy
lokeshsenthilkumar
8
466
check if string is a prefix of array
1,961
0.541
Easy
27,536
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2003861/Python-Most-Simple-Solution-One-Liner!
class Solution: def isPrefixString(self, s, words): return s in accumulate(words)
check-if-string-is-a-prefix-of-array
Python - Most Simple Solution - One Liner!
domthedeveloper
3
163
check if string is a prefix of array
1,961
0.541
Easy
27,537
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2221696/Python-solution-for-beginners-by-beginner.
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: ans = '' for i in words: ans += i if ans == s : return True return False
check-if-string-is-a-prefix-of-array
Python solution for beginners by beginner.
EbrahimMG
2
93
check if string is a prefix of array
1,961
0.541
Easy
27,538
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1403347/PYTHON-or-simple-solution
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: x = '' for i in words : x += i if x == s : return True if len(x) > len(s) : return False
check-if-string-is-a-prefix-of-array
PYTHON | simple solution
rohitkhairnar
1
136
check if string is a prefix of array
1,961
0.541
Easy
27,539
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1402845/Straightforward-O(n)-approach
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: prefix = "" n = len(s) for w in words: prefix+=w if(prefix==s): return True elif(len(prefix)>n): return False return False
check-if-string-is-a-prefix-of-array
Straightforward O(n) approach
krishna_sharma_s
1
53
check if string is a prefix of array
1,961
0.541
Easy
27,540
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1397000/Easy-Python-Solution-or-Faster-than-98
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: for word in words: if s: if s.startswith(word): s = s[len(word):] else: return False if not s: # for the case when len(s) > len(len(words[0])......len(words[n-1]) return True else: return False
check-if-string-is-a-prefix-of-array
Easy Python Solution | Faster than 98%
the_sky_high
1
181
check if string is a prefix of array
1,961
0.541
Easy
27,541
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2804883/Simple-Python-Approach
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: news = "" for i in range(0,len(words)): news += words[i] if news == s: return True return False
check-if-string-is-a-prefix-of-array
Simple Python Approach
Shagun_Mittal
0
1
check if string is a prefix of array
1,961
0.541
Easy
27,542
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2690352/Simple-Python-Solution
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: j=0 ind=0 n=len(s) m=len(words) for i in range(n): if s[j:i+1]==words[ind]: ind+=1 j=i+1 if ind==m: break if j==n: return True return False
check-if-string-is-a-prefix-of-array
Simple Python Solution
Siddharth_singh
0
6
check if string is a prefix of array
1,961
0.541
Easy
27,543
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2601297/Python-Simple-Solution
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: # get a start index start = 0 # save the length of the string s as we will need it repeatedly s_length = len(s) # go over all words for word in words: # check whether we are out of scope for s if start >= s_length: return True # check if current word is included in s if s[start:start+len(word)] != word: return False # increase the start start += len(word) # check whether we reached the end of the string of the prefix if start >= s_length: return True else: return False
check-if-string-is-a-prefix-of-array
[Python] - Simple Solution
Lucew
0
11
check if string is a prefix of array
1,961
0.541
Easy
27,544
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2289502/Python-or-Easy-and-Understood-or-Fast-and-Simple
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: st=0 end=len(words) while(end): k=''.join(words[:end]) if k==s : return True end-=1 return False
check-if-string-is-a-prefix-of-array
Python | Easy and Understood | Fast and Simple
ajay_keelu
0
26
check if string is a prefix of array
1,961
0.541
Easy
27,545
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2104759/Python-%3A-Beginner-Friendly
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: string='' for word in words: string+=word if string==s: return True return False
check-if-string-is-a-prefix-of-array
Python : Beginner Friendly
kushal2201
0
27
check if string is a prefix of array
1,961
0.541
Easy
27,546
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/2072711/Extremely-simple-and-easy
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: sentence = "" for word in words: sentence += word if s == sentence: return True return False ```
check-if-string-is-a-prefix-of-array
Extremely simple and easy
Okosa
0
33
check if string is a prefix of array
1,961
0.541
Easy
27,547
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1793722/2-Lines-Python-Solution-oror-98-Faster-(28ms)-oror-Memory-less-than-90
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: for i in range(len(words)): if ''.join(words[:i+1]) == s: return True return False
check-if-string-is-a-prefix-of-array
2-Lines Python Solution || 98% Faster (28ms) || Memory less than 90%
Taha-C
0
63
check if string is a prefix of array
1,961
0.541
Easy
27,548
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1552557/One-pass-96-speed
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: pos = i = 0 len_s, len_w = len(s), len(words) while pos < len_s and i < len_w: if s[pos: pos + len(words[i])] == words[i]: pos += len(words[i]) i += 1 else: return False return pos == len_s
check-if-string-is-a-prefix-of-array
One pass, 96% speed
EvgenySH
0
131
check if string is a prefix of array
1,961
0.541
Easy
27,549
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1513193/Python-O(n)-time-solution.-(Compare-only-once)
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: n = len(s) prefix = '' length = 0 for word in words: if length < n: prefix += word length += len(word) return length == n and prefix == s
check-if-string-is-a-prefix-of-array
Python O(n) time solution. (Compare only once)
byuns9334
0
113
check if string is a prefix of array
1,961
0.541
Easy
27,550
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1466274/Simple-and-Easy-Python-Solution
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: tmp="" for i in words: tmp+=i if tmp==s: return True return False
check-if-string-is-a-prefix-of-array
Simple and Easy Python Solution
sangam92
0
75
check if string is a prefix of array
1,961
0.541
Easy
27,551
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1422416/Python3-36ms-Create-S-from-Words
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: ss, i = "", 0 while(True): if len(ss) == len(s) or i == len(words): break ss += words[i] if len(ss) > len(s): return False i += 1 return s == ss
check-if-string-is-a-prefix-of-array
Python3 36ms Create S from Words
Hejita
0
72
check if string is a prefix of array
1,961
0.541
Easy
27,552
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1392572/Python-oror-Basic-Solution
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: p = "" count = 0 for i in words: p += i count += len(i) if len(s) == count: if p == s: return (True) else: return (False) elif len(s) < count: return (False)
check-if-string-is-a-prefix-of-array
Python || Basic Solution
naveenrathore
0
66
check if string is a prefix of array
1,961
0.541
Easy
27,553
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1392109/Java-%2B-Python
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: ans = "" for w in words: ans += w if ans == s: return True if ans > s: return False
check-if-string-is-a-prefix-of-array
Java + Python
jlee9077
0
16
check if string is a prefix of array
1,961
0.541
Easy
27,554
https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/discuss/1390225/Python-Simple-solution
class Solution: def isPrefixString(self, s: str, words: List[str]) -> bool: n = len(s) m = 0 c = "" for i in words: m+=len(i) c+=i if m > n: return False elif m == n and s == c: return True return False
check-if-string-is-a-prefix-of-array
[Python] Simple solution
ritika99
0
41
check if string is a prefix of array
1,961
0.541
Easy
27,555
https://leetcode.com/problems/remove-stones-to-minimize-the-total/discuss/1390207/Python3-priority-queue
class Solution: def minStoneSum(self, piles: List[int], k: int) -> int: pq = [-x for x in piles] heapify(pq) for _ in range(k): heapreplace(pq, pq[0]//2) return -sum(pq)
remove-stones-to-minimize-the-total
[Python3] priority queue
ye15
7
413
remove stones to minimize the total
1,962
0.593
Medium
27,556
https://leetcode.com/problems/remove-stones-to-minimize-the-total/discuss/1390561/Python-3-or-Heap-or-Explanation
class Solution: def minStoneSum(self, piles: List[int], k: int) -> int: heap = [-p for p in piles] heapq.heapify(heap) for _ in range(k): cur = -heapq.heappop(heap) heapq.heappush(heap, -(cur-cur//2)) return -sum(heap)
remove-stones-to-minimize-the-total
Python 3 | Heap | Explanation
idontknoooo
6
287
remove stones to minimize the total
1,962
0.593
Medium
27,557
https://leetcode.com/problems/remove-stones-to-minimize-the-total/discuss/2366593/Python-with-explanation-maxheap
class Solution: def minStoneSum(self, piles: List[int], k: int) -> int: for i, val in enumerate(piles): #we need maxheap, so negate all values piles[i]=-val heapify(piles) while k: x = -heappop(piles) #pop largest absolute no. x-=(x//2) heappush(piles,-x) #negate value before pushing k-=1 return sum(piles)*-1 #all values are negative so use abs or multiply with -1
remove-stones-to-minimize-the-total
Python with explanation - maxheap
sunakshi132
0
16
remove stones to minimize the total
1,962
0.593
Medium
27,558
https://leetcode.com/problems/remove-stones-to-minimize-the-total/discuss/2347622/Python3-or-O(klogn)-or-Priority-Queue
class Solution: def minStoneSum(self, piles: List[int], k: int) -> int: newPiles=[-1*i for i in piles] heapify(newPiles) while k>0: currEle=-1*heappop(newPiles) currEle-=(currEle//2) heappush(newPiles,-1*currEle) k-=1 return -1*sum(newPiles)
remove-stones-to-minimize-the-total
[Python3] | O(klogn) | Priority Queue
swapnilsingh421
0
11
remove stones to minimize the total
1,962
0.593
Medium
27,559
https://leetcode.com/problems/remove-stones-to-minimize-the-total/discuss/2007136/Simple-solution-using-priority-queue-(python)
class Solution: def minStoneSum(self, piles: List[int], k: int) -> int: # since python provides only min heap, we need to make all the elements negative # so that it can be utilized as a max heap for i in range(len(piles)): piles[i]=-piles[i] q=heapq.heapify(piles) while k>0: p=heapq.heappop(piles) # since the elements are negative, we need to use ceil instead of floor p=p-(math.ceil(p/2)) heapq.heappush(piles,p) k-=1 return -sum(piles)
remove-stones-to-minimize-the-total
Simple solution using priority queue (python)
pbhuvaneshwar
0
46
remove stones to minimize the total
1,962
0.593
Medium
27,560
https://leetcode.com/problems/remove-stones-to-minimize-the-total/discuss/1635504/python-simple-heap-solution
class Solution: def minStoneSum(self, piles: List[int], k: int) -> int: import heapq piles = [-x for x in piles] heapq.heapify(piles) def convert(x): y = -x y = (y+1)//2 return -y for _ in range(k): v = heapq.heappop(piles) heapq.heappush(piles, convert(v)) res = 0 while piles: res += heapq.heappop(piles) return -res
remove-stones-to-minimize-the-total
python simple heap solution
byuns9334
0
78
remove stones to minimize the total
1,962
0.593
Medium
27,561
https://leetcode.com/problems/remove-stones-to-minimize-the-total/discuss/1390611/Clean-and-Simple-oror-94-faster-oror-Easy-understandable
class Solution: def minStoneSum(self, piles: List[int], k: int) -> int: local = [-p for p in piles] heapq.heapify(local) for i in range(k): tmp = -1*heapq.heappop(local) tmp = (tmp+1)//2 heapq.heappush(local,-tmp) return -sum(local)
remove-stones-to-minimize-the-total
📌 Clean & Simple || 94% faster || Easy-understandable 🐍
abhi9Rai
0
65
remove stones to minimize the total
1,962
0.593
Medium
27,562
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1390576/Two-Pointers
class Solution: def minSwaps(self, s: str) -> int: res, bal = 0, 0 for ch in s: bal += 1 if ch == '[' else -1 if bal == -1: res += 1 bal = 1 return res
minimum-number-of-swaps-to-make-the-string-balanced
Two Pointers
votrubac
19
2,000
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,563
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1948127/Easy-and-Intuitive-Python-Solution-With-Images-and-Walkthrough
class Solution: def minSwaps(self, s: str) -> int: count = 0 for i in s: if i == "[": count += 1 # increment only if we encounter an open bracket. else: if count > 0: #decrement only if count is positive. Else do nothing and move on. This is because for the case " ] [ [ ] " we do not need to in count -= 1 return (count + 1) // 2
minimum-number-of-swaps-to-make-the-string-balanced
Easy and Intuitive Python Solution With Images and Walkthrough
danielkua
9
406
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,564
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1390209/Python3-greedy
class Solution: def minSwaps(self, s: str) -> int: ans = prefix = 0 for ch in s: if ch == "[": prefix += 1 else: prefix -= 1 if prefix == -1: ans += 1 prefix = 1 return ans
minimum-number-of-swaps-to-make-the-string-balanced
[Python3] greedy
ye15
3
110
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,565
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/2807126/Python3-Solution-or-Clean-and-Concise
class Solution: def minSwaps(self, s): cur, ans = 0, 0 for i in s: if i == ']' and cur == 0: ans += 1 if i == '[' or cur == 0: cur += 1 else: cur -= 1 return ans
minimum-number-of-swaps-to-make-the-string-balanced
✔ Python3 Solution | Clean & Concise
satyam2001
1
97
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,566
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1444539/Python3-solution-or-O(n)-or-explained-pattern
class Solution: def minSwaps(self, s: str) -> int: l = 0 r = 0 for i in range(len(s)): if s[i] == ']': if l == 0: r += 1 else: l -= 1 else: l += 1 if l % 2 == 0: res = l // 2 else: res = (l+1)//2 return res
minimum-number-of-swaps-to-make-the-string-balanced
Python3 solution | O(n) | explained pattern
FlorinnC1
1
368
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,567
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1397368/Python-Using-a-counter.-or-89.58-runtime-99.07-memory
class Solution: def minSwaps(self, s: str) -> int: count=0 for char in s: if count and char != '[': count -= 1 else: count += 1 return (count + 1) //2
minimum-number-of-swaps-to-make-the-string-balanced
[Python] Using a counter. | 89.58% runtime, 99.07% memory
Archangel21809
1
170
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,568
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/2794004/Python-Solution-Easy-to-understand
class Solution: def minSwaps(self, s: str) -> int: extraClose, maxClose = 0, 0 for i in s: if i == "[": extraClose -=1 else: extraClose +=1 maxClose = max(maxClose, extraClose) return (maxClose +1)//2
minimum-number-of-swaps-to-make-the-string-balanced
Python Solution Easy to understand
agnishwar29
0
3
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,569
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1916065/Python-easy-to-read-and-understand
class Solution: def minSwaps(self, s: str) -> int: mx_cnt, cnt = 0, 0 for i in range(len(s)): if s[i] == "]": cnt += 1 mx_cnt = max(mx_cnt, cnt) else: cnt -= 1 return (mx_cnt + 1) // 2
minimum-number-of-swaps-to-make-the-string-balanced
Python easy to read and understand
sanial2001
0
137
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,570
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1907952/Python-3-Solution
class Solution: def minSwaps(self, s: str) -> int: cnt =0 for i in s: if i == '[': cnt += 1 elif i == ']' and cnt > 0: cnt -= 1 return ceil(cnt/2)
minimum-number-of-swaps-to-make-the-string-balanced
Python 3 Solution
DietCoke777
0
52
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,571
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1475094/Python3-Solution-with-using-stack
class Solution: def minSwaps(self, s: str) -> int: stack = [] for idx in range(len(s)): if s[idx] == ']' and stack and stack[-1] == '[': stack.pop() else: stack.append(s[idx]) bracket_count_left_part = len(stack) // 2 return bracket_count_left_part // 2 + (bracket_count_left_part &amp; 1)
minimum-number-of-swaps-to-make-the-string-balanced
[Python3] Solution with using stack
maosipov11
0
134
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,572
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/1437465/Python3-1-Line
class Solution: def minSwaps(self, s: str) -> int: return (reduce(lambda mismatch, ch: mismatch + (-1 if (mismatch > 0 and ch == ']') else 1), s, 0) + 1) // 2
minimum-number-of-swaps-to-make-the-string-balanced
Python3 - 1 Line ✅
Bruception
-2
118
minimum number of swaps to make the string balanced
1,963
0.684
Medium
27,573
https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/discuss/1390573/Clean-and-Simple-oror-98-faster-oror-Easy-Code
class Solution: def longestObstacleCourseAtEachPosition(self, obs: List[int]) -> List[int]: local = [] res=[0 for _ in range(len(obs))] for i in range(len(obs)): n=obs[i] if len(local)==0 or local[-1]<=n: local.append(n) res[i]=len(local) else: ind = bisect.bisect_right(local,n) local[ind]=n res[i]=ind+1 return res
find-the-longest-valid-obstacle-course-at-each-position
📌 Clean & Simple || 98% faster || Easy-Code 🐍
abhi9Rai
1
48
find the longest valid obstacle course at each position
1,964
0.469
Hard
27,574
https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/discuss/1390219/Python3-LIS
class Solution: def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]: ans, vals = [], [] for i, x in enumerate(obstacles): k = bisect_right(vals, x) ans.append(k+1) if k == len(vals): vals.append(x) else: vals[k] = x return ans
find-the-longest-valid-obstacle-course-at-each-position
[Python3] LIS
ye15
1
59
find the longest valid obstacle course at each position
1,964
0.469
Hard
27,575
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1404073/Python3-1-line
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum(x in word for x in patterns)
number-of-strings-that-appear-as-substrings-in-word
[Python3] 1-line
ye15
19
1,400
number of strings that appear as substrings in word
1,967
0.799
Easy
27,576
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2642630/Python-O(N)
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: count=0 for i in patterns: if i in word: count+=1 return count
number-of-strings-that-appear-as-substrings-in-word
Python O(N)
Sneh713
1
118
number of strings that appear as substrings in word
1,967
0.799
Easy
27,577
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2364475/Python-one-liner
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return len([pattern for pattern in patterns if pattern in word])
number-of-strings-that-appear-as-substrings-in-word
Python one-liner
Arrstad
1
44
number of strings that appear as substrings in word
1,967
0.799
Easy
27,578
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1611033/python-simple-solution-using-map
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: li = list(map(lambda x: x in word, patterns)) return li.count(True)
number-of-strings-that-appear-as-substrings-in-word
python simple solution using map
apgokul
1
110
number of strings that appear as substrings in word
1,967
0.799
Easy
27,579
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1597820/Python-one-liner-making-use-of-bool-to-int
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum(i in word for i in patterns) ```
number-of-strings-that-appear-as-substrings-in-word
[Python] one liner making use of bool to int
nelsarrag
1
76
number of strings that appear as substrings in word
1,967
0.799
Easy
27,580
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1473916/O(n-%2B-m)-T-or-O(n-%2B-m)-S-or-suffix-tree-or-Python-3
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: def ukkonen(string: str) -> 'Node': """Build suffix tree by Ukkonen algorithm. :param string: string with last character is terminator. :return: """ class Node: """Class for suffix tree nodes.""" index = 0 def __init__(self, begin, end, parent): """Create node. :param begin: begin index. :param end: end index (not included). :param parent: """ self.edges = dict() self.begin = begin self.end = end self.parent = parent self.id = self.index self.index += 1 def __hash__(self) -> int: """Return hash value. :return: """ return self.id def __len__(self) -> int: """Return length of input edge. :return: """ return self.end - self.begin def __str__(self) -> str: """Return string interpretation suffix tree. :return: """ result = [] def dfs(node): nonlocal result result.append(f'{string[node.begin:node.end]} -> ' + '{') for character in node.edges: result.append(f'{character}: -> ' + '{') dfs(node.edges[character]) result.append('} ') result.append('} ') dfs(self) return ''.join(result) root = Node(0, 0, None) suffix_links_table = dict() def split(node: 'Node', position: int) -> 'Node': """Split edge by position (need for suffix links). :param node: :param position: :return: """ if position == len(node): return node if position == 0: return node.parent new_node = Node(node.begin, node.begin + position, node.parent) node.parent.edges[string[node.begin]] = new_node new_node.edges[string[node.begin + position]] = node node.parent = new_node node.begin += position return new_node def suffix_link(node: 'Node') -> 'Node': """Return suffix link by node. :param node: :return: """ nonlocal suffix_links_table if node in suffix_links_table: return suffix_links_table[node] if node is root: result = root else: link = suffix_link(node.parent) result = split(*next_state( link, len(link), node.begin + (1 if node.parent is root else 0), node.end )) suffix_links_table[node] = result return result def next_state(node: 'Node', position: int, begin: int, end: int) -> tuple['Node', int]: """Return next state. :param node: node. :param position: position on edge. :param begin: begin index. :param end: end index. :return: """ while begin < end: if position == len(node): if string[begin] in node.edges: node = node.edges[string[begin]] position = 0 else: result = (None, None) break else: if string[node.begin + position] != string[begin]: result = (None, None) break if end - begin < len(node) - position: result = (node, position + end - begin) break begin += len(node) - position position = len(node) else: result = (node, position) return result node = root position = 0 for index, character in enumerate(string): while True: node_, position_ = next_state(node, position, index, index + 1) if node_ is not None: node = node_ position = position_ break mid = split(node, position) leaf = Node(index, len(string), mid) mid.edges[string[index]] = leaf node = suffix_link(mid) position = len(node) if mid is root: break return root weight_pattern = collections.Counter(patterns) patterns = list(set(patterns)) word += '$' suffix_tree = ukkonen(word) result = 0 for pattern in patterns: position = 0 node = suffix_tree for character in pattern: if position < node.end: if position >= len(word): break if character != word[position]: break position += 1 else: if character not in node.edges: break node = node.edges[character] position = node.begin + 1 else: result += weight_pattern[pattern] return result
number-of-strings-that-appear-as-substrings-in-word
O(n + m) T | O(n + m) S | suffix tree | Python 3
CiFFiRO
1
300
number of strings that appear as substrings in word
1,967
0.799
Easy
27,581
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1405851/Python-or-Simple-1-liner
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum(pattern in word for pattern in patterns)
number-of-strings-that-appear-as-substrings-in-word
Python | Simple 1 liner
leeteatsleep
1
103
number of strings that appear as substrings in word
1,967
0.799
Easy
27,582
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2808148/Python3-one-line-solution
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum([p in word for p in patterns])
number-of-strings-that-appear-as-substrings-in-word
Python3 one-line solution
sipi09
0
2
number of strings that appear as substrings in word
1,967
0.799
Easy
27,583
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2806359/simple-python-solution
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: ans=0 for i in patterns: ans+=1 if i in word else 0 return ans
number-of-strings-that-appear-as-substrings-in-word
simple python solution
Nikhil2532
0
2
number of strings that appear as substrings in word
1,967
0.799
Easy
27,584
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2767294/Python-or-LeetCode-or-1967.-Number-of-Strings-That-Appear-as-Substrings-in-Word
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: x = 0 for i in patterns: if i in word: x += 1 return x
number-of-strings-that-appear-as-substrings-in-word
Python | LeetCode | 1967. Number of Strings That Appear as Substrings in Word
UzbekDasturchisiman
0
4
number of strings that appear as substrings in word
1,967
0.799
Easy
27,585
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2767294/Python-or-LeetCode-or-1967.-Number-of-Strings-That-Appear-as-Substrings-in-Word
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum(x in word for x in patterns)
number-of-strings-that-appear-as-substrings-in-word
Python | LeetCode | 1967. Number of Strings That Appear as Substrings in Word
UzbekDasturchisiman
0
4
number of strings that appear as substrings in word
1,967
0.799
Easy
27,586
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2764901/Python-solution-by-checking-if-the-pattern-exist-in-word
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: count = 0 for pattern in patterns: if pattern in word: count += 1 return count
number-of-strings-that-appear-as-substrings-in-word
Python solution by checking if the pattern exist in word
samanehghafouri
0
2
number of strings that appear as substrings in word
1,967
0.799
Easy
27,587
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2631668/Solution-using-For-loop
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: count = 0 for match in patterns: if match in word: count+=1 return count
number-of-strings-that-appear-as-substrings-in-word
Solution using For loop
akankshanagar
0
4
number of strings that appear as substrings in word
1,967
0.799
Easy
27,588
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2336532/Python-easy-beginner-solution
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: count = 0 for i in patterns: if i in word: count+=1 return count
number-of-strings-that-appear-as-substrings-in-word
Python easy beginner solution
EbrahimMG
0
11
number of strings that appear as substrings in word
1,967
0.799
Easy
27,589
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2099744/Python-simple-solution
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: ans = 0 for i in patterns: if i in word: ans += 1 return ans
number-of-strings-that-appear-as-substrings-in-word
Python simple solution
StikS32
0
62
number of strings that appear as substrings in word
1,967
0.799
Easy
27,590
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2094955/PYTHON-or-Simple-python-solution
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: res = 0 for i in patterns: if i in word: res += 1 return res
number-of-strings-that-appear-as-substrings-in-word
PYTHON | Simple python solution
shreeruparel
0
19
number of strings that appear as substrings in word
1,967
0.799
Easy
27,591
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/2003825/Python-One-Liner!
class Solution: def numOfStrings(self, patterns, word): return sum(p in word for p in patterns)
number-of-strings-that-appear-as-substrings-in-word
Python - One-Liner!
domthedeveloper
0
61
number of strings that appear as substrings in word
1,967
0.799
Easy
27,592
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1860657/Python-solution-memory-less-than-99
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: count = 0 for i in patterns: if i in word: count += 1 return count
number-of-strings-that-appear-as-substrings-in-word
Python solution, memory less than 99%
alishak1999
0
49
number of strings that appear as substrings in word
1,967
0.799
Easy
27,593
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1664385/Python-using-str.__contains__
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum(map(word.__contains__, patterns))
number-of-strings-that-appear-as-substrings-in-word
Python, using str.__contains__
emwalker
0
29
number of strings that appear as substrings in word
1,967
0.799
Easy
27,594
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1662485/Python3-one-liner
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum(1 for x in patterns if x in word)
number-of-strings-that-appear-as-substrings-in-word
Python3 one-liner
denizen-ru
0
49
number of strings that appear as substrings in word
1,967
0.799
Easy
27,595
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1580868/python-sol-or-faster-than-99
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: count = 0 for i in range(len(patterns)): if patterns[i] in word: count += 1 return count
number-of-strings-that-appear-as-substrings-in-word
python sol | faster than 99%
anandanshul001
0
81
number of strings that appear as substrings in word
1,967
0.799
Easy
27,596
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1468038/Python3-Faster-Than-91.40-Memory-Less-Than-88.68
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: c = 0 for i in patterns: if i in word: c += 1 return c
number-of-strings-that-appear-as-substrings-in-word
Python3 Faster Than 91.40%, Memory Less Than 88.68%
Hejita
0
72
number of strings that appear as substrings in word
1,967
0.799
Easy
27,597
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1405465/PYTHON-3-%3A-EASY-SOLUTION
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: return sum(1 for i in patterns if i in word)
number-of-strings-that-appear-as-substrings-in-word
PYTHON 3 : EASY SOLUTION
rohitkhairnar
0
94
number of strings that appear as substrings in word
1,967
0.799
Easy
27,598
https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/discuss/1404444/Easy-Python-Solution(28ms)
class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: count=0 for i in range(len(patterns)): if patterns[i] in word: count+=1 return count
number-of-strings-that-appear-as-substrings-in-word
Easy Python Solution(28ms)
Sneh17029
0
127
number of strings that appear as substrings in word
1,967
0.799
Easy
27,599