From 79082d619cb5277fc41c265b41b867b01456fa2f Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:33:59 -0400 Subject: [PATCH] Time: 101 ms (100%), Space: 16.7 MB (69.71%) - LeetHub --- ...shortest-uncommon-substring-in-an-array.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 3076-shortest-uncommon-substring-in-an-array/3076-shortest-uncommon-substring-in-an-array.py diff --git a/3076-shortest-uncommon-substring-in-an-array/3076-shortest-uncommon-substring-in-an-array.py b/3076-shortest-uncommon-substring-in-an-array/3076-shortest-uncommon-substring-in-an-array.py new file mode 100644 index 0000000..97c5ea8 --- /dev/null +++ b/3076-shortest-uncommon-substring-in-an-array/3076-shortest-uncommon-substring-in-an-array.py @@ -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