Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create October_30_Longest_Duplicate_Substring.py AND October_31_Flatt… #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions October'21/October_30_Longest_Duplicate_Substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution:
def longestDupSubstring(self, S: str) -> str:
l = 1
h = len(S)
ans = (0,0)
while l<=h:
mid = l+(h-l)//2
pk = self.check(S,mid)
if pk:
ans = pk
l = mid+1
else:
h = mid-1
return S[ans[0]:ans[0]+ans[1]]

def check(self, A, k):
p = 10**9+7
x = [random.randint(1,p-1) for i in range(2)]
xFactor = [1]*2
for i in range(k):
for j in range(2):
xFactor[j] = (xFactor[j]*x[j])%p
print(x)
print(xFactor)

hashes = {}

hash = [0,0]

for i in range(k):
for j in range(2):
hash[j] = ((hash[j]*x[j])%p +ord(A[i]))%p

hashes[tuple(hash)] = 0

for i in range(k,len(A)):
for j in range(2):
hash[j]=((hash[j]*x[j])%p-(ord(A[i-k])*xFactor[j])%p+ord(A[i]))%p

temp = tuple(hash)
if temp in hashes:
return (hashes[temp],k)
hashes[tuple(hash)] = i-k+1

return None



34 changes: 34 additions & 0 deletions October'21/October_31_Flatten_a_Multilevel_Doubly_Linked_List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
# Definition for a Node.
class Node:
def __init__(self, val, prev, next, child):
self.val = val
self.prev = prev
self.next = next
self.child = child
"""

class Solution:
def flatten(self, head: 'Node') -> 'Node':

cur = head
prev = None
parent = []

while cur or parent:

if cur is None:
cur = parent.pop()
prev.next = cur

if cur.child:
if cur.next:
parent.append(cur.next)
cur.next = cur.child
cur.child = None

cur.prev = prev
prev = cur
cur = cur.next

return head