Skip to content

Commit 10875dd

Browse files
add a solution for 1087
1 parent 81cb7fe commit 10875dd

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/main/java/com/fishercoder/solutions/_1087.java

+48-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Collections;
56
import java.util.List;
67

78
public class _1087 {
89
public static class Solution1 {
9-
public String[] expand(String S) {
10-
List<char[]> letters = parse(S);
10+
public String[] expand(String s) {
11+
List<char[]> letters = parse(s);
1112
List<String> result = backtracking(letters, 0, new StringBuilder(), new ArrayList<>());
1213
String[] r = result.stream().toArray(String[]::new);
1314
Arrays.sort(r);
@@ -51,4 +52,49 @@ private List<char[]> parse(String s) {
5152
return result;
5253
}
5354
}
55+
56+
public static class Solution2 {
57+
/**
58+
* My completely original solution on 1/17/2022.
59+
*/
60+
public String[] expand(String s) {
61+
List<String> list = new ArrayList<>();
62+
list.add("");
63+
for (int i = 0; i < s.length(); i++) {
64+
List<String> newList = new ArrayList<>();
65+
if (s.charAt(i) == '{') {
66+
int j = i + 1;
67+
while (s.charAt(j) != '}') {
68+
j++;
69+
}
70+
String s2 = s.substring(i + 1, j);
71+
String[] chars = s2.split("\\,");
72+
for (String c : chars) {
73+
for (String sb : list) {
74+
sb += c;
75+
newList.add(sb);
76+
}
77+
}
78+
i = j;
79+
} else {
80+
for (String sb : list) {
81+
sb += s.charAt(i);
82+
newList.add(sb);
83+
}
84+
}
85+
list.clear();
86+
list.addAll(newList);
87+
}
88+
List<String> res = new ArrayList<>();
89+
for (String sb : list) {
90+
res.add(sb);
91+
}
92+
Collections.sort(res);
93+
String[] ans = new String[res.size()];
94+
for (int i = 0; i < res.size(); i++) {
95+
ans[i] = res.get(i);
96+
}
97+
return ans;
98+
}
99+
}
54100
}

src/test/java/com/fishercoder/_1087Test.java

+5
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,30 @@
88

99
public class _1087Test {
1010
private static _1087.Solution1 solution1;
11+
private static _1087.Solution2 solution2;
1112

1213
@BeforeClass
1314
public static void setup() {
1415
solution1 = new _1087.Solution1();
16+
solution2 = new _1087.Solution2();
1517
}
1618

1719
@Test
1820
public void test1() {
1921
assertArrayEquals(new String[]{"ade", "adf", "bde", "bdf", "cde", "cdf"}, solution1.expand("{a,b,c}d{e,f}"));
22+
assertArrayEquals(new String[]{"ade", "adf", "bde", "bdf", "cde", "cdf"}, solution2.expand("{a,b,c}d{e,f}"));
2023
}
2124

2225
@Test
2326
public void test2() {
2427
assertArrayEquals(new String[]{"abcd"}, solution1.expand("abcd"));
28+
assertArrayEquals(new String[]{"abcd"}, solution2.expand("abcd"));
2529
}
2630

2731
@Test
2832
public void test3() {
2933
assertArrayEquals(new String[]{"acdf", "acef", "bcdf", "bcef"}, solution1.expand("{a,b}c{d,e}f"));
34+
assertArrayEquals(new String[]{"acdf", "acef", "bcdf", "bcef"}, solution2.expand("{a,b}c{d,e}f"));
3035
}
3136

3237
}

0 commit comments

Comments
 (0)