## C# || CombinationIterator – How To Implement Iterator For Combination Using C#

The following is a module with functions which demonstrates how to implement iterator for combination using C#.

1. Combination Iterator – Problem Statement

Design the CombinationIterator class:

• CombinationIterator(string characters, int combinationLength) Initializes the object with a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
• next() Returns the next combination of length combinationLength in lexicographical order.
• hasNext() Returns true if and only if there exists a next combination.

Example 1:

``` Input Input: ["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [["abc", 2], [], [], [], [], [], []] Output: [null, "ab", true, "ac", true, "bc", false]```

``` ```

```Explanation: CombinationIterator itr = new CombinationIterator("abc", 2); itr.next(); // return "ab" itr.hasNext(); // return True itr.next(); // return "ac" itr.hasNext(); // return True itr.next(); // return "bc" itr.hasNext(); // return False ```

2. Combination Iterator – Solution

The following is a solution which demonstrates how to implement iterator for combination.

The following solution uses backtracking to generate combinations.

``` 2. Combination Iterator - Solution C# // ============================================================================ // Author: Kenneth Perkins // Date: Nov 20, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to implement iterator for combination // ============================================================================ public class CombinationIterator { Queue<string> combinations; public CombinationIterator(string characters, int combinationLength) { combinations = new Queue<string>(); Generate(characters, combinationLength, 0, new StringBuilder()); } private void Generate(string characters, int combinationLength, int startIndex, StringBuilder currentCombo) { if (currentCombo.Length == combinationLength) { combinations.Enqueue(currentCombo.ToString()); return; } for (int index = startIndex; index < characters.Length; ++index) { // Add this item to the combination currentCombo.Append(characters[index]); // Keep generating permutations Generate(characters, combinationLength, index + 1, currentCombo); // Remove last item as its already been explored currentCombo.Remove(currentCombo.Length - 1, 1); } } public string Next() { return combinations.Dequeue(); } public bool HasNext() { return combinations.Count > 0; } }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829303132333435363738394041 // ============================================================================//    Author: Kenneth Perkins//    Date:   Nov 20, 2021//    Taken From: http://programmingnotes.org///    File:  Solution.cs//    Description: Demonstrates how to implement iterator for combination// ============================================================================public class CombinationIterator {    Queue<string> combinations;     public CombinationIterator(string characters, int combinationLength) {        combinations = new Queue<string>();        Generate(characters, combinationLength, 0, new StringBuilder());    }     private void Generate(string characters, int combinationLength, int startIndex, StringBuilder currentCombo) {        if (currentCombo.Length == combinationLength) {            combinations.Enqueue(currentCombo.ToString());            return;        }         for (int index = startIndex; index < characters.Length; ++index) {            // Add this item to the combination            currentCombo.Append(characters[index]);             // Keep generating permutations            Generate(characters, combinationLength, index + 1, currentCombo);             // Remove last item as its already been explored            currentCombo.Remove(currentCombo.Length - 1, 1);        }    }     public string Next() {        return combinations.Dequeue();    }     public bool HasNext() {        return combinations.Count > 0;    }}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

``` [null,"ab",true,"ac",true,"bc",false] ```