Problem 24. Lexicographic permutations

Description

import java.util.ArrayList;

public class euler24 {
    public static ArrayList<String> list = new ArrayList<String>();
    public static void main(String[]args) {
        String s = "0123456789";
        perm("", s);
        System.out.println(list.get(999999));
    }
    private static void perm(String prefix, String s) {
        int N = s.length();
        if (N == 0) {
            list.add(prefix);
        }
        else {
            for (int i = 0; i < N; i++)
                perm(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>