Skip to content

Commit 3235361

Browse files
committed
Add solution #1096
1 parent fd52e46 commit 3235361

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,134 LeetCode solutions in JavaScript
1+
# 1,135 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -876,6 +876,7 @@
876876
1093|[Statistics from a Large Sample](./solutions/1093-statistics-from-a-large-sample.js)|Medium|
877877
1094|[Car Pooling](./solutions/1094-car-pooling.js)|Medium|
878878
1095|[Find in Mountain Array](./solutions/1095-find-in-mountain-array.js)|Hard|
879+
1096|[Brace Expansion II](./solutions/1096-brace-expansion-ii.js)|Hard|
879880
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
880881
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|
881882
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|

solutions/1096-brace-expansion-ii.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* 1096. Brace Expansion II
3+
* https://leetcode.com/problems/brace-expansion-ii/
4+
* Difficulty: Hard
5+
*
6+
* Under the grammar given below, strings can represent a set of lowercase words. Let R(expr)
7+
* denote the set of words the expression represents.
8+
*
9+
* The grammar can best be understood through simple examples:
10+
* - Single letters represent a singleton set containing that word.
11+
* - R("a") = {"a"}
12+
* - R("w") = {"w"}
13+
* - When we take a comma-delimited list of two or more expressions, we take the union of
14+
* possibilities.
15+
* - R("{a,b,c}") = {"a","b","c"}
16+
* - R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word
17+
* at most once)
18+
* - When we concatenate two expressions, we take the set of possible concatenations between two
19+
* words where the first word comes from the first expression and the second word comes from
20+
* the second expression.
21+
* - R("{a,b}{c,d}") = {"ac","ad","bc","bd"}
22+
* - R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh",
23+
* "acefg", "acefh"}
24+
*
25+
* Formally, the three rules for our grammar:
26+
* - For every lowercase letter x, we have R(x) = {x}.
27+
* - For expressions e1, e2, ... , ek with k >= 2, we have R({e1, e2, ...}) = R(e1) ∪ R(e2) ∪ ...
28+
* - For expressions e1 and e2, we have R(e1 + e2) = {a + b for (a, b) in R(e1) × R(e2)},
29+
* where + denotes concatenation, and × denotes the cartesian product.
30+
*
31+
* Given an expression representing a set of words under the given grammar, return the sorted list
32+
* of words that the expression represents.
33+
*/
34+
35+
/**
36+
* @param {string} expression
37+
* @return {string[]}
38+
*/
39+
var braceExpansionII = function(expression) {
40+
function cartesian(set1, set2) {
41+
const result = new Set();
42+
for (const a of set1) {
43+
for (const b of set2) {
44+
result.add(a + b);
45+
}
46+
}
47+
return result;
48+
}
49+
50+
function evaluate(exp) {
51+
let i = 0;
52+
53+
function parseUnit() {
54+
let result = new Set();
55+
56+
if (exp[i] === '{') {
57+
i++;
58+
result = parseExprList();
59+
i++;
60+
} else {
61+
result.add(exp[i]);
62+
i++;
63+
}
64+
65+
return result;
66+
}
67+
68+
function parseExprList() {
69+
const result = new Set();
70+
71+
const firstExpr = parseExpr();
72+
for (const word of firstExpr) {
73+
result.add(word);
74+
}
75+
76+
while (i < exp.length && exp[i] === ',') {
77+
i++;
78+
const nextExpr = parseExpr();
79+
for (const word of nextExpr) {
80+
result.add(word);
81+
}
82+
}
83+
84+
return result;
85+
}
86+
87+
function parseExpr() {
88+
let result = new Set(['']);
89+
90+
while (i < exp.length && exp[i] !== ',' && exp[i] !== '}') {
91+
const unit = parseUnit();
92+
result = cartesian(result, unit);
93+
}
94+
95+
return result;
96+
}
97+
98+
return parseExprList();
99+
}
100+
101+
return [...evaluate(expression)].sort();
102+
};

0 commit comments

Comments
 (0)