54
54
55
55
## 解法
56
56
57
- 回溯法。
58
-
59
57
<!-- 这里可写通用的实现逻辑 -->
60
58
59
+ ** 方法一:DFS**
60
+
61
+ 我们设计一个函数 $dfs(i)$,表示对于字符串 $word[ i:] $,返回其所有可能的缩写。
62
+
63
+ 函数 $dfs(i)$ 的执行逻辑如下:
64
+
65
+ 如果 $i \geq n$,说明已经处理完了字符串 $word$,直接返回一个空字符串组成的列表。
66
+
67
+ 否则,我们可以选择保留 $word[ i] $,然后对 $dfs(i + 1)$ 返回的列表中的每个字符串前面添加 $word[ i] $,将得到的结果添加到答案中。
68
+
69
+ 我们也可以选择删除 $word[ i] $ 及其后面的若干个字符,假设我们删除了 $word[ i..j)$,那么第 $j$ 个字符不删除,然后对 $dfs(j + 1)$ 返回的列表中的每个字符串前面添加 $j - i$,将得到的结果添加到答案中。
70
+
71
+ 最后,我们在主函数中调用 $dfs(0)$ 即可。
72
+
73
+ 时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $word$ 的长度。
74
+
75
+ ** 方法二:二进制枚举**
76
+
77
+ 由于字符串 $word$ 的长度不超过 $15$,因此我们可以使用二进制枚举的方法枚举所有的缩写。我们用一个长度为 $n$ 的二进制数 $i$ 表示一种缩写方式,其中 $0$ 表示保留对应的字符,而 $1$ 表示删除对应的字符。我们在 $[ 0, 2^n)$ 的范围内枚举所有 $i$,并将其转换成对应的缩写,添加到答案列表中即可。
78
+
79
+ 时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $word$ 的长度。
80
+
61
81
<!-- tabs:start -->
62
82
63
83
### ** Python3**
67
87
``` python
68
88
class Solution :
69
89
def generateAbbreviations (self , word : str ) -> List[str ]:
70
- def dfs (s , t ):
71
- if not s:
72
- ans.append(' ' .join(t))
73
- return
74
- for i in range (1 , len (s) + 1 ):
75
- t.append(str (i))
76
- if i < len (s):
77
- t.append(s[i])
78
- dfs(s[i + 1 :], t)
79
- t.pop()
80
- else :
81
- dfs(s[i:], t)
82
- t.pop()
83
-
84
- t.append(s[0 ])
85
- dfs(s[1 :], t)
86
- t.pop()
90
+ def dfs (i : int ) -> List[str ]:
91
+ if i >= n:
92
+ return [" " ]
93
+ ans = [word[i] + s for s in dfs(i + 1 )]
94
+ for j in range (i + 1 , n + 1 ):
95
+ for s in dfs(j + 1 ):
96
+ ans.append(str (j - i) + (word[j] if j < n else " " ) + s)
97
+ return ans
98
+
99
+ n = len (word)
100
+ return dfs(0 )
101
+ ```
87
102
103
+ ``` python
104
+ class Solution :
105
+ def generateAbbreviations (self , word : str ) -> List[str ]:
106
+ n = len (word)
88
107
ans = []
89
- dfs(word, [])
108
+ for i in range (1 << n):
109
+ cnt = 0
110
+ s = []
111
+ for j in range (n):
112
+ if i >> j & 1 :
113
+ cnt += 1
114
+ else :
115
+ if cnt:
116
+ s.append(str (cnt))
117
+ cnt = 0
118
+ s.append(word[j])
119
+ if cnt:
120
+ s.append(str (cnt))
121
+ ans.append(" " .join(s))
90
122
return ans
91
123
```
92
124
@@ -96,35 +128,197 @@ class Solution:
96
128
97
129
``` java
98
130
class Solution {
99
- private List<String > ans;
131
+ private String word;
132
+ private int n;
100
133
101
134
public List<String > generateAbbreviations (String word ) {
102
- ans = new ArrayList<> ();
103
- List<String > t = new ArrayList<> ();
104
- dfs(word, t);
135
+ this . word = word;
136
+ n = word. length();
137
+ return dfs(0 );
138
+ }
139
+
140
+ private List<String > dfs (int i ) {
141
+ if (i >= n) {
142
+ return List . of(" " );
143
+ }
144
+ List<String > ans = new ArrayList<> ();
145
+ for (String s : dfs(i + 1 )) {
146
+ ans. add(String . valueOf(word. charAt(i)) + s);
147
+ }
148
+ for (int j = i + 1 ; j <= n; ++ j) {
149
+ for (String s : dfs(j + 1 )) {
150
+ ans. add((j - i) + " " + (j < n ? String . valueOf(word. charAt(j)) : " " ) + s);
151
+ }
152
+ }
105
153
return ans;
106
154
}
155
+ }
156
+ ```
107
157
108
- private void dfs (String s , List<String > t ) {
109
- if (" " . equals(s)) {
110
- ans. add(String . join(" " , t));
111
- return ;
158
+ ``` java
159
+ class Solution {
160
+ public List<String > generateAbbreviations (String word ) {
161
+ int n = word. length();
162
+ List<String > ans = new ArrayList<> ();
163
+ for (int i = 0 ; i < 1 << n; ++ i) {
164
+ StringBuilder s = new StringBuilder ();
165
+ int cnt = 0 ;
166
+ for (int j = 0 ; j < n; ++ j) {
167
+ if ((i >> j & 1 ) == 1 ) {
168
+ ++ cnt;
169
+ } else {
170
+ if (cnt > 0 ) {
171
+ s. append(cnt);
172
+ cnt = 0 ;
173
+ }
174
+ s. append(word. charAt(j));
175
+ }
176
+ }
177
+ if (cnt > 0 ) {
178
+ s. append(cnt);
179
+ }
180
+ ans. add(s. toString());
112
181
}
113
- for (int i = 1 ; i < s. length() + 1 ; ++ i) {
114
- t. add(i + " " );
115
- if (i < s. length()) {
116
- t. add(String . valueOf(s. charAt(i)));
117
- dfs(s. substring(i + 1 ), t);
118
- t. remove(t. size() - 1 );
119
- } else {
120
- dfs(s. substring(i), t);
182
+ return ans;
183
+ }
184
+ }
185
+ ```
186
+
187
+ ### ** C++**
188
+
189
+ ``` cpp
190
+ class Solution {
191
+ public:
192
+ vector<string > generateAbbreviations(string word) {
193
+ int n = word.size();
194
+ function<vector<string >(int)> dfs = [ &] (int i) -> vector<string > {
195
+ if (i >= n) {
196
+ return {""};
197
+ }
198
+ vector<string > ans;
199
+ for (auto& s : dfs(i + 1)) {
200
+ string p(1, word[ i] );
201
+ ans.emplace_back(p + s);
202
+ }
203
+ for (int j = i + 1; j <= n; ++j) {
204
+ for (auto& s : dfs(j + 1)) {
205
+ string p = j < n ? string(1, word[ j] ) : "";
206
+ ans.emplace_back(to_string(j - i) + p + s);
207
+ }
208
+ }
209
+ return ans;
210
+ };
211
+ return dfs(0);
212
+ }
213
+ };
214
+ ```
215
+
216
+ ```cpp
217
+ class Solution {
218
+ public:
219
+ vector<string> generateAbbreviations(string word) {
220
+ int n = word.size();
221
+ vector<string> ans;
222
+ for (int i = 0; i < 1 << n; ++i) {
223
+ string s;
224
+ int cnt = 0;
225
+ for (int j = 0; j < n; ++j) {
226
+ if (i >> j & 1) {
227
+ ++cnt;
228
+ } else {
229
+ if (cnt) {
230
+ s += to_string(cnt);
231
+ cnt = 0;
232
+ }
233
+ s.push_back(word[j]);
234
+ }
235
+ }
236
+ if (cnt) {
237
+ s += to_string(cnt);
121
238
}
122
- t . remove(t . size() - 1 );
239
+ ans.push_back(s );
123
240
}
124
- t. add(String . valueOf(s. charAt(0 )));
125
- dfs(s. substring(1 ), t);
126
- t. remove(t. size() - 1 );
241
+ return ans;
127
242
}
243
+ };
244
+ ```
245
+
246
+ ### ** Go**
247
+
248
+ ``` go
249
+ func generateAbbreviations (word string ) []string {
250
+ n := len (word)
251
+ var dfs func (int ) []string
252
+ dfs = func (i int ) []string {
253
+ if i >= n {
254
+ return []string {" " }
255
+ }
256
+ ans := []string {}
257
+ for _ , s := range dfs (i + 1 ) {
258
+ ans = append (ans, word[i:i+1 ]+s)
259
+ }
260
+ for j := i + 1 ; j <= n; j++ {
261
+ for _ , s := range dfs (j + 1 ) {
262
+ p := " "
263
+ if j < n {
264
+ p = word[j : j+1 ]
265
+ }
266
+ ans = append (ans, strconv.Itoa (j-i)+p+s)
267
+ }
268
+ }
269
+ return ans
270
+ }
271
+ return dfs (0 )
272
+ }
273
+ ```
274
+
275
+ ``` go
276
+ func generateAbbreviations (word string ) (ans []string ) {
277
+ n := len (word)
278
+ for i := 0 ; i < 1 <<n; i++ {
279
+ s := &strings.Builder {}
280
+ cnt := 0
281
+ for j := 0 ; j < n; j++ {
282
+ if i>>j&1 == 1 {
283
+ cnt++
284
+ } else {
285
+ if cnt > 0 {
286
+ s.WriteString (strconv.Itoa (cnt))
287
+ cnt = 0
288
+ }
289
+ s.WriteByte (word[j])
290
+ }
291
+ }
292
+ if cnt > 0 {
293
+ s.WriteString (strconv.Itoa (cnt))
294
+ }
295
+ ans = append (ans, s.String ())
296
+ }
297
+ return
298
+ }
299
+ ```
300
+
301
+ ### ** TypeScript**
302
+
303
+ ``` ts
304
+ function generateAbbreviations(word : string ): string [] {
305
+ const n = word .length ;
306
+ const dfs = (i : number ): string [] => {
307
+ if (i >= n ) {
308
+ return [' ' ];
309
+ }
310
+ const ans: string [] = [];
311
+ for (const s of dfs (i + 1 )) {
312
+ ans .push (word [i ] + s );
313
+ }
314
+ for (let j = i + 1 ; j <= n ; ++ j ) {
315
+ for (const s of dfs (j + 1 )) {
316
+ ans .push ((j - i ).toString () + (j < n ? word [j ] : ' ' ) + s );
317
+ }
318
+ }
319
+ return ans ;
320
+ };
321
+ return dfs (0 );
128
322
}
129
323
```
130
324
0 commit comments