Hướng dẫn all permutations of names python - tất cả các hoán vị của tên python
Tại sao bạn không đơn giản làm:
Bạn không nhận được trùng lặp như bạn có thể thấy: Cải thiện bài viết Lưu bài viết Một hoán vị cũng được gọi là một số sắp xếp của người Viking, hoặc đơn đặt hàng, là một sự sắp xếp lại các yếu tố của một danh sách được đặt hàng thành một thư từ một-một với chính s. Một chuỗi độ dài n có n! hoán vị. & nbsp; Nguồn: Mathword (http://mathworld.wolfram.com/permuting.html) Dưới đây là các hoán vị của chuỗi ABC. & NBSP; ABC ACB BAC BCA CBA Cab Dưới đây là một giải pháp được sử dụng làm cơ sở trong việc quay lại. Python3
0List 2
4
6 7 8 8 ABC ACB BAC BCA CBA CAB0 ABC ACB BAC BCA CBA CAB1 ABC ACB BAC BCA CBA CAB2 ABC ACB BAC BCA CBA CAB3
ABC ACB BAC BCA CBA CAB5 ABC ACB BAC BCA CBA CAB6 ABC ACB BAC BCA CBA CAB1 ABC ACB BAC BCA CBA CAB8 ABC ACB BAC BCA CBA CAB9 Enter the string : abc All possible strings are : abc acb bac bca cab cba0 Enter the string : abc All possible strings are : abc acb bac bca cab cba1 Enter the string : abc All possible strings are : abc acb bac bca cab cba2___ Enter the string : abc All possible strings are : abc acb bac bca cab cba6 Enter the string : abc All possible strings are : abc acb bac bca cab cba7 8 Enter the string : abc All possible strings are : abc acb bac bca cab cba9 Enter the string : abc All possible strings are : abc acb bac bca cab cba6 def 1Enter the string : abc All possible strings are : abc acb bac bca cab cba3 Enter the string : abc All possible strings are : abc acb bac bca cab cba4 def 4Enter the string : abc All possible strings are : abc acb bac bca cab cba6 Enter the string : abc All possible strings are : abc acb bac bca cab cba7 8 Enter the string : abc All possible strings are : abc acb bac bca cab cba9 Enter the string : abc All possible strings are : abc acb bac bca cab cba6 def 1Enter the string : abc All possible strings are : abc acb bac bca cab cba3 Enter the string : abc All possible strings are : abc acb bac bca cab cba4 def 4
8 toString( 1
8 toString( 4toString( 5
Enter the string : abc All possible strings are : abc acb bac bca cab cba4 2Output: ABC ACB BAC BCA CBA CAB
Mô hình thuật toán: Backtracking & nbsp;O(n*n!) Note that there are n! permutations and it requires O(n) time to print a permutation. Độ phức tạp về thời gian: O (n*n!) Lưu ý rằng có n! hoán vị và nó yêu cầu thời gian O (n) để in hoán vị.O(r – l) Không gian phụ trợ: O (R - L) The above solution prints duplicate permutations
if there are repeating characters in the input string. Please see the below link for a solution that prints only distinct permutations even if there are duplicates in input. Cách tiếp cận khác: Python3
6 ): 0toString( 4): 2 8 8 List 1): ABC ACB BAC BCA CBA CAB1 ABC ACB BAC BCA CBA CAB2 ): 9 8 1 2ABC ACB BAC BCA CBA CAB1 return
ABC ACB BAC BCA CBA CAB8 ABC ACB BAC BCA CBA CAB9 Enter the string : abc All possible strings are : abc acb bac bca cab cba0 Enter the string : abc All possible strings are : abc acb bac bca cab cba1 ): 0toString( 4return 2ABC ACB BAC BCA CBA CAB1 return 4 8 return 6ABC ACB BAC BCA CBA CAB1 return 8 8 00List 1 02ABC ACB BAC BCA CBA CAB1 04 8 06Enter the string : abc All possible strings are : abc acb bac bca cab cba3 Enter the string : abc All possible strings are : abc acb bac bca cab cba4 09ABC ACB BAC BCA CBA CAB1 11 8 return 8Enter the string : abc All possible strings are : abc acb bac bca cab cba3 15ABC ACB BAC BCA CBA CAB1 17Enter the string : abc All possible strings are : abc acb bac bca cab cba3 19 20 8 22 23 8 25): 0 27____12ABC ACB BAC BCA CBA CAB2 ): 0 31 2 33Output: Enter the string : abc All possible strings are : abc acb bac bca cab cba Độ phức tạp về thời gian: O (n*n!) Độ phức tạp thời gian giống như cách tiếp cận trên, tức là có n!hoán vị và nó yêu cầu thời gian O (n) để in hoán vị. O(n*n!) The time complexity is the same as the above approach, i.e. there are n! permutations and it requires O(n) time to print a permutation. Không gian phụ trợ: O (| s |)O(|s|) |