|
| 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