LeetCode Contest 380: All Solution in one Video || Python || C++ || Java

preview_player
Показать описание
Please subscribe to the channel 4th solution coming soon.....
Рекомендации по теме
Комментарии
Автор

Subscribe to get Solutions in every contest...

gy
Автор

50 subs and I will post 4th solution too

gy
Автор

Here is the Fourth one:


from typing import List
import bisect

class Solution:
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
a_beau = self.kmp_search(s, a)
b_beau = self.kmp_search(s, b)

b_beau.sort() # Sort b_beau to use binary search

beautiful = []
for i in a_beau:
left = bisect.bisect_left(b_beau, i - k)
right = bisect.bisect_right(b_beau, i + k + len(b))

for j in range(left, right):
if abs(b_beau[j] - i) <= k:
beautiful.append(i)
break

return beautiful

def kmp_search(self, text: str, pattern: str) -> List[int]:
indices = []
pi =

q = 0
for i in range(len(text)):
while q > 0 and pattern[q] != text[i]:
q = pi[q - 1]
if pattern[q] == text[i]:
q += 1
if q == len(pattern):
indices.append(i - q + 1)
q = pi[q - 1]

return indices

def compute_prefix_function(self, pattern: str) -> List[int]:
m = len(pattern)
pi = [0] * m
k = 0
for q in range(1, m):
while k > 0 and pattern[k] != pattern[q]:
k = pi[k - 1]
if pattern[k] == pattern[q]:
k += 1
pi[q] = k
return pi

gy
Автор

These are all in python where is java code

pradumkumar_
visit shbcf.ru