How to convert recursion to iteration?
<p>This question is asked few times but I still find it quite difficult to convert easily readable and intuitive code into iterative code. For example I was practicing a coding question and I am given 26 integers which indicate how many times each character appears in the string. I should print all possible strings. Following is my recursive code</p>

<pre><code>private static void combinatorial(String prefix, ArrayList&lt;Integer&gt; remainingToFill,
        int totalLength) {
    if (prefix.length() == totalLength) {
        System.out.println(prefix);
    }
    for (int i = 0; i &lt; remainingToFill.size(); i++) {
        if (remainingToFill.get(i) &gt; 0) {
            ArrayList&lt;Integer&gt; toFill = new ArrayList&lt;&gt;(remainingToFill);
            toFill.set(i, toFill.get(i) - 1);
            combinatorial(prefix + (char) ('a' + i), toFill, totalLength);
        }
    }
}
</code></pre>

<p>I coded iterative version of this but the resultant function is much more complex and not readable and took more time for me to code it. How do I tackle this kind of problem? Is there any simple technique I can follow which would lead to easy and readable code? </p>