-
Notifications
You must be signed in to change notification settings - Fork 43
/
BraceExpansion.java
182 lines (162 loc) · 4.85 KB
/
BraceExpansion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package LeetCodeJava.DFS;
// https://leetcode.com/problems/brace-expansion/description/
// https://leetcode.ca/2018-11-21-1087-Brace-Expansion/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* 1087. Brace Expansion
* Description
* You are given a string s representing a list of words. Each letter in the word has one or more options.
*
* If there is one option, the letter is represented as is.
* If there is more than one option, then curly braces delimit the options. For example, "{a,b,c}" represents options ["a", "b", "c"].
* For example, if s = "a{b,c}", the first character is always 'a', but the second character can be 'b' or 'c'. The original list is ["ab", "ac"].
*
* Return all words that can be formed in this manner, sorted in lexicographical order.
*
*
*
* Example 1:
*
* Input: s = "{a,b}c{d,e}f"
* Output: ["acdf","acef","bcdf","bcef"]
* Example 2:
*
* Input: s = "abcd"
* Output: ["abcd"]
*
*
* Constraints:
*
* 1 <= s.length <= 50
* s consists of curly brackets '{}', commas ',', and lowercase English letters.
* s is guaranteed to be a valid input.
* There are no nested curly brackets.
* All characters inside a pair of consecutive opening and ending curly brackets are different.
*
*/
public class BraceExpansion {
// V0
// TODO : implement, validate
// public String[] expand(String s) {
// }
// V1
// IDEA : DFS
// https://leetcode.ca/2018-11-21-1087-Brace-Expansion/
private List<String> ans;
private List<String[]> items;
public String[] expand_1(String s) {
ans = new ArrayList<>();
items = new ArrayList<>();
convert(s);
System.out.println(">>> items = ");
/**
*
* NOTE !!!
*
* exp 1:
*
* Input: s = "{a,b}c{d,e}f"
* Output: ["acdf","acef","bcdf","bcef"]
*
* so,
* >>> items =
* [a, b]
* [c]
* [d, e]
* [f]
*/
for (String[] item : items){
System.out.println(Arrays.toString(item));
}
dfs(0, new ArrayList<>());
Collections.sort(ans);
return ans.toArray(new String[0]);
}
private void convert(String s) {
if ("".equals(s)) {
return;
}
if (s.charAt(0) == '{') {
int j = s.indexOf("}");
items.add(s.substring(1, j).split(","));
convert(s.substring(j + 1));
} else {
int j = s.indexOf("{");
if (j != -1) {
items.add(s.substring(0, j).split(","));
convert(s.substring(j));
} else {
items.add(s.split(","));
}
}
}
// backtrack
/**
* NOTE !!!
*
* once we get items, then the problem
* is transformed as "how to get all collections
* from each item from items", a classic backtrack pattern
*
* dfs(int i, List<String> t)
* i is "starting index" we use it and "undo" to collect
* all item from items
*/
private void dfs(int i, List<String> t) {
if (i == items.size()) {
ans.add(String.join("", t));
return;
}
for (String c : items.get(i)) {
t.add(c);
dfs(i + 1, t);
// undo
t.remove(t.size() - 1);
}
}
// V2
// IDEA : DFS
// https://walkccc.me/LeetCode/problems/1087/#__tabbed_1_2
public String[] expand_2(String s) {
List<String> ans = new ArrayList<>();
dfs_2(s, 0, new StringBuilder(), ans);
Collections.sort(ans);
return ans.toArray(new String[0]);
}
private void dfs_2(final String s, int i, StringBuilder sb, List<String> ans) {
if (i == s.length()) {
ans.add(sb.toString());
return;
}
if (s.charAt(i) == '{') {
final int nextRightBraceIndex = s.indexOf("}", i);
for (final String c : s.substring(i + 1, nextRightBraceIndex).split(",")) {
sb.append(c);
dfs_2(s, nextRightBraceIndex + 1, sb, ans);
sb.deleteCharAt(sb.length() - 1);
}
} else { // s[i] != '{'
sb.append(s.charAt(i));
dfs_2(s, i + 1, sb, ans);
sb.deleteCharAt(sb.length() - 1);
}
}
// V3
// IDEA : DFS
// https://blog.csdn.net/qq_46105170/article/details/108840420
// V4
// IDEA : DFS
// https://blog.csdn.net/qq_21201267/article/details/107541722
// testing
// public static void main(String[] args) {
// BraceExpansion be = new BraceExpansion();
// String input = "{a,b}c{d,e}f";
// String[] res = be.expand(input);
// for(String x : Arrays.asList(res)){
// System.out.println(x);
// }
// }
}