Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String permutations in Java (non-recursive)

I'm a high school student of grade 10 trying to work through some problems in a Data Structures and Algorithms book on Java.

One of the questions is to print all permutations of a string.

class C14
{
public static void main(char a[])
{
    // char[] a = {'c','a','r','b','o','n'};
    int c=0,w=0;
    for(int q = 0;q<a.length;q++) 
    {
        for(int i=0;i<a.length;i++)
        {
            for(int j=1;j<a.length;j++)
            {
                for(int k=1;k<a.length-1;k++) 
                {
                    for(int z=0;z<a.length;z++)
                    {
                        System.out.print(a[z]);
                        c++;
                    }
                    w++;
                    System.out.println();
                    char p=a[k+1];
                    a[k+1]=a[k];
                    a[k]=p;
                }
                System.out.println();
            }
            System.out.println();
            char x=a[0];
            a[0]=a[1];
            a[1]=x;
        }
      }
    System.out.println(" Character count = " + c);
    System.out.println(" Word count = " + w);
    }
}

This is my attempt. The book asks me to do it for the characters 'c','a','r','b','o','n'. My solution does just that, but when I try to use 3 or 4 letter words, it gives me repetitions. If I remove the outermost loop and try to print it, it works for 3 and 4 letter words but not for 5+ letter words.

I'll be glad to clarify my reasoning for it, I know it's not the most efficient, but do bear in mind the fact that I'm only in grade 10 and this is what came to my mind first.

Can someone please help me out, or at least hint at what's wrong? Please don't advise a recursive solution, because I want to work through it iteratively first. Thanks, Sumit.

like image 753
Sumit Shyamsukha Avatar asked Aug 30 '12 04:08

Sumit Shyamsukha


1 Answers

Permutation with repetitions

When you have n things to choose from ... you have n choices each time!

When choosing r of them, the permutations are:

n × n × ... (r times) = n^r

I'm presenting 2 cases. First case is when the we know the size of n and r already, and its the easy one. The second when n and r are dynamic.

//when n and r are known statically

class Permutation
{
    public static void main(String[] args)
    {
        char[] values = {'a', 'b', 'c', 'd'};
        int n = values.length;
        int r = 2; 

        int i = 0, j = 0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                System.out.println(values[j] + " " + values[i]);
            }
        }
    }
}


//when n and r are known only dynamically

class Permutation
{
    public static void main(String[] args)
    {
        char[] values = {'a', 'b', 'c', 'd'};
        int n = values.length;
        int r = 2; 
        int i[] = new int[r];
        int rc = 0;
        for(int j=0; j<Math.pow(n,r); j++)
        {

            rc=0;
            while(rc<r)
            {
                System.out.print(values[i[rc]] + " ");
                rc++;
            }
            System.out.println();
            rc = 0;
            while(rc<r)
            {
                if(i[rc]<n-1)
                {
                    i[rc]++;
                    break;
                }
                else
                {
                    i[rc]=0;
                }
                rc++;
            }
        }
    }
}
like image 70
John Avatar answered Oct 21 '22 02:10

John