Skip to content

Commit 5308064

Browse files
committed
feat: add solutions to lc problem: No.0320
No.0320.Generalized Abbreviation
1 parent 347fa6a commit 5308064

File tree

7 files changed

+223
-19
lines changed

7 files changed

+223
-19
lines changed

solution/0300-0399/0320.Generalized Abbreviation/README.md

+91-9
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,52 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>请你写出一个能够举单词全部缩写的函数。</p>
9+
<p>单词的 <strong>广义缩写词</strong> 可以通过下述步骤构造:先取任意数量的不重叠的子字符串,再用它们各自的长度进行替换。
1010

11-
<p><strong>注意:</strong>输出的顺序并不重要。</p>
11+
例如:
1212

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>)
1416

15-
<pre><strong>输入:</strong> <code>&quot;word&quot;</code>
16-
<strong>输出:</strong>
17-
[&quot;word&quot;, &quot;1ord&quot;, &quot;w1rd&quot;, &quot;wo1d&quot;, &quot;wor1&quot;, &quot;2rd&quot;, &quot;w2d&quot;, &quot;wo2&quot;, &quot;1o1d&quot;, &quot;1or1&quot;, &quot;w1r1&quot;, &quot;1o2&quot;, &quot;2r1&quot;, &quot;3d&quot;, &quot;w3&quot;, &quot;4&quot;]
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"]
1833
</pre>
1934

20-
<p>&nbsp;</p>
35+
<p><strong>示例 2:</strong></p>
2136

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>
2250

2351
## 解法
2452

53+
回溯法。
54+
2555
<!-- 这里可写通用的实现逻辑 -->
2656

2757
<!-- tabs:start -->
@@ -31,15 +61,67 @@
3161
<!-- 这里可写当前语言的特殊实现逻辑 -->
3262

3363
```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
3587
```
3688

3789
### **Java**
3890

3991
<!-- 这里可写当前语言的特殊实现逻辑 -->
4092

4193
```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+
}
43125
```
44126

45127
### **...**

solution/0300-0399/0320.Generalized Abbreviation/README_EN.md

+72-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@
44

55
## Description
66

7-
<p>A word&#39;s&nbsp;<strong>generalized abbreviation</strong>&nbsp;can be constructed by taking any number of non-overlapping substrings and replacing them with their respective lengths. For example, <code>&quot;abcde&quot;</code> can be abbreviated into <code>&quot;a3e&quot;</code> (<code>&quot;bcd&quot;</code> turned into <code>&quot;3&quot;</code>), <code>&quot;1bcd1&quot;</code> (<code>&quot;a&quot;</code> and <code>&quot;e&quot;</code> both turned into <code>&quot;1&quot;</code>), and <code>&quot;23&quot;</code> (<code>&quot;ab&quot;</code> turned into <code>&quot;2&quot;</code> and <code>&quot;cde&quot;</code> turned into <code>&quot;3&quot;</code>).</p>
7+
<p>A word&#39;s <strong>generalized abbreviation</strong> can be constructed by taking any number of <strong>non-overlapping</strong> and <strong>non-adjacent</strong> substrings and replacing them with their respective lengths.</p>
8+
9+
<ul>
10+
<li>For example, <code>&quot;abcde&quot;</code> can be abbreviated into:
11+
<ul>
12+
<li><code>&quot;a3e&quot;</code> (<code>&quot;bcd&quot;</code> turned into <code>&quot;3&quot;</code>)</li>
13+
<li><code>&quot;1bcd1&quot;</code> (<code>&quot;a&quot;</code> and <code>&quot;e&quot;</code> both turned into <code>&quot;1&quot;</code>)</li>
14+
<li><code>&quot;5&quot;</code> (<code>&quot;abcde&quot;</code> turned into <code>&quot;5&quot;</code>)</li>
15+
<li><code>&quot;abcde&quot;</code> (no substrings replaced)</li>
16+
</ul>
17+
</li>
18+
<li>However, these abbreviations are <strong>invalid</strong>:
19+
<ul>
20+
<li><code>&quot;23&quot;</code> (<code>&quot;ab&quot;</code> turned into <code>&quot;2&quot;</code> and <code>&quot;cde&quot;</code> turned into <code>&quot;3&quot;</code>) is invalid as the substrings chosen are adjacent.</li>
21+
<li><code>&quot;22de&quot;</code> (<code>&quot;ab&quot;</code> turned into <code>&quot;2&quot;</code> and <code>&quot;bc&quot;</code> turned into <code>&quot;2&quot;</code>) is invalid as the substring chosen overlap.</li>
22+
</ul>
23+
</li>
24+
</ul>
825

926
<p>Given a string <code>word</code>, return <em>a list of all the possible <strong>generalized abbreviations</strong> of</em> <code>word</code>. Return the answer in <strong>any order</strong>.</p>
1027

@@ -24,21 +41,72 @@
2441
<li><code>word</code> consists of only lowercase English letters.</li>
2542
</ul>
2643

27-
2844
## Solutions
2945

3046
<!-- tabs:start -->
3147

3248
### **Python3**
3349

3450
```python
35-
51+
class Solution:
52+
def generateAbbreviations(self, word: str) -> List[str]:
53+
def dfs(s, t):
54+
if not s:
55+
ans.append(''.join(t))
56+
return
57+
for i in range(1, len(s) + 1):
58+
t.append(str(i))
59+
if i < len(s):
60+
t.append(s[i])
61+
dfs(s[i + 1:], t)
62+
t.pop()
63+
else:
64+
dfs(s[i:], t)
65+
t.pop()
66+
67+
t.append(s[0])
68+
dfs(s[1:], t)
69+
t.pop()
70+
71+
ans = []
72+
dfs(word, [])
73+
return ans
3674
```
3775

3876
### **Java**
3977

4078
```java
41-
79+
class Solution {
80+
private List<String> ans;
81+
82+
public List<String> generateAbbreviations(String word) {
83+
ans = new ArrayList<>();
84+
List<String> t = new ArrayList<>();
85+
dfs(word, t);
86+
return ans;
87+
}
88+
89+
private void dfs(String s, List<String> t) {
90+
if ("".equals(s)) {
91+
ans.add(String.join("", t));
92+
return;
93+
}
94+
for (int i = 1; i < s.length() + 1; ++i) {
95+
t.add(i + "");
96+
if (i < s.length()) {
97+
t.add(String.valueOf(s.charAt(i)));
98+
dfs(s.substring(i + 1), t);
99+
t.remove(t.size() - 1);
100+
} else {
101+
dfs(s.substring(i), t);
102+
}
103+
t.remove(t.size() - 1);
104+
}
105+
t.add(String.valueOf(s.charAt(0)));
106+
dfs(s.substring(1), t);
107+
t.remove(t.size() - 1);
108+
}
109+
}
42110
```
43111

44112
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
private List<String> ans;
3+
4+
public List<String> generateAbbreviations(String word) {
5+
ans = new ArrayList<>();
6+
List<String> t = new ArrayList<>();
7+
dfs(word, t);
8+
return ans;
9+
}
10+
11+
private void dfs(String s, List<String> t) {
12+
if ("".equals(s)) {
13+
ans.add(String.join("", t));
14+
return;
15+
}
16+
for (int i = 1; i < s.length() + 1; ++i) {
17+
t.add(i + "");
18+
if (i < s.length()) {
19+
t.add(String.valueOf(s.charAt(i)));
20+
dfs(s.substring(i + 1), t);
21+
t.remove(t.size() - 1);
22+
} else {
23+
dfs(s.substring(i), t);
24+
}
25+
t.remove(t.size() - 1);
26+
}
27+
t.add(String.valueOf(s.charAt(0)));
28+
dfs(s.substring(1), t);
29+
t.remove(t.size() - 1);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def generateAbbreviations(self, word: str) -> List[str]:
3+
def dfs(s, t):
4+
if not s:
5+
ans.append(''.join(t))
6+
return
7+
for i in range(1, len(s) + 1):
8+
t.append(str(i))
9+
if i < len(s):
10+
t.append(s[i])
11+
dfs(s[i + 1:], t)
12+
t.pop()
13+
else:
14+
dfs(s[i:], t)
15+
t.pop()
16+
17+
t.append(s[0])
18+
dfs(s[1:], t)
19+
t.pop()
20+
21+
ans = []
22+
dfs(word, [])
23+
return ans

solution/0900-0999/0911.Online Election/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ class TopVotedCandidate:
7676
mx = cur = 0
7777
counter = Counter()
7878
self.times = times
79-
self.wins = [0] * len(persons)
79+
self.wins = []
8080
for i, p in enumerate(persons):
8181
counter[p] += 1
8282
if counter[p] >= mx:
8383
mx, cur = counter[p], p
84-
self.wins[i] = cur
84+
self.wins.append(cur)
8585

8686
def q(self, t: int) -> int:
8787
left, right = 0, len(self.wins) - 1

solution/0900-0999/0911.Online Election/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ class TopVotedCandidate:
6464
mx = cur = 0
6565
counter = Counter()
6666
self.times = times
67-
self.wins = [0] * len(persons)
67+
self.wins = []
6868
for i, p in enumerate(persons):
6969
counter[p] += 1
7070
if counter[p] >= mx:
7171
mx, cur = counter[p], p
72-
self.wins[i] = cur
72+
self.wins.append(cur)
7373

7474
def q(self, t: int) -> int:
7575
left, right = 0, len(self.wins) - 1

solution/0900-0999/0911.Online Election/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ def __init__(self, persons: List[int], times: List[int]):
44
mx = cur = 0
55
counter = Counter()
66
self.times = times
7-
self.wins = [0] * len(persons)
7+
self.wins = []
88
for i, p in enumerate(persons):
99
counter[p] += 1
1010
if counter[p] >= mx:
1111
mx, cur = counter[p], p
12-
self.wins[i] = cur
12+
self.wins.append(cur)
1313

1414
def q(self, t: int) -> int:
1515
left, right = 0, len(self.wins) - 1

0 commit comments

Comments
 (0)