-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1096. Brace Expansion II.java
96 lines (90 loc) · 3.05 KB
/
1096. Brace Expansion II.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
//My Solution: Parse recursively. if char is letter append it.
// if char is { concatenate cur with next
// if char is , finish the build of current item, add item or cur set into ans
// if char is } finsih the build of current item, add item or cur set into ans and break recursion
class Solution {
char[] chs;
int idx = 0;
char[] brace = {'{', '}'};
public List<String> braceExpansionII(String expression) {
chs = expression.toCharArray();
List<String> ans = new ArrayList(parse());
Collections.sort(ans);
return ans;
}
private Set<String> parse(){
Set<String> ans = new HashSet();
Set<String> cur = new HashSet();
StringBuilder item = new StringBuilder();
while(idx < chs.length){
if('a' <= chs[idx] && chs[idx] <= 'z'){
item.append(chs[idx]);
}else if(chs[idx] == brace[0]){
++idx;
if(item.length() > 0){
cur = concatenate(cur, item);
item.setLength(0);
}
Set<String> next = parse();
cur = concatenate(cur, next);
}else if(chs[idx] == brace[1]){
if(item.length() > 0){
cur = concatenate(cur, item);
item.setLength(0);
}
if(!cur.isEmpty()){
ans.addAll(cur);
cur.clear();
}
break;
}else{
if(item.length() > 0){
cur = concatenate(cur, item);
item.setLength(0);
}
if(!cur.isEmpty()){
ans.addAll(cur);
cur.clear();
}
}
++idx;
}
if(idx == chs.length){
if(item.length() > 0){
cur = concatenate(cur, item);
}
if(!cur.isEmpty()){
ans.addAll(cur);
}
}
return ans;
}
private Set<String> concatenate(Set<String> left, Set<String> right){
if(left.size() == 0) return new HashSet(right);
Set<String> ans = new HashSet();
StringBuilder sb = new StringBuilder();
for(String prefix : left){
sb = new StringBuilder(prefix);
for(String latter : right){
sb.append(latter);
ans.add(sb.toString());
sb.setLength(prefix.length());
}
}
return ans;
}
private Set<String> concatenate(Set<String> left, StringBuilder right){
Set<String> ans = new HashSet();
if(left.isEmpty()) {
ans.add(right.toString());
return ans;
}
StringBuilder sb = new StringBuilder();
for(String prefix : left){
sb = new StringBuilder(prefix);
sb.append(right);
ans.add(sb.toString());
}
return ans;
}
}