Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String diagonal = "asdf";
        List<String> permutations = allPermutation("", diagonal);
        System.out.println("size is : " + permutations.size());
        System.out.println(permutations);
    }

    public static List<String> allPermutation(String permutation, String letters) {
        List<String> list = new ArrayList<>();
        if (letters.isEmpty()) {
            list.add(permutation);
            return list;
        }

        for (int i = 0; i < letters.length(); i++) {
            list.addAll(allPermutation(permutation + letters.charAt(i), removeCharAt(i, letters)));
        }

        return list;
    }

    public static String removeCharAt(int pos, String text) {
        return text.substring(0, pos) + text.substring(pos + 1, text.length());
    }
}