Quantcast
Channel: Scratch Where It's Itching
Viewing all articles
Browse latest Browse all 73

Permutations

$
0
0
A simple piece of code for calculating all permutations of a given word:
    public static List permutations(String s) {        List list = new ArrayList<>();        permutations(list, s, "");        return list;    }    private static void permutations(List list, String from, String to) {        if (from.isEmpty()) {            list.add(to);        }        else {            for (int i = 0; i < from.length(); i++) {                permutations(list,                        new StringBuilder(from).deleteCharAt(i).toString(),                        to + from.charAt(i));            }        }    }


Viewing all articles
Browse latest Browse all 73

Trending Articles