forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrace Expansion.java
More file actions
47 lines (45 loc) · 1.42 KB
/
Brace Expansion.java
File metadata and controls
47 lines (45 loc) · 1.42 KB
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
class Solution {
public String[] expand(String s) {
List<List<String>> splits = parseInput(s);
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
helper(splits, result, sb, 0);
Collections.sort(result);
String[] answer = new String[result.size()];
for (int i = 0; i < result.size(); i++) {
answer[i] = result.get(i);
}
return answer;
}
private void helper(List<List<String>> splits, List<String> result, StringBuilder sb, int idx) {
if (idx >= splits.size()) {
if (sb.length() == splits.size()) {
result.add(new StringBuilder(sb.toString()).toString());
}
} else {
for (int i = idx; i < splits.size(); i++) {
List<String> currentSplit = splits.get(i);
for (String section : currentSplit) {
sb.append(section);
helper(splits, result, sb, i + 1);
sb.deleteCharAt(sb.length() - 1);
}
}
}
}
private List<List<String>> parseInput(String s) {
List<List<String>> splits = new ArrayList<>();
int idx = 0;
while (idx < s.length()) {
if (s.charAt(idx) == '{') {
int endIdx = s.indexOf('}', idx);
String[] segements = s.substring(idx + 1, endIdx).split(",");
splits.add(Arrays.asList(segements));
idx = endIdx + 1;
} else {
splits.add(List.of(String.valueOf(s.charAt(idx++))));
}
}
return splits;
}
}