Huawei interview hand-torn real questions [arrangement of strings]

Input a string and print out all permutations of the characters in the string.

You can return this string array in any order, but there can be no duplicate elements in it.

        For a typical full permutation problem, according to the characteristics of string permutations, consider depth-first search for all permutations. That is, through character exchange, first fix the first character (n case), then fix the second character (n−1 case), ..., and finally fix the nth character (1 case).   Leek

     

        When there are repeated characters in the character string, there are also repeated permutation schemes in the permutation scheme. In order to eliminate the repetition scheme, when fixing a certain character, it is necessary to ensure that "each character is only fixed at this position once", that is, when a repeated character is encountered, it is not exchanged and skipped directly. From a DFS perspective, this operation is called "pruning".

import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
 
class Main {
    public static List<String> res = new LinkedList<>();
    public static char[] c;
	public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String input_str = in.nextLine();
        
        System.out.println(Arrays.toString(permutation(input_str)));
        
    }

    public static String[] permutation(String s) {
        c = s.toCharArray();
        dfs(0);
        return res.toArray(new String[res.size()]);
    }
    public static void dfs(int x) {
        if(x == c.length - 1) {
            res.add(String.valueOf(c));      // 添加排列方案
            return;
        }
        HashSet<Character> set = new HashSet<>();
        for(int i = x; i < c.length; i++) {
            if(set.contains(c[i])) continue; // 重复,因此剪枝
            set.add(c[i]);
            swap(i, x);                      // 交换,将 c[i] 固定在第 x 位
            dfs(x + 1);                      // 开启固定第 x + 1 位字符
            swap(i, x);                      // 恢复交换
        }
    }
    public static void swap(int a, int b) {
        char tmp = c[a];
        c[a] = c[b];
        c[b] = tmp;
    }
 
}

  Huawei OD 2023/2022 latest test questions and explanations, 100% pass rate.

Java: https://renjie.blog.csdn.net/article/details/127947829

Python: https://renjie.blog.csdn.net/article/details/127946125

C++: https://renjie.blog.csdn.net/article/details/126965954

Js: https://renjie.blog.csdn.net/article/details/128974467

C: https://renjie.blog.csdn.net/article/details/129190260

  Huawei OD 2023/2022 latest test questions and explanations, 100% pass rate.

Java: https://renjie.blog.csdn.net/article/details/127947829

Python: https://renjie.blog.csdn.net/article/details/127946125

C++: https://renjie.blog.csdn.net/article/details/126965954

Js: https://renjie.blog.csdn.net/article/details/128974467

C: https://renjie.blog.csdn.net/article/details/129190260

  Huawei OD 2023/2022 latest test questions and explanations, 100% pass rate.

Java: https://renjie.blog.csdn.net/article/details/127947829

Python: https://renjie.blog.csdn.net/article/details/127946125

C++: https://renjie.blog.csdn.net/article/details/126965954

Js: https://renjie.blog.csdn.net/article/details/128974467

C: https://renjie.blog.csdn.net/article/details/129190260

Guess you like

Origin blog.csdn.net/misayaaaaa/article/details/130426866