Time: 101 ms (100%), Space: 16.7 MB (69.71%) - LeetHub

This commit is contained in:
Deven
2024-07-16 21:33:59 -04:00
parent 7a387e55e1
commit 79082d619c
@@ -0,0 +1,29 @@
class Solution:
def shortestSubstrings(self, arr: List[str]) -> List[str]:
max_len = max([len(x) for x in arr])
sol = [""] * len(arr)
for l in range(1, max_len + 1):
seen = {}
for i, x in enumerate(arr):
for j in range(len(x) - l + 1):
window = x[j:j + l]
if seen.get(window) == None or seen.get(window) == i:
seen[window] = i
else:
seen[window] = -1
# Check if we found everything
for k in seen.keys():
if seen[k] == -1:
continue
cur_sol = sol[seen[k]]
if len(cur_sol) == 0 or (len(k) <= len(cur_sol) and k < cur_sol):
sol[seen[k]] = k
for i in range(len(arr)):
if len(sol[i]) == 0 and len(arr[i]) > l:
break
if i == len(arr) - 1:
return sol
return sol