Skip to content

Commit 3da4dcd

Browse files
add 1087
1 parent c2cf068 commit 3da4dcd

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ _If you like this project, please leave me a star._ ★
9898
|1100|[Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1100.java) | |Medium|String, Sliding Window|
9999
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
100100
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | |Easy||
101+
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1087.java) | |Medium|Backtracking|
101102
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc)|Easy||
102103
|1085|[Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1085.java)| [:tv:](https://www.youtube.com/watch?v=GKYmPuHZpQg)|Easy||
103104
|1079|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1079.java)| |Medium||
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* 1087. Brace Expansion
9+
*
10+
* A string S represents a list of words.
11+
* Each letter in the word has 1 or more options. If there is one option, the letter is represented as is.
12+
* If there is more than one option, then curly braces delimit the options.
13+
* For example, "{a,b,c}" represents options ["a", "b", "c"].
14+
* For example, "{a,b,c}d{e,f}" represents the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].
15+
*
16+
* Return all words that can be formed in this manner, in lexicographical order.
17+
*
18+
* Example 1:
19+
* Input: "{a,b}c{d,e}f"
20+
* Output: ["acdf","acef","bcdf","bcef"]
21+
*
22+
* Example 2:
23+
* Input: "abcd"
24+
* Output: ["abcd"]
25+
*
26+
* Note:
27+
* 1 <= S.length <= 50
28+
* There are no nested curly brackets.
29+
* All characters inside a pair of consecutive opening and ending curly brackets are different.
30+
* */
31+
public class _1087 {
32+
public static class Solution1 {
33+
public String[] expand(String S) {
34+
List<char[]> letters = parse(S);
35+
List<String> result = backtracking(letters, 0, new StringBuilder(), new ArrayList<>());
36+
String[] r = result.stream().toArray(String[]::new);
37+
Arrays.sort(r);
38+
return r;
39+
}
40+
41+
private List<String> backtracking(List<char[]> letters, int start, StringBuilder sb, List<String> result) {
42+
if (start >= letters.size()) {
43+
result.add(sb.toString());
44+
return result;
45+
}
46+
char[] chars = letters.get(start);
47+
for (int i = 0; i < chars.length; i++) {
48+
sb.append(chars[i]);
49+
backtracking(letters, start + 1, sb, result);
50+
sb.setLength(sb.length() - 1);
51+
}
52+
return result;
53+
}
54+
55+
private List<char[]> parse(String s) {
56+
List<char[]> result = new ArrayList<>();
57+
for (int i = 0; i < s.length(); i++) {
58+
if (s.charAt(i) == '{') {
59+
int start = ++i;
60+
while (i < s.length() && s.charAt(i) != '}') {
61+
i++;
62+
}
63+
String[] strings = s.substring(start, i).split(",");
64+
char[] chars = new char[strings.length];
65+
for (int j = 0; j < strings.length; j++) {
66+
chars[j] = strings[j].charAt(0);
67+
}
68+
result.add(chars);
69+
} else {
70+
char[] chars = new char[1];
71+
chars[0] = s.charAt(i);
72+
result.add(chars);
73+
}
74+
}
75+
return result;
76+
}
77+
}
78+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1087;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1087Test {
10+
private static _1087.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1087.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new String[]{"ade", "adf", "bde", "bdf", "cde", "cdf"}, solution1.expand("{a,b,c}d{e,f}"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new String[]{"abcd"}, solution1.expand("abcd"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertArrayEquals(new String[]{"acdf", "acef", "bcdf", "bcef"}, solution1.expand("{a,b}c{d,e}f"));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)