Search code examples
javabluej

Generate all permutations of digits in an number of unknown length without using recursion


Many of my friends and teachers argued me that the program of finding all possible permutations of digits, in a number of 'n' digits without using recursion is not possible while one said it is possible but tricky. So I need some help solving this problem. Thank You.


Solution

  • Recursion is the act of a function calling itself. Under the hood function calls are pushed to the stack of the system and when a function finishes execution, it will be popped from the stack. A stack is a data structure which stores items and has the following operations:

    • push: puts an item to the end of a stack
    • pop: removes an item from the end of a stack and returns it
    • top: returns the item at the end of a stack

    You need to define what your stack will store. In our case that could be the current, possibly unfinished permutation. If you want an iterative solution, your job is to handle the stack yourself.