Java implements the arrangement of strings

Enter a string and print out all permutations of the characters in the string lexicographically. For example, if the string abc is input, all strings abc, acb, bac, bca, cab and cba that can be arranged by the characters a, b, and c are printed out.

code

1. Non-lexicographical sorting

    /**
     * Recursively output all permutations of strings
     * Divide the string into the first character and the rest of the string
     * Swap the first character of the string with the remaining characters of the string
     * Repeat the above operation for the remaining characters
     * @param val
     * @param start
     */
    private static void permutationString(StringBuffer val, int start) {
        // When start reaches the end of the string, there are no interchangeable characters, output the arrangement result at this time
        if (start == val.length() - 1) {
            System.out.println(val);
        }
        // Swap the characters corresponding to start with the characters in the following string in turn
        // i starts from start, in order to output itself
        for (int i = start; i < val.length(); i++) {
            // If the position to be swapped is different, and the two characters are equal, no need to swap
            // Avoid outputting the character aaa multiple times
            if (i != start && val.charAt(start) == val.charAt(i)) {
                continue;
            }
            // swap the positions of two characters
            swap(val, start, i);
            // Exclude the characters corresponding to the current start, and arrange all the following strings
            permutationString(val, start + 1);
            // Swap back to the position of the two characters, so that the character at the start position is swapped with the characters after it
            swap(val, start, i);
        }
    }

    private static void swap(StringBuffer val, int i, int j) {
        char _i = val.charAt(i);
        char _j = val.charAt (j);
        val.setCharAt(i, _j);
        val.setCharAt(j, _i);
    }


    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("abc");
        Set<String> results = Sets.newHashSet();
        permutationString(sb, 0);
        for (String s : results) {
            System.out.println(s);
        }
    }

2, with the help of TreeSet dictionary sorting

    private static void permutationString(TreeSet<String> results, StringBuffer val, int start) {
        // When start reaches the end of the string, there are no interchangeable characters, output the arrangement result at this time
        if (start == val.length() - 1) {
            results.add(val.toString());
        }
        // Swap the characters corresponding to start with the characters in the following string in turn
        // i starts from start, in order to output itself
        for (int i = start; i < val.length(); i++) {
            // If the position to be swapped is different, and the two characters are equal, no need to swap
            // Avoid outputting the character aaa multiple times
            // The Set collection is used, and the repeated problem is handled by the Set
            //if (i != start && val.charAt(start) == val.charAt(i)) {
            //    continue;
            //}
            // swap the positions of two characters
            swap(val, start, i);
            // Exclude the characters corresponding to the current start, and arrange all the following strings
            permutationString(results, val, start + 1);
            // Swap back to the position of the two characters, so that the character at the start position is swapped with the characters after it
            swap(val, start, i);
        }
    }

    private static void swap(StringBuffer val, int i, int j) {
        char _i = val.charAt(i);
        char _j = val.charAt (j);
        val.setCharAt(i, _j);
        val.setCharAt(j, _i);
    }


    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("abc");
        TreeSet<String> results = Sets.newTreeSet();
        permutationString(results, sb, 0);
        for (String s : results) {
            System.out.println(s);
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940159&siteId=291194637