Skip to content

Commit 014bbc3

Browse files
committed
feat: update solutions to lc problem: No.1096
No.1096.Brace Expansion II
1 parent 2974106 commit 014bbc3

File tree

5 files changed

+50
-68
lines changed

5 files changed

+50
-68
lines changed

solution/1000-1099/1096.Brace Expansion II/README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@
7373

7474
**方法一:递归**
7575

76-
设计递归函数 `dfs(exp)`
76+
我们设计一个递归函数 $dfs(exp)$,用于处理表达式 $exp$,并将结果存入集合 $s$ 中。
7777

78-
- 如果 `exp` 不包含 `{}`,将 `exp` 中的字符加入结果集 `s` 中,返回;
79-
- 否则,找到第一个 `}` 的位置 $j$,从 $j$ 往前找到第一个 `{` 的位置 $i$,将 $exp[i+1..j-1]$ 之间的字符串按照 `,` 分割,得到一个字符串数组 `bs`。将 `exp[0..i-1]``exp[j+1..]` 作为前缀 $a$ 和后缀 $c$,分别与 `bs` 中的每个字符串 $b$ 拼接,然后递归调用 `dfs(a+b+c)`
78+
对于表达式 $exp$,我们首先找到第一个右花括号的位置 $j$,如果 $j = -1$,说明 $exp$ 中没有右花括号,即 $exp$ 为单一元素,直接将 $exp$ 加入集合 $s$ 中即可。
8079

81-
最后对结果集排序,返回。
80+
否则,我们从位置 $j$ 开始往左找到第一个左花括号的位置 $i$,此时 $exp[:i]$ 和 $exp[j + 1:]$ 分别为 $exp$ 的前缀和后缀,记为 $a$ 和 $c$。而 $exp[i + 1: j]$ 为 $exp$ 中花括号内的部分,即 $exp$ 中的子表达式,我们将其按照逗号分割成多个字符串 $b_1, b_2, \cdots, b_k$,然后对每个 $b_i$,我们将 $a + b_i + c$ 拼接成新的表达式,递归调用 $dfs$ 函数处理新的表达式,即 $dfs(a + b_i + c)$。
81+
82+
最后,我们将集合 $s$ 中的元素按照字典序排序,即可得到答案。
83+
84+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为表达式 $expression$ 的长度。
8285

8386
<!-- tabs:start -->
8487

@@ -94,10 +97,8 @@ class Solution:
9497
if j == -1:
9598
s.add(exp)
9699
return
97-
i = j
98-
while exp[i] != '{':
99-
i -= 1
100-
a, c, = exp[:i], exp[j + 1:]
100+
i = exp.rfind('{', 0, j - 1)
101+
a, c = exp[:i], exp[j + 1:]
101102
for b in exp[i + 1: j].split(','):
102103
dfs(a + b + c)
103104

@@ -143,22 +144,21 @@ class Solution {
143144
```cpp
144145
class Solution {
145146
public:
146-
set<string> s;
147147
vector<string> braceExpansionII(string expression) {
148148
dfs(expression);
149149
return vector<string>(s.begin(), s.end());
150150
}
151151

152+
private:
153+
set<string> s;
154+
152155
void dfs(string exp) {
153-
int j = exp.find('}');
154-
if (j == -1) {
156+
int j = exp.find_first_of('}');
157+
if (j == string::npos) {
155158
s.insert(exp);
156159
return;
157160
}
158-
int i = j;
159-
while (exp[i] != '{') {
160-
--i;
161-
}
161+
int i = exp.rfind('{', j);
162162
string a = exp.substr(0, i);
163163
string c = exp.substr(j + 1);
164164
stringstream ss(exp.substr(i + 1, j - i - 1));
@@ -175,26 +175,23 @@ public:
175175
```go
176176
func braceExpansionII(expression string) []string {
177177
s := map[string]struct{}{}
178-
var dfs func(exp string)
178+
var dfs func(string)
179179
dfs = func(exp string) {
180-
j := strings.IndexByte(exp, '}')
180+
j := strings.Index(exp, "}")
181181
if j == -1 {
182182
s[exp] = struct{}{}
183183
return
184184
}
185-
i := j
186-
for exp[i] != '{' {
187-
i--
188-
}
185+
i := strings.LastIndex(exp[:j], "{")
189186
a, c := exp[:i], exp[j+1:]
190187
for _, b := range strings.Split(exp[i+1:j], ",") {
191188
dfs(a + b + c)
192189
}
193190
}
194191
dfs(expression)
195-
ans := []string{}
196-
for v := range s {
197-
ans = append(ans, v)
192+
ans := make([]string, 0, len(s))
193+
for k := range s {
194+
ans = append(ans, k)
198195
}
199196
sort.Strings(ans)
200197
return ans

solution/1000-1099/1096.Brace Expansion II/README_EN.md

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ class Solution:
7878
if j == -1:
7979
s.add(exp)
8080
return
81-
i = j
82-
while exp[i] != '{':
83-
i -= 1
84-
a, c, = exp[:i], exp[j + 1:]
81+
i = exp.rfind('{', 0, j - 1)
82+
a, c = exp[:i], exp[j + 1:]
8583
for b in exp[i + 1: j].split(','):
8684
dfs(a + b + c)
8785

@@ -125,22 +123,21 @@ class Solution {
125123
```cpp
126124
class Solution {
127125
public:
128-
set<string> s;
129126
vector<string> braceExpansionII(string expression) {
130127
dfs(expression);
131128
return vector<string>(s.begin(), s.end());
132129
}
133130

131+
private:
132+
set<string> s;
133+
134134
void dfs(string exp) {
135-
int j = exp.find('}');
136-
if (j == -1) {
135+
int j = exp.find_first_of('}');
136+
if (j == string::npos) {
137137
s.insert(exp);
138138
return;
139139
}
140-
int i = j;
141-
while (exp[i] != '{') {
142-
--i;
143-
}
140+
int i = exp.rfind('{', j);
144141
string a = exp.substr(0, i);
145142
string c = exp.substr(j + 1);
146143
stringstream ss(exp.substr(i + 1, j - i - 1));
@@ -157,26 +154,23 @@ public:
157154
```go
158155
func braceExpansionII(expression string) []string {
159156
s := map[string]struct{}{}
160-
var dfs func(exp string)
157+
var dfs func(string)
161158
dfs = func(exp string) {
162-
j := strings.IndexByte(exp, '}')
159+
j := strings.Index(exp, "}")
163160
if j == -1 {
164161
s[exp] = struct{}{}
165162
return
166163
}
167-
i := j
168-
for exp[i] != '{' {
169-
i--
170-
}
164+
i := strings.LastIndex(exp[:j], "{")
171165
a, c := exp[:i], exp[j+1:]
172166
for _, b := range strings.Split(exp[i+1:j], ",") {
173167
dfs(a + b + c)
174168
}
175169
}
176170
dfs(expression)
177-
ans := []string{}
178-
for v := range s {
179-
ans = append(ans, v)
171+
ans := make([]string, 0, len(s))
172+
for k := range s {
173+
ans = append(ans, k)
180174
}
181175
sort.Strings(ans)
182176
return ans

solution/1000-1099/1096.Brace Expansion II/Solution.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
class Solution {
22
public:
3-
set<string> s;
43
vector<string> braceExpansionII(string expression) {
54
dfs(expression);
65
return vector<string>(s.begin(), s.end());
76
}
87

8+
private:
9+
set<string> s;
10+
911
void dfs(string exp) {
10-
int j = exp.find('}');
11-
if (j == -1) {
12+
int j = exp.find_first_of('}');
13+
if (j == string::npos) {
1214
s.insert(exp);
1315
return;
1416
}
15-
int i = j;
16-
while (exp[i] != '{') {
17-
--i;
18-
}
17+
int i = exp.rfind('{', j);
1918
string a = exp.substr(0, i);
2019
string c = exp.substr(j + 1);
2120
stringstream ss(exp.substr(i + 1, j - i - 1));

solution/1000-1099/1096.Brace Expansion II/Solution.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
func braceExpansionII(expression string) []string {
22
s := map[string]struct{}{}
3-
var dfs func(exp string)
3+
var dfs func(string)
44
dfs = func(exp string) {
5-
j := strings.IndexByte(exp, '}')
5+
j := strings.Index(exp, "}")
66
if j == -1 {
77
s[exp] = struct{}{}
88
return
99
}
10-
i := j
11-
for exp[i] != '{' {
12-
i--
13-
}
10+
i := strings.LastIndex(exp[:j], "{")
1411
a, c := exp[:i], exp[j+1:]
1512
for _, b := range strings.Split(exp[i+1:j], ",") {
1613
dfs(a + b + c)
1714
}
1815
}
1916
dfs(expression)
20-
ans := []string{}
21-
for v := range s {
22-
ans = append(ans, v)
17+
ans := make([]string, 0, len(s))
18+
for k := range s {
19+
ans = append(ans, k)
2320
}
2421
sort.Strings(ans)
2522
return ans

solution/1000-1099/1096.Brace Expansion II/Solution.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ def dfs(exp):
55
if j == -1:
66
s.add(exp)
77
return
8-
i = j
9-
while exp[i] != '{':
10-
i -= 1
11-
a, c, = (
12-
exp[:i],
13-
exp[j + 1 :],
14-
)
15-
for b in exp[i + 1 : j].split(','):
8+
i = exp.rfind('{', 0, j - 1)
9+
a, c = exp[:i], exp[j + 1:]
10+
for b in exp[i + 1: j].split(','):
1611
dfs(a + b + c)
1712

1813
s = set()

0 commit comments

Comments
 (0)