|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
8 | 8 |
|
9 |
| -<p>请你写出一个能够举单词全部缩写的函数。</p> |
| 9 | +<p>单词的 <strong>广义缩写词</strong> 可以通过下述步骤构造:先取任意数量的不重叠的子字符串,再用它们各自的长度进行替换。 |
10 | 10 |
|
11 |
| -<p><strong>注意:</strong>输出的顺序并不重要。</p> |
| 11 | +例如: |
12 | 12 |
|
13 |
| -<p><strong>示例:</strong></p> |
| 13 | +- <code>"abcde"</code> 可以缩写为 <code>"a3e"</code>(<code>"bcd"</code> 变为 <code>"3"</code> ) |
| 14 | +- <code>"1bcd1"</code>(<code>"a"</code> 和 <code>"e"</code> 都变为 <code>"1"</code>) |
| 15 | +- <code>"5"</code>(<code>"abcde"</code> 变为 <code>"5"</code>) |
14 | 16 |
|
15 |
| -<pre><strong>输入:</strong> <code>"word"</code> |
16 |
| -<strong>输出:</strong> |
17 |
| -["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"] |
| 17 | +但是,这些缩写是无效的: |
| 18 | + |
| 19 | +- <code>"23"</code>(<code>"ab"</code> 变为 <code>"2"</code> ,<code>"cde"</code> 变为 <code>"3"</code> ),选择的子串是相邻的。 |
| 20 | +- <code>"22de"</code>(<code>"ab"</code> 变为 <code>"2"</code> ,<code>"bc"</code> 变为 <code>"2"</code> ),选择的子串重叠了。 |
| 21 | + |
| 22 | +</p> |
| 23 | + |
| 24 | +<p>给你一个字符串 <code>word</code> ,返回一个由所有可能 <strong>广义缩写词</strong> 组成的列表。按 <strong>任意顺序</strong> 返回答案。</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | + |
| 28 | +<p><strong>示例 1:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>输入:</strong>word = "word" |
| 32 | +<strong>输出:</strong>["4","3d","2r1","2rd","1o2","1o1d","1or1","1ord","w3","w2d","w1r1","w1rd","wo2","wo1d","wor1","word"] |
18 | 33 | </pre>
|
19 | 34 |
|
20 |
| -<p> </p> |
| 35 | +<p><strong>示例 2:</strong></p> |
21 | 36 |
|
| 37 | +<pre> |
| 38 | +<strong>输入:</strong>word = "a" |
| 39 | +<strong>输出:</strong>["1","a"] |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | + |
| 44 | +<p><strong>提示:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= word.length <= 15</code></li> |
| 48 | + <li><code>word</code> 仅由小写英文字母组成</li> |
| 49 | +</ul> |
22 | 50 |
|
23 | 51 | ## 解法
|
24 | 52 |
|
| 53 | +回溯法。 |
| 54 | + |
25 | 55 | <!-- 这里可写通用的实现逻辑 -->
|
26 | 56 |
|
27 | 57 | <!-- tabs:start -->
|
|
31 | 61 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
32 | 62 |
|
33 | 63 | ```python
|
34 |
| - |
| 64 | +class Solution: |
| 65 | + def generateAbbreviations(self, word: str) -> List[str]: |
| 66 | + def dfs(s, t): |
| 67 | + if not s: |
| 68 | + ans.append(''.join(t)) |
| 69 | + return |
| 70 | + for i in range(1, len(s) + 1): |
| 71 | + t.append(str(i)) |
| 72 | + if i < len(s): |
| 73 | + t.append(s[i]) |
| 74 | + dfs(s[i + 1:], t) |
| 75 | + t.pop() |
| 76 | + else: |
| 77 | + dfs(s[i:], t) |
| 78 | + t.pop() |
| 79 | + |
| 80 | + t.append(s[0]) |
| 81 | + dfs(s[1:], t) |
| 82 | + t.pop() |
| 83 | + |
| 84 | + ans = [] |
| 85 | + dfs(word, []) |
| 86 | + return ans |
35 | 87 | ```
|
36 | 88 |
|
37 | 89 | ### **Java**
|
38 | 90 |
|
39 | 91 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
40 | 92 |
|
41 | 93 | ```java
|
42 |
| - |
| 94 | +class Solution { |
| 95 | + private List<String> ans; |
| 96 | + |
| 97 | + public List<String> generateAbbreviations(String word) { |
| 98 | + ans = new ArrayList<>(); |
| 99 | + List<String> t = new ArrayList<>(); |
| 100 | + dfs(word, t); |
| 101 | + return ans; |
| 102 | + } |
| 103 | + |
| 104 | + private void dfs(String s, List<String> t) { |
| 105 | + if ("".equals(s)) { |
| 106 | + ans.add(String.join("", t)); |
| 107 | + return; |
| 108 | + } |
| 109 | + for (int i = 1; i < s.length() + 1; ++i) { |
| 110 | + t.add(i + ""); |
| 111 | + if (i < s.length()) { |
| 112 | + t.add(String.valueOf(s.charAt(i))); |
| 113 | + dfs(s.substring(i + 1), t); |
| 114 | + t.remove(t.size() - 1); |
| 115 | + } else { |
| 116 | + dfs(s.substring(i), t); |
| 117 | + } |
| 118 | + t.remove(t.size() - 1); |
| 119 | + } |
| 120 | + t.add(String.valueOf(s.charAt(0))); |
| 121 | + dfs(s.substring(1), t); |
| 122 | + t.remove(t.size() - 1); |
| 123 | + } |
| 124 | +} |
43 | 125 | ```
|
44 | 126 |
|
45 | 127 | ### **...**
|
|
0 commit comments