Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 31da159

Browse files
committed
feat: add solutions to lc problem: No.1087
No.1087.Brace Expansion
1 parent 778311f commit 31da159

File tree

4 files changed

+233
-6
lines changed

4 files changed

+233
-6
lines changed

‎solution/1000-1099/1087.Brace Expansion/README.md‎

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,103 @@
4040
<li>在一对连续的花括号(开花括号与闭花括号)之间的所有字母都不会相同</li>
4141
</ol>
4242

43-
4443
## 解法
4544

4645
<!-- 这里可写通用的实现逻辑 -->
4746

47+
先将字符串 s 进行 convert 转换,比如 `"{a,b}{z,x,y}"` 转换为 `[['a', 'b'], ['z', 'x', 'y']]`,然后利用 DFS 回溯获取每一个单词,放到 ans 中,最后对 ans 进行排序并返回即可。
48+
4849
<!-- tabs:start -->
4950

5051
### **Python3**
5152

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

5455
```python
55-
56+
class Solution:
57+
def expand(self, s: str) -> List[str]:
58+
def convert(s):
59+
if not s:
60+
return
61+
if s[0] == '{':
62+
j = s.find('}')
63+
items.append(s[1: j].split(','))
64+
convert(s[j + 1:])
65+
else:
66+
j = s.find('{')
67+
68+
if j != -1:
69+
items.append(s[: j].split(','))
70+
convert(s[j:])
71+
else:
72+
items.append(s.split(','))
73+
74+
def dfs(i, t):
75+
if i == len(items):
76+
ans.append(''.join(t))
77+
return
78+
for c in items[i]:
79+
t.append(c)
80+
dfs(i + 1, t)
81+
t.pop()
82+
83+
items = []
84+
convert(s)
85+
ans = []
86+
dfs(0, [])
87+
ans.sort()
88+
return ans
5689
```
5790

5891
### **Java**
5992

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

6295
```java
63-
96+
class Solution {
97+
private List<String> ans;
98+
private List<String[]> items;
99+
100+
public String[] expand(String s) {
101+
ans = new ArrayList<>();
102+
items = new ArrayList<>();
103+
convert(s);
104+
dfs(0, new ArrayList<>());
105+
Collections.sort(ans);
106+
return ans.toArray(new String[0]);
107+
}
108+
109+
private void convert(String s) {
110+
if ("".equals(s)) {
111+
return;
112+
}
113+
if (s.charAt(0) == '{') {
114+
int j = s.indexOf("}");
115+
items.add(s.substring(1, j).split(","));
116+
convert(s.substring(j + 1));
117+
} else {
118+
int j = s.indexOf("{");
119+
if (j != -1) {
120+
items.add(s.substring(0, j).split(","));
121+
convert(s.substring(j));
122+
} else {
123+
items.add(s.split(","));
124+
}
125+
}
126+
}
127+
128+
private void dfs(int i, List<String> t) {
129+
if (i == items.size()) {
130+
ans.add(String.join("", t));
131+
return;
132+
}
133+
for (String c : items.get(i)) {
134+
t.add(c);
135+
dfs(i + 1, t);
136+
t.remove(t.size() - 1);
137+
}
138+
}
139+
}
64140
```
65141

66142
### **...**

‎solution/1000-1099/1087.Brace Expansion/README_EN.md‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,95 @@
3434
<li>All characters inside a pair of consecutive opening and ending curly brackets are different.</li>
3535
</ul>
3636

37-
3837
## Solutions
3938

4039
<!-- tabs:start -->
4140

4241
### **Python3**
4342

4443
```python
45-
44+
class Solution:
45+
def expand(self, s: str) -> List[str]:
46+
def convert(s):
47+
if not s:
48+
return
49+
if s[0] == '{':
50+
j = s.find('}')
51+
items.append(s[1: j].split(','))
52+
convert(s[j + 1:])
53+
else:
54+
j = s.find('{')
55+
56+
if j != -1:
57+
items.append(s[: j].split(','))
58+
convert(s[j:])
59+
else:
60+
items.append(s.split(','))
61+
62+
def dfs(i, t):
63+
if i == len(items):
64+
ans.append(''.join(t))
65+
return
66+
for c in items[i]:
67+
t.append(c)
68+
dfs(i + 1, t)
69+
t.pop()
70+
71+
items = []
72+
convert(s)
73+
ans = []
74+
dfs(0, [])
75+
ans.sort()
76+
return ans
4677
```
4778

4879
### **Java**
4980

5081
```java
51-
82+
class Solution {
83+
private List<String> ans;
84+
private List<String[]> items;
85+
86+
public String[] expand(String s) {
87+
ans = new ArrayList<>();
88+
items = new ArrayList<>();
89+
convert(s);
90+
dfs(0, new ArrayList<>());
91+
Collections.sort(ans);
92+
return ans.toArray(new String[0]);
93+
}
94+
95+
private void convert(String s) {
96+
if ("".equals(s)) {
97+
return;
98+
}
99+
if (s.charAt(0) == '{') {
100+
int j = s.indexOf("}");
101+
items.add(s.substring(1, j).split(","));
102+
convert(s.substring(j + 1));
103+
} else {
104+
int j = s.indexOf("{");
105+
if (j != -1) {
106+
items.add(s.substring(0, j).split(","));
107+
convert(s.substring(j));
108+
} else {
109+
items.add(s.split(","));
110+
}
111+
}
112+
}
113+
114+
private void dfs(int i, List<String> t) {
115+
if (i == items.size()) {
116+
ans.add(String.join("", t));
117+
return;
118+
}
119+
for (String c : items.get(i)) {
120+
t.add(c);
121+
dfs(i + 1, t);
122+
t.remove(t.size() - 1);
123+
}
124+
}
125+
}
52126
```
53127

54128
### **...**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
private List<String> ans;
3+
private List<String[]> items;
4+
5+
public String[] expand(String s) {
6+
ans = new ArrayList<>();
7+
items = new ArrayList<>();
8+
convert(s);
9+
dfs(0, new ArrayList<>());
10+
Collections.sort(ans);
11+
return ans.toArray(new String[0]);
12+
}
13+
14+
private void convert(String s) {
15+
if ("".equals(s)) {
16+
return;
17+
}
18+
if (s.charAt(0) == '{') {
19+
int j = s.indexOf("}");
20+
items.add(s.substring(1, j).split(","));
21+
convert(s.substring(j + 1));
22+
} else {
23+
int j = s.indexOf("{");
24+
if (j != -1) {
25+
items.add(s.substring(0, j).split(","));
26+
convert(s.substring(j));
27+
} else {
28+
items.add(s.split(","));
29+
}
30+
}
31+
}
32+
33+
private void dfs(int i, List<String> t) {
34+
if (i == items.size()) {
35+
ans.add(String.join("", t));
36+
return;
37+
}
38+
for (String c : items.get(i)) {
39+
t.add(c);
40+
dfs(i + 1, t);
41+
t.remove(t.size() - 1);
42+
}
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def expand(self, s: str) -> List[str]:
3+
def convert(s):
4+
if not s:
5+
return
6+
if s[0] == '{':
7+
j = s.find('}')
8+
items.append(s[1: j].split(','))
9+
convert(s[j + 1:])
10+
else:
11+
j = s.find('{')
12+
13+
if j != -1:
14+
items.append(s[: j].split(','))
15+
convert(s[j:])
16+
else:
17+
items.append(s.split(','))
18+
19+
def dfs(i, t):
20+
if i == len(items):
21+
ans.append(''.join(t))
22+
return
23+
for c in items[i]:
24+
t.append(c)
25+
dfs(i + 1, t)
26+
t.pop()
27+
28+
items = []
29+
convert(s)
30+
ans = []
31+
dfs(0, [])
32+
ans.sort()
33+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /