change this code to linked list public class Permutations

change this code to linked list:

public class Permutations {

    // print n! permutation of the characters of the string s (in order)
    public static void perm1(String s) { perm1(\"\", s); }
    private static void perm1(String prefix, String s) {
        int n = s.length();
        if (n == 0) StdOut.println(prefix);
        else {
            for (int i = 0; i < n; i++)
               perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, n));
        }

    }

    // print n! permutation of the elements of array a (not in order)
    public static void perm2(String s) {
        int n = s.length();
        char[] a = new char[n];
        for (int i = 0; i < n; i++)
            a[i] = s.charAt(i);
        perm2(a, n);
    }

    private static void perm2(char[] a, int n) {
        if (n == 1) {
            StdOut.println(a);
            return;
        }
        for (int i = 0; i < n; i++) {
            swap(a, i, n-1);
            perm2(a, n-1);
            swap(a, i, n-1);
        }
    }

    // swap the characters at indices i and j
    private static void swap(char[] a, int i, int j) {
        char c = a[i];
        a[i] = a[j];
        a[j] = c;
    }

    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        String alphabet = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\";
        String elements = alphabet.substring(0, n);
        perm1(elements);
        StdOut.println();
        perm2(elements);
    }
}

public void clear()
// Makes this StringLog empty.
{                 
    for (int i = 0; i <= lastIndex; i++)
      log[i] = null;
    lastIndex = -1;
}

public String getName()
// Returns the name of this StringLog.
{
    return name;
}

public String toString()
// Returns a nicely formatted string representing this StringLog.
{
    String logString = \"\";
   //String logString;
    {
    for (int j = 0; j <= lastIndex - 1; j++)
    logString = logString + log[j] + System.getProperty(\"line.separator\");
}
    for (int i = lastIndex; i == lastIndex; i++)
    logString = logString + log[i] ;
   
     
    return logString;
}
}

Solution

Hi,

The solution could be like this:

public class PermTest {

public static void main(String[] args) throws Exception {
String str = \"abcdef\";
StringBuffer strBuf = new StringBuffer(str);
doPerm(strBuf,str.length());

}

private static void doPerm(StringBuffer str, int index){

if(index <= 0)
System.out.println(str);
else { //recursively solve this by placing all other chars at current first pos
doPerm(str, index-1);
int currPos = str.length()-index;
for (int i = currPos+1; i < str.length(); i++) {//start swapping all other chars with current first char
swap(str,currPos, i);
doPerm(str, index-1);
swap(str,i, currPos);//restore back my string buffer
}
}
}

private static void swap(StringBuffer str, int pos1, int pos2){
char t1 = str.charAt(pos1);
str.setCharAt(pos1, str.charAt(pos2));
str.setCharAt(pos2, t1);
}
}   

change this code to linked list: public class Permutations { // print n! permutation of the characters of the string s (in order) public static void perm1(Strin
change this code to linked list: public class Permutations { // print n! permutation of the characters of the string s (in order) public static void perm1(Strin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site