package com.fishercoder.solutions;

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

/**
 * 5087. Letter Tile Possibilities
 *
 * You have a set of tiles, where each tile has one letter tiles[i] printed on it.
 * Return the number of possible non-empty sequences of letters you can make.
 *
 * Example 1:
 * Input: "AAB"
 * Output: 8
 * Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
 *
 * Example 2:
 * Input: "AAABBC"
 * Output: 188
 *
 * Note:
 * 1. 1 <= tiles.length <= 7
 * 2. tiles consists of uppercase English letters.
 * */
public class _1079 {
    public static class Solution1 {
        public int numTilePossibilities(String tiles) {
            char[] chars = tiles.toCharArray();
            Arrays.sort(chars);
            boolean[] used = new boolean[chars.length];
            StringBuilder sb = new StringBuilder();
            List<String> result = new ArrayList<>();
            dfs(chars, used, sb, result);
            return result.size();
        }

        private void dfs(char[] chars, boolean[] used, StringBuilder sb, List<String> result) {
            if (sb.length() != 0) {
                result.add(sb.toString());
            }
            IntStream.range(0, chars.length)
                    .filter(i -> !used[i])
                    .filter(i -> i <= 0 || chars[i - 1] != chars[i] || used[i - 1])
                    .forEach(i -> {
                        used[i] = true;
                        sb.append(chars[i]);
                        dfs(chars, used, sb, result);
                        used[i] = false;
                        sb.deleteCharAt(sb.length() - 1);
                    });
        }
    }
}

@@ -145,498 +145,8 @@
 m;%0A%0A
-/**%0A * 5087. Letter Tile Possibilities%0A *%0A * You have a set of tiles, where each tile has one letter tiles%5Bi%5D printed on it.%0A * Return the number of possible non-empty sequences of letters you can make.%0A *%0A * Example 1:%0A * Input: %22AAB%22%0A * Output: 8%0A * Explanation: The possible sequences are %22A%22, %22B%22, %22AA%22, %22AB%22, %22BA%22, %22AAB%22, %22ABA%22, %22BAA%22.%0A *%0A * Example 2:%0A * Input: %22AAABBC%22%0A * Output: 188%0A *%0A * Note:%0A * 1. 1 %3C= tiles.length %3C= 7%0A * 2. tiles consists of uppercase English letters.%0A * */%0A
 publ

refactor 1079