package com.fishercoder.solutions;

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

/**
 * 1087. Brace Expansion
 *
 * A string S represents a list of words.
 * Each letter in the word has 1 or more options.  If there is one option, the letter is represented as is.
 * If there is more than one option, then curly braces delimit the options.
 * For example, "{a,b,c}" represents options ["a", "b", "c"].
 * For example, "{a,b,c}d{e,f}" represents the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].
 *
 * Return all words that can be formed in this manner, in lexicographical order.
 *
 * Example 1:
 * Input: "{a,b}c{d,e}f"
 * Output: ["acdf","acef","bcdf","bcef"]
 *
 * Example 2:
 * Input: "abcd"
 * Output: ["abcd"]
 *
 * Note:
 * 1 <= S.length <= 50
 * There are no nested curly brackets.
 * All characters inside a pair of consecutive opening and ending curly brackets are different.
 * */
public class _1087 {
    public static class Solution1 {
        public String[] expand(String S) {
            List<char[]> letters = parse(S);
            List<String> result = backtracking(letters, 0, new StringBuilder(), new ArrayList<>());
            String[] r = result.stream().toArray(String[]::new);
            Arrays.sort(r);
            return r;
        }

        private List<String> backtracking(List<char[]> letters, int start, StringBuilder sb, List<String> result) {
            if (start >= letters.size()) {
                result.add(sb.toString());
                return result;
            }
            char[] chars = letters.get(start);
            for (int i = 0; i < chars.length; i++) {
                sb.append(chars[i]);
                backtracking(letters, start + 1, sb, result);
                sb.setLength(sb.length() - 1);
            }
            return result;
        }

        private List<char[]> parse(String s) {
            List<char[]> result = new ArrayList<>();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '{') {
                    int start = ++i;
                    while (i < s.length() && s.charAt(i) != '}') {
                        i++;
                    }
                    String[] strings = s.substring(start, i).split(",");
                    char[] chars = new char[strings.length];
                    for (int j = 0; j < strings.length; j++) {
                        chars[j] = strings[j].charAt(0);
                    }
                    result.add(chars);
                } else {
                    char[] chars = new char[1];
                    chars[0] = s.charAt(i);
                    result.add(chars);
                }
            }
            return result;
        }
    }
}

@@ -110,821 +110,8 @@
 t;%0A%0A
-/**%0A * 1087. Brace Expansion%0A *%0A * A string S represents a list of words.%0A * Each letter in the word has 1 or more options.  If there is one option, the letter is represented as is.%0A * If there is more than one option, then curly braces delimit the options.%0A * For example, %22%7Ba,b,c%7D%22 represents options %5B%22a%22, %22b%22, %22c%22%5D.%0A * For example, %22%7Ba,b,c%7Dd%7Be,f%7D%22 represents the list %5B%22ade%22, %22adf%22, %22bde%22, %22bdf%22, %22cde%22, %22cdf%22%5D.%0A *%0A * Return all words that can be formed in this manner, in lexicographical order.%0A *%0A * Example 1:%0A * Input: %22%7Ba,b%7Dc%7Bd,e%7Df%22%0A * Output: %5B%22acdf%22,%22acef%22,%22bcdf%22,%22bcef%22%5D%0A *%0A * Example 2:%0A * Input: %22abcd%22%0A * Output: %5B%22abcd%22%5D%0A *%0A * Note:%0A * 1 %3C= S.length %3C= 50%0A * There are no nested curly brackets.%0A * All characters inside a pair of consecutive opening and ending curly brackets are different.%0A * */%0A
 publ

refactor 1087