JavaLearner1 JavaLearner1 - 6 months ago 9
Java Question

Permutations of a String and position of Specific String

I need to write a code to find all the permutations of a string and also to find the position of Specific string given. I already found this code from stackoverflow to find the permutations of the String

public class StringCombi2 {

public static void permutation(String str) {
permutation("", str);
}

private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);

else {
for (int i = 0; i < n; i++)

permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}


}

public static void main(String arsd[])
{
permutation("student");
}
}


This code prints all the possible permutations of String "Student".

Now i need to find the position of the String "Studten" and print its position (Ex. ith position)

I am not sure how to find it..can anyone help me out...

Example:
THe output will get generated like this

student
studetn
studnet
studnte
studten
studtne
stuednt
stuedtn
stuendt
stuentd
.
.
.

The string Studten is generated on iteration 4. So the position of String "Studten" is 4.

I have to find this using java code...

Answer

If you want the iteration displayed with the permutation output, the simplest way would be to create a global counter, print the counter with the permutation, and increment the counter immediately after:

public class StringCombi2 {

    private static int counter = 0; //create counter

    public static void permutation(String str) {
        permutation("", str);
    }

    private static void permutation(String prefix, String str) {
        int n = str.length();
        if (n == 0) {
            System.out.println(counter + ": " + prefix); //print counter with permutation
            counter++; // increment counter
        } else {
            for (int i = 0; i < n; i++)
                permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
        }
    }

    public static void main(String arsd[]) {
        permutation("student");
    }
}

If you only want to show the iterations that you care about, you can place another if statement that checks if the current permutation equals the one you're looking for. When you do that, you find that "studten" appears twice:

public class StringCombi2 {

    private static int counter = 0;

    public static void permutation(String str) {
        permutation("", str);
    }

    private static void permutation(String prefix, String str) {
        int n = str.length();
        if (n == 0) {
            if (prefix.equals("studten")) {
                System.out.println(prefix + " is at iteration: " + counter);
            } else {
                counter++;
            }

        } else {
            for (int i = 0; i < n; i++)
                permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
        }
    }

    public static void main(String arsd[]) {
        permutation("student");
    }
}

and your output will be:

studten is at iteration: 4
studten is at iteration: 629