简体   繁体   中英

String permutations and recursion

Been studying a programming course over the summer.

It's a fairly standard assignment and I've looked at alot of posts and videos but i just can't get it to work!

The assignment goes: "Finish the recursive method that will print all the permutatons of the strings."

public static void permute(String s, PrintStream out) {
    permute(s, 0, out);
}

private static void permute(String s, int pos, PrintStream out) {
    // Fill in your own code
}

I've seen a couple of solutions, but with the pre given format my hands are kinda tied. I played around abit just to get the platform we use to compile tthe code. What I came up with ain't pretty and I'm really stuck!

public static void permute(String s, PrintStream out) {
    permute(s, 0, out);
}

private static void permute(String s, int pos, PrintStream out) {
    out.println(s);
    for (int i = pos; i < s.length() - 1; i++) {
        StringBuilder sb = new StringBuilder();
        sb.append(s);
        if (i == pos + 1) {
            sb.setCharAt(0, s.charAt(s.length() - 1));
            sb.setCharAt(2, s.charAt(s.length() - s.length()));
            permute(sb.toString(), i, out);
        } else {
            sb.setCharAt(1, s.charAt(s.length() - 1));
            sb.setCharAt(2, s.charAt(s.length() - 2));
            out.println(sb.toString());
        }
    }
}

It prints ABC ACB CAB CBA . I do realize there is alot of issues with this code. I wrote it specifically to handle ABC , then I was going to figure it out from there. I came up woefully short.

You could use the following way to recursively permutate a String .

static int count = 0;

public static void main(String args[]) {
    permutation("", "JASPER");
    // Gives the total permutated word count
    System.out.println(count);
}

private static void permutation(String perm, String word) {
    if (word.isEmpty()) {
        System.err.println(perm + word);
    } else {
        for (int i = 0; i < word.length(); i++) {
            permutation(perm + word.charAt(i), word.substring(0, i)
                    + word.substring(i + 1, word.length()));
            count++;
        }
    }
}

This works for any given string.

This is what I did to solve it:

public static void permute(String s, PrintStream out) {
    permute("", s, out);
}

private static void permute(String pre, String rem, PrintStream out) {

    if (rem.length() == 0) {
        out.println(pre);
    }

    for (int i = 0; i < rem.length(); i++) {
        permute(pre+rem.charAt(i), rem.substring(0, i)+rem.substring(i+1, rem.length()), out);
    }
}

Feels a little bit cheesy with the parameter I added. But I could'nt see another way on my own. It's recursion atleast, if anyone has a solution to the original form. I'll gladly take it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM