package leetcode;

/**
 * https://leetcode.com/problems/iterator-for-combination/
 */
public class Problem1286 {
    private static class CombinationIterator {
        public CombinationIterator(String characters, int combinationLength) {
            // TODO
        }

        public String next() {
            // TODO
            return null;
        }

        public boolean hasNext() {
            // TODO
            return false;
        }
    }

    private static void nextCombination(String str, int length, int index, String accu) {
        if (accu.length() == length) {
            System.out.println(accu);
            return;
        }
        for (int i = index; i < str.length(); i++) {
            char c = str.charAt(i);
            nextCombination(str, length, i + 1, accu + c);
        }
    }

    public static void main(String[] args) {
//        CombinationIterator iter = new CombinationIterator("abc", 2);
//        System.out.println(iter.next()); // returns "ab"
//        System.out.println(iter.hasNext()); // returns true
//        System.out.println(iter.next()); // returns "ac"
//        System.out.println(iter.hasNext()); // returns true
//        System.out.println(iter.next()); // returns "bc"
//        System.out.println(iter.hasNext()); // returns false

        nextCombination("12345", 3, 0, "");
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -205,16 +205,113 @@
 rator %7B%0A
+        private final List%3CString%3E combinations = new ArrayList%3C%3E();%0A        private int index;%0A%0A
         
@@ -385,39 +385,105 @@
 ) %7B%0A            
-// TODO
+generateCombinations(characters, combinationLength, 0, %22%22, combinations);
 %0A        %7D%0A%0A    
@@ -525,39 +525,40 @@
     
-// TODO%0A            return null
+return combinations.get(index++)
 ;%0A  
@@ -617,40 +617,42 @@
     
-// TODO%0A            return false
+return index %3C combinations.size()
 ;%0A  
@@ -655,31 +655,29 @@
 ;%0A        %7D%0A
+%0A
     
-%7D%0A%0A
     private 
@@ -688,20 +688,24 @@
 ic void 
-next
+generate
 Combinat
@@ -707,16 +707,17 @@
 bination
+s
 (String 
@@ -755,20 +755,100 @@
 ing accu
-) %7B%0A
+,%0A                                                 List%3CString%3E combinations) %7B%0A    
         
@@ -894,34 +894,36 @@
     
-System.out.println
+    combinations.add
 (accu);%0A
     
@@ -918,16 +918,20 @@
 (accu);%0A
+    
         
@@ -946,26 +946,34 @@
 rn;%0A        
+ 
-%7D%0A
+   %7D%0A    
         for 
@@ -1025,16 +1025,20 @@
         
+    
 char c =
@@ -1057,36 +1057,44 @@
 i);%0A            
+   
-next
+ generate
 Combination(str,
@@ -1088,16 +1088,17 @@
 bination
+s
 (str, le
@@ -1122,553 +1122,48 @@
  + c
-);%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A//        CombinationIterator iter = new CombinationIterator(%22abc%22, 2);%0A//        System.out.println(iter.next()); // returns %22ab%22%0A//        System.out.println(iter.hasNext()); // returns true%0A//        System.out.println(iter.next()); // returns %22ac%22%0A//        System.out.println(iter.hasNext()); // returns true%0A//        System.out.println(iter.next()); // returns %22bc%22%0A//        System.out.println(iter.hasNext()); // returns false%0A%0A        nextCombination(%2212345%22, 3, 0, %22%22);
+, combinations);%0A            %7D%0A        %7D
 %0A   

Solve problem 1286