package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * https://leetcode.com/problems/brace-expansion-ii/
 */
public class Problem1096 {
    public List<String> braceExpansionII(String expression) {
        List<List<List<String>>> groups = new ArrayList<>();
        groups.add(new ArrayList<>());
        int level = 0;
        int start = -1;
        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);
            if (c == '{') {
                if (level == 0) {
                    start = i + 1;
                }
                level++;
            } else if (c == '}') {
                level--;
                if (level == 0) {
                    groups.get(groups.size() - 1).add(
                        braceExpansionII(expression.substring(start, i)));
                }
            } else if (c == ',' && level == 0) {
                groups.add(new ArrayList<>());
            } else if (level == 0) {
                groups.get(groups.size() - 1).add(Arrays.asList(String.valueOf(c)));
            }
        }

        Set<String> set = new TreeSet<>();
        for (List<List<String>> group : groups) {
            List<String> pre = new ArrayList<>();
            pre.add("");
            for (List<String> g : group) {
                List<String> tmp = new ArrayList<>();
                for (String pStr : pre) {
                    for (String gStr : g) {
                        tmp.add(pStr + gStr);
                    }
                }
                pre = tmp;
            }
            set.addAll(pre);
        }
        return new ArrayList<>(set);
    }

    public static void main(String[] args) {
        Problem1096 prob = new Problem1096();
        System.out.println(prob.braceExpansionII("{a{b,c}d{e,f}}")); // ["abde",abdf","acde","acdf"]
//        System.out.println(prob.braceExpansionII("a{b,c}")); // ["ab","ac"]
//        System.out.println(prob.braceExpansionII("{c{d,e}f}")); // ["cdf","cef"]
//        System.out.println(prob.braceExpansionII("{a,b}{c{d,e}}")); // ["acd","ace","bcd","bce"]
//        System.out.println(prob.braceExpansionII("{{a,z},a{b,c},{ab,z}}")); // ["a","ab","ac","z"]
//        System.out.println(prob.braceExpansionII("{a,b,c}")); // ["a","b","c"]
//        System.out.println(prob.braceExpansionII("{{a,b},{b,c}}")); // ["a","b","c"]
//        System.out.println(prob.braceExpansionII("{a,b}{c,d}")); // ["ac","ad","bc","bd"]
//        System.out.println(prob.braceExpansionII("{a{b,c}}{{d,e}f{g,h}}")); // ["abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"]
    }
}

@@ -1721,981 +1721,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1096 prob = new Problem1096();%0A        System.out.println(prob.braceExpansionII(%22%7Ba%7Bb,c%7Dd%7Be,f%7D%7D%22)); // %5B%22abde%22,abdf%22,%22acde%22,%22acdf%22%5D%0A//        System.out.println(prob.braceExpansionII(%22a%7Bb,c%7D%22)); // %5B%22ab%22,%22ac%22%5D%0A//        System.out.println(prob.braceExpansionII(%22%7Bc%7Bd,e%7Df%7D%22)); // %5B%22cdf%22,%22cef%22%5D%0A//        System.out.println(prob.braceExpansionII(%22%7Ba,b%7D%7Bc%7Bd,e%7D%7D%22)); // %5B%22acd%22,%22ace%22,%22bcd%22,%22bce%22%5D%0A//        System.out.println(prob.braceExpansionII(%22%7B%7Ba,z%7D,a%7Bb,c%7D,%7Bab,z%7D%7D%22)); // %5B%22a%22,%22ab%22,%22ac%22,%22z%22%5D%0A//        System.out.println(prob.braceExpansionII(%22%7Ba,b,c%7D%22)); // %5B%22a%22,%22b%22,%22c%22%5D%0A//        System.out.println(prob.braceExpansionII(%22%7B%7Ba,b%7D,%7Bb,c%7D%7D%22)); // %5B%22a%22,%22b%22,%22c%22%5D%0A//        System.out.println(prob.braceExpansionII(%22%7Ba,b%7D%7Bc,d%7D%22)); // %5B%22ac%22,%22ad%22,%22bc%22,%22bd%22%5D%0A//        System.out.println(prob.braceExpansionII(%22%7Ba%7Bb,c%7D%7D%7B%7Bd,e%7Df%7Bg,h%7D%7D%22)); // %5B%22abdfg%22, %22abdfh%22, %22abefg%22, %22abefh%22, %22acdfg%22, %22acdfh%22, %22acefg%22, %22acefh%22%5D%0A    %7D
 %0A%7D%0A

Solve problem 1096