简体   繁体   中英

Permutations of a String and its Sub-Strings using ArrayList in Java

I've been asked to write a program to find the permutations of a String and its Sub-Strings using an ArrayList. I came up with a solution, but it is not displaying the required output. So, I would appreciate if someone could enlighten me a bit on this.

The question is as follows:

To compute all permutations of a string and it's sub-strings. For example, given a string S such as "abc", it should output a list/array of strings retlist [a, b, c, ab, ba, ac, ca, bc, cb, abc, acb, bac, bca, cab, cba]. Your code should take S as input, and produce retlist for the permuted list. On top of having a code that works, please optimize the code for efficiency of speed and memory (we will be testing for large strings and the faster it goes, the better).

As said in the question, when permutating a string of "abc", it should print a result as below:

[a, b, c, ab, ba, ac, ca, bc, cb, abc, acb, bac, bca, cab, cba]

What I came up with so far:

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

public class Permutation {

    public static void main(String[] args) {
        enumerateSubString("abc");
    }

    public static List<String> enumerateSubString(String S_input) {
        ArrayList<String> retlist = new ArrayList<String>();

        int n = S_input.length();

        if (n == 1) {
            retlist.add(S_input);
        } else {
            for (int i = 0; i < n; i++) {
                retlist.addAll(enumerateSubString(S_input.substring(0, i) + S_input.substring(i + 1, n)));
            }
        }

        System.out.print(retlist);
        return retlist;
    }
}

And the result that I am getting right now with the above code:

[c][b][c, b][c][a][c, a][b][a][b, a][c, b, c, a, b, a]

Thanks

Adding another answer with generic algorithm. This will give all permutations in the order mentioned. Hope this solve purpose

public class Test {

public static void main(String[] args) {

    String s = "abc";

    Map<Integer, List<String>> map = new HashMap<>();

    List<String> strArray = new ArrayList<String>(Arrays.asList(s.split("")));
    map.put(1, strArray);

    if (strArray.size() > 1) {
        new Test().enumerateSubString(strArray.size(), map);
    }
    System.out.println(map.values());
}

public void enumerateSubString(int n, Map<Integer, List<String>> map) {

    if (!map.containsKey(n)) {
        map.put(n, new ArrayList<>());
    }

    if (n == 2) {
        // List of string with each having 1 character
        List<String> list_1 = map.get(1);
        // List of string which each having 2 characters
        List<String> list_2 = new ArrayList<>();
        for (int i = 0; i < list_1.size(); i++) {
            for (int j = i + 1; j < list_1.size(); j++) {
                list_2.add(list_1.get(i) + list_1.get(j));
                list_2.add(swap(list_1.get(i) + list_1.get(j)));
            }
        }
        // Map list_2 to key 2
        map.put(n, list_2);
    } else {
        // Recursive function
        enumerateSubString(n - 1, map);
        // List of string with each having n-1 characters
        List<String> list = map.get(n - 1);
        // List of string with each having n characters
        List<String> list_n = map.get(n);
        // Add each character to list of n-1 to get n charater string list
        for (String l1 : map.get(1)) {
            for (String l : list) {
                // this condition is to avoid repetation of charaters n
                // String
                if (l.indexOf(l1) < 0) {
                    list_n.add(l1 + l);
                }
            }
        }
    }
}

// Function to swap characters
private String swap(String str) {
    String s1 = str.substring(1) + str.substring(0, 1);
    return s1;
}}

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