package com.fishercoder.solutions;

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

/**
 * 1286. Iterator for Combination
 *
 * Design an Iterator class, which has:
 * A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
 * A function next() that returns the next combination of length combinationLength in lexicographical order.
 * A function hasNext() that returns True if and only if there exists a next combination.
 *
 * Example:
 * CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator.
 * iterator.next(); // returns "ab"
 * iterator.hasNext(); // returns true
 * iterator.next(); // returns "ac"
 * iterator.hasNext(); // returns true
 * iterator.next(); // returns "bc"
 * iterator.hasNext(); // returns false
 *
 * Constraints:
 * 1 <= combinationLength <= characters.length <= 15
 * There will be at most 10^4 function calls per test.
 * It's guaranteed that all calls of the function next are valid.
 * */
public class _1286 {
    public static class Solution1 {
        public static class CombinationIterator {

            List<String> list;
            int index;
            int combinationLength;
            boolean[] visited;

            public CombinationIterator(String characters, int combinationLength) {
                this.index = 0;
                this.list = new ArrayList<>();
                this.combinationLength = combinationLength;
                this.visited = new boolean[characters.length()];
                buildAllCombinations(characters, 0, new StringBuilder(), visited);
            }

            private void buildAllCombinations(String characters, int start, StringBuilder sb, boolean[] visited) {
                if (sb.length() == combinationLength) {
                    list.add(sb.toString());
                    return;
                } else {
                    for (int i = start; i < characters.length(); ) {
                        if (!visited[i]) {
                            sb.append(characters.charAt(i));
                            visited[i] = true;
                            buildAllCombinations(characters, i++, sb, visited);
                            visited[i - 1] = false;
                            sb.setLength(sb.length() - 1);
                        } else {
                            i++;
                        }
                    }
                }
            }

            public String next() {
                return list.get(index++);
            }

            public boolean hasNext() {
                return index < list.size();
            }
        }
    }
}

@@ -85,960 +85,8 @@
 t;%0A%0A
-/**%0A * 1286. Iterator for Combination%0A *%0A * Design an Iterator class, which has:%0A * A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.%0A * A function next() that returns the next combination of length combinationLength in lexicographical order.%0A * A function hasNext() that returns True if and only if there exists a next combination.%0A *%0A * Example:%0A * CombinationIterator iterator = new CombinationIterator(%22abc%22, 2); // creates the iterator.%0A * iterator.next(); // returns %22ab%22%0A * iterator.hasNext(); // returns true%0A * iterator.next(); // returns %22ac%22%0A * iterator.hasNext(); // returns true%0A * iterator.next(); // returns %22bc%22%0A * iterator.hasNext(); // returns false%0A *%0A * Constraints:%0A * 1 %3C= combinationLength %3C= characters.length %3C= 15%0A * There will be at most 10%5E4 function calls per test.%0A * It's guaranteed that all calls of the function next are valid.%0A * */%0A
 publ

refactor 1286