|
52 | 52 |
|
53 | 53 | ## Solutions
|
54 | 54 |
|
55 |
| -### Solution 1 |
| 55 | +### Solution 1: Enumeration |
| 56 | + |
| 57 | +Given the small data scale, we can directly enumerate all substrings of each string and then determine whether it is a substring of other strings. |
| 58 | + |
| 59 | +Specifically, we first enumerate each string `arr[i]`, then enumerate the length $j$ of each substring from small to large, and then enumerate the starting position $l$ of each substring. We can get the current substring as `sub = arr[i][l:l+j]`. Then we determine whether `sub` is a substring of other strings. If it is, we skip the current substring; otherwise, we update the answer. |
| 60 | + |
| 61 | +The time complexity is $O(n^2 \times m^4)$, and the space complexity is $O(m)$. Where $n$ is the length of the string array `arr`, and $m$ is the maximum length of the string. In this problem, $m \le 20$. |
56 | 62 |
|
57 | 63 | <!-- tabs:start -->
|
58 | 64 |
|
59 | 65 | ```python
|
60 |
| - |
| 66 | +class Solution: |
| 67 | + def shortestSubstrings(self, arr: List[str]) -> List[str]: |
| 68 | + ans = [""] * len(arr) |
| 69 | + for i, s in enumerate(arr): |
| 70 | + m = len(s) |
| 71 | + for j in range(1, m + 1): |
| 72 | + for l in range(m - j + 1): |
| 73 | + sub = s[l : l + j] |
| 74 | + if not ans[i] or ans[i] > sub: |
| 75 | + if all(k == i or sub not in t for k, t in enumerate(arr)): |
| 76 | + ans[i] = sub |
| 77 | + if ans[i]: |
| 78 | + break |
| 79 | + return ans |
61 | 80 | ```
|
62 | 81 |
|
63 | 82 | ```java
|
64 |
| - |
| 83 | +class Solution { |
| 84 | + public String[] shortestSubstrings(String[] arr) { |
| 85 | + int n = arr.length; |
| 86 | + String[] ans = new String[n]; |
| 87 | + Arrays.fill(ans, ""); |
| 88 | + for (int i = 0; i < n; ++i) { |
| 89 | + int m = arr[i].length(); |
| 90 | + for (int j = 1; j <= m && ans[i].isEmpty(); ++j) { |
| 91 | + for (int l = 0; l <= m - j; ++l) { |
| 92 | + String sub = arr[i].substring(l, l + j); |
| 93 | + if (ans[i].isEmpty() || sub.compareTo(ans[i]) < 0) { |
| 94 | + boolean ok = true; |
| 95 | + for (int k = 0; k < n && ok; ++k) { |
| 96 | + if (k != i && arr[k].contains(sub)) { |
| 97 | + ok = false; |
| 98 | + } |
| 99 | + } |
| 100 | + if (ok) { |
| 101 | + ans[i] = sub; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return ans; |
| 108 | + } |
| 109 | +} |
65 | 110 | ```
|
66 | 111 |
|
67 | 112 | ```cpp
|
68 |
| - |
| 113 | +class Solution { |
| 114 | +public: |
| 115 | + vector<string> shortestSubstrings(vector<string>& arr) { |
| 116 | + int n = arr.size(); |
| 117 | + vector<string> ans(n); |
| 118 | + for (int i = 0; i < n; ++i) { |
| 119 | + int m = arr[i].size(); |
| 120 | + for (int j = 1; j <= m && ans[i].empty(); ++j) { |
| 121 | + for (int l = 0; l <= m - j; ++l) { |
| 122 | + string sub = arr[i].substr(l, j); |
| 123 | + if (ans[i].empty() || sub < ans[i]) { |
| 124 | + bool ok = true; |
| 125 | + for (int k = 0; k < n && ok; ++k) { |
| 126 | + if (k != i && arr[k].find(sub) != string::npos) { |
| 127 | + ok = false; |
| 128 | + } |
| 129 | + } |
| 130 | + if (ok) { |
| 131 | + ans[i] = sub; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + return ans; |
| 138 | + } |
| 139 | +}; |
69 | 140 | ```
|
70 | 141 |
|
71 | 142 | ```go
|
| 143 | +func shortestSubstrings(arr []string) []string { |
| 144 | + ans := make([]string, len(arr)) |
| 145 | + for i, s := range arr { |
| 146 | + m := len(s) |
| 147 | + for j := 1; j <= m && len(ans[i]) == 0; j++ { |
| 148 | + for l := 0; l <= m-j; l++ { |
| 149 | + sub := s[l : l+j] |
| 150 | + if len(ans[i]) == 0 || ans[i] > sub { |
| 151 | + ok := true |
| 152 | + for k, t := range arr { |
| 153 | + if k != i && strings.Contains(t, sub) { |
| 154 | + ok = false |
| 155 | + break |
| 156 | + } |
| 157 | + } |
| 158 | + if ok { |
| 159 | + ans[i] = sub |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return ans |
| 166 | +} |
| 167 | +``` |
72 | 168 |
|
| 169 | +```ts |
| 170 | +function shortestSubstrings(arr: string[]): string[] { |
| 171 | + const n: number = arr.length; |
| 172 | + const ans: string[] = Array(n).fill(''); |
| 173 | + for (let i = 0; i < n; ++i) { |
| 174 | + const m: number = arr[i].length; |
| 175 | + for (let j = 1; j <= m && ans[i] === ''; ++j) { |
| 176 | + for (let l = 0; l <= m - j; ++l) { |
| 177 | + const sub: string = arr[i].slice(l, l + j); |
| 178 | + if (ans[i] === '' || sub.localeCompare(ans[i]) < 0) { |
| 179 | + let ok: boolean = true; |
| 180 | + for (let k = 0; k < n && ok; ++k) { |
| 181 | + if (k !== i && arr[k].includes(sub)) { |
| 182 | + ok = false; |
| 183 | + } |
| 184 | + } |
| 185 | + if (ok) { |
| 186 | + ans[i] = sub; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + return ans; |
| 193 | +} |
73 | 194 | ```
|
74 | 195 |
|
75 | 196 | <!-- tabs:end -->
|
|
0 commit comments