Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Under the grammar given below, strings can represent a set of lowercase words. Let R(expr) denote the set of words the expression represents.
The grammar can best be understood through simple examples:
R("a") = {"a"}R("w") = {"w"}R("{a,b,c}") = {"a","b","c"}R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word at most once)R("{a,b}{c,d}") = {"ac","ad","bc","bd"}R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}Formally, the three rules for our grammar:
x, we have R(x) = {x}.e1, e2, ... , ek with k >= 2, we have R({e1, e2, ...}) = R(e1) ∪ R(e2) ∪ ...e1 and e2, we have R(e1 + e2) = {a + b for (a, b) in R(e1) × R(e2)}, where + denotes concatenation, and × denotes the cartesian product.Given an expression representing a set of words under the given grammar, return the sorted list of words that the expression represents.
Example 1:
Input: expression = "{a,b}{c,{d,e}}"
Output: ["ac","ad","ae","bc","bd","be"]
Example 2:
Input: expression = "{{a,z},a{b,c},{ab,z}}"
Output: ["a","ab","ac","z"]
Explanation: Each distinct word is written only once in the final answer.
Constraints:
1 <= expression.length <= 60expression[i] consists of '{', '}', ','or lowercase English letters.expression represents a set of words based on the grammar given in the description.Problem summary: Under the grammar given below, strings can represent a set of lowercase words. Let R(expr) denote the set of words the expression represents. The grammar can best be understood through simple examples: Single letters represent a singleton set containing that word. R("a") = {"a"} R("w") = {"w"} When we take a comma-delimited list of two or more expressions, we take the union of possibilities. R("{a,b,c}") = {"a","b","c"} R("{{a,b},{b,c}}") = {"a","b","c"} (notice the final set only contains each word at most once) When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression. R("{a,b}{c,d}") = {"ac","ad","bc","bd"} R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"} Formally, the three rules for our
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Backtracking · Stack
"{a,b}{c,{d,e}}""{{a,z},a{b,c},{ab,z}}"brace-expansion)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1096: Brace Expansion II
class Solution {
private TreeSet<String> s = new TreeSet<>();
public List<String> braceExpansionII(String expression) {
dfs(expression);
return new ArrayList<>(s);
}
private void dfs(String exp) {
int j = exp.indexOf('}');
if (j == -1) {
s.add(exp);
return;
}
int i = exp.lastIndexOf('{', j);
String a = exp.substring(0, i);
String c = exp.substring(j + 1);
for (String b : exp.substring(i + 1, j).split(",")) {
dfs(a + b + c);
}
}
}
// Accepted solution for LeetCode #1096: Brace Expansion II
func braceExpansionII(expression string) []string {
s := map[string]struct{}{}
var dfs func(string)
dfs = func(exp string) {
j := strings.Index(exp, "}")
if j == -1 {
s[exp] = struct{}{}
return
}
i := strings.LastIndex(exp[:j], "{")
a, c := exp[:i], exp[j+1:]
for _, b := range strings.Split(exp[i+1:j], ",") {
dfs(a + b + c)
}
}
dfs(expression)
ans := make([]string, 0, len(s))
for k := range s {
ans = append(ans, k)
}
sort.Strings(ans)
return ans
}
# Accepted solution for LeetCode #1096: Brace Expansion II
class Solution:
def braceExpansionII(self, expression: str) -> List[str]:
def dfs(exp):
j = exp.find('}')
if j == -1:
s.add(exp)
return
i = exp.rfind('{', 0, j - 1)
a, c = exp[:i], exp[j + 1 :]
for b in exp[i + 1 : j].split(','):
dfs(a + b + c)
s = set()
dfs(expression)
return sorted(s)
// Accepted solution for LeetCode #1096: Brace Expansion II
struct Solution;
use std::collections::HashSet;
use std::iter::FromIterator;
use std::iter::Peekable;
use std::str::Chars;
use std::vec::IntoIter;
#[derive(Debug)]
enum Tok {
S(String),
Op(char),
}
impl Solution {
fn brace_expansion_ii(expression: String) -> Vec<String> {
let mut it = expression.chars().peekable();
let toks = Self::parse_tokens(&mut it);
let mut it = toks.into_iter().peekable();
let mut sorted: Vec<String> = Self::parse(&mut it).into_iter().collect();
sorted.sort_unstable();
sorted
}
fn concat(a: &HashSet<String>, b: &HashSet<String>) -> HashSet<String> {
let mut res = HashSet::new();
for i in a {
for j in b {
res.insert(format!("{}{}", i, j));
}
}
res
}
fn parse(it: &mut Peekable<IntoIter<Tok>>) -> HashSet<String> {
let mut res = HashSet::from_iter(vec!["".to_string()]);
while it.len() != 0 {
if Self::eat(it, '{') {
res = Self::concat(&res, &Self::parse(it));
Self::eat(it, '}');
continue;
}
if Self::eat(it, ',') {
res = &res | &Self::parse(it);
continue;
}
if let Some(Tok::Op('}')) = it.peek() {
break;
}
if let Some(Tok::S(s)) = it.next() {
res = Self::concat(&res, &HashSet::from_iter(vec![s]));
continue;
}
}
res
}
fn eat(it: &mut Peekable<IntoIter<Tok>>, c: char) -> bool {
if let Some(&Tok::Op(t)) = it.peek() {
if t == c {
it.next();
true
} else {
false
}
} else {
false
}
}
fn parse_tokens(it: &mut Peekable<Chars>) -> Vec<Tok> {
let mut res = vec![];
while let Some(c) = it.next() {
match c {
'{' | '}' | ',' => res.push(Tok::Op(c)),
'a'..='z' => {
let mut s = "".to_string();
s.push(c);
while let Some('a'..='z') = it.peek() {
s.push(it.next().unwrap());
}
res.push(Tok::S(s));
}
_ => {}
}
}
res
}
}
#[test]
fn test() {
let expression = "{a,b}{c,{d,e}}".to_string();
let res = vec_string!["ac", "ad", "ae", "bc", "bd", "be"];
assert_eq!(Solution::brace_expansion_ii(expression), res);
let expression = "{{a,z},a{b,c},{ab,z}}".to_string();
let res = vec_string!["a", "ab", "ac", "z"];
assert_eq!(Solution::brace_expansion_ii(expression), res);
}
// Accepted solution for LeetCode #1096: Brace Expansion II
function braceExpansionII(expression: string): string[] {
const dfs = (exp: string) => {
const j = exp.indexOf('}');
if (j === -1) {
s.add(exp);
return;
}
const i = exp.lastIndexOf('{', j);
const a = exp.substring(0, i);
const c = exp.substring(j + 1);
for (const b of exp.substring(i + 1, j).split(',')) {
dfs(a + b + c);
}
};
const s: Set<string> = new Set();
dfs(expression);
return Array.from(s).sort();
}
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.