1
$\begingroup$

How to find all permutation of a String in Iterative way ?

Code (Recursion) :

private void permute(String str, int l, int r)
{
    if (l == r)
        System.out.println(str);
    else
    {
        for (int i = l; i <= r; i++)
        {
            str = swap(str,l,i);
            permute(str, l+1, r);
            str = swap(str,l,i);
        }
    }
}


public String swap(String a, int i, int j)
{
    char temp;
    char[] charArray = a.toCharArray();
    temp = charArray[i] ;
    charArray[i] = charArray[j];
    charArray[j] = temp;
    return String.valueOf(charArray);
} 
$\endgroup$
2
  • 1
    $\begingroup$ Welcome to Computer Science! What have you tried? Where did you get stuck? We do not want to just hand you the solution; we want you to gain understanding. However, as it is we do not know what your underlying problem is, so we can not begin to help. See here for tips on asking questions about exercise problems. If you are uncertain how to improve your question, why not ask around in Computer Science Chat? $\endgroup$ – Raphael Jun 16 '17 at 9:17
  • 1
    $\begingroup$ Knuth shows how to do it in his Art of Computer Programming. $\endgroup$ – Yuval Filmus Jun 16 '17 at 11:24
1
$\begingroup$

Wikipedia describes several algorithms, including:

$\endgroup$
1
0
$\begingroup$

You can use the algorithm in the link below:
http://www.geeksforgeeks.org/permutations-string-using-iteration/

$\endgroup$
2
  • $\begingroup$ Perhaps you can, at the very least, type the algorithm here? $\endgroup$ – Yuval Filmus Jun 16 '17 at 11:24
  • $\begingroup$ that page says: "How to print all permutations iteratively? A simple solution to use permutations of n-1 elements to generate permutations of n elements." Isn't that recursion? $\endgroup$ – nonopolarity Aug 17 '19 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.