diff --git a/October'21/October_30_Longest_Duplicate_Substring.py b/October'21/October_30_Longest_Duplicate_Substring.py new file mode 100644 index 0000000..1f12ccf --- /dev/null +++ b/October'21/October_30_Longest_Duplicate_Substring.py @@ -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 + + + \ No newline at end of file diff --git a/October'21/October_31_Flatten_a_Multilevel_Doubly_Linked_List.py b/October'21/October_31_Flatten_a_Multilevel_Doubly_Linked_List.py new file mode 100644 index 0000000..b801447 --- /dev/null +++ b/October'21/October_31_Flatten_a_Multilevel_Doubly_Linked_List.py @@ -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 \ No newline at end of file