Hướng dẫn count number of permutations of a string python - đếm số hoán vị của một chuỗi python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples: 

    Input : aab
    Output : 3
    Different permutations are "aab",
    "aba" and "baa".
    
    Input : ybghjhbuytb
    Output : 1663200

    Bàn luậnsimple solution is to find all the distinct permutation and count them.

    Chúng tôi được cung cấp một chuỗi chỉ có bảng chữ cái viết thường. Nhiệm vụ là tìm hiểu tổng số hoán vị riêng biệt có thể được tạo bởi chuỗi đó.without finding all permutation. Idea is to find all the characters that is getting repeated, i.e., frequency of all the character. Then, we divide the factorial of the length of string by multiplication of factorial of frequency of characters.

    Một giải pháp đơn giản là tìm tất cả các hoán vị khác biệt và đếm chúng.h and y are repeated 2 times whereas g is repeated 3 times. 
    So, number of permutation is 11! / (2!2!3!) = 1663200

    Chúng ta có thể tìm thấy số lượng mà không tìm thấy tất cả hoán vị. Ý tưởng là tìm tất cả các nhân vật đang được lặp lại, tức là tần suất của tất cả các nhân vật. Sau đó, chúng tôi chia độ giai đoạn về độ dài của chuỗi bằng cách nhân của giai đoạn tần số của các ký tự.

    C++

    #include

    Trong ví dụ thứ hai, số lượng ký tự là 11 và ở đây H và Y được lặp lại 2 lần trong khi g được lặp lại 3 lần. & NBSP; Vì vậy, số lần hoán vị là 11! / (2! 2! 3!) = 1663200

    Dưới đây là việc thực hiện ý tưởng trên. & NBSP;

    using namespace std;

    #include2

    const int MAX_CHAR = 26;

    int factorial(int #include1

    using1using2

    #include3int #include5

    using6

    #include3#include7 #include8int using0

    #include2

    #include3using4 using5

    int using8

    #include3namespace7namespace8namespace9std;0

    #include3int namespace2

    #include3int namespace5

    #include3#include7 #include8int std;5

    const int MAX_CHAR = 26;

    int factorial(int #include1

    using1int4

    #include3int #include5

    using6

    #include3#include7 #include8int using0

    #include2

    #include3MAX_CHAR = 26;3MAX_CHAR = 26;4MAX_CHAR = 26;5

    #include3MAX_CHAR = 26;7#include8MAX_CHAR = 26;9int0

    #include3using4 using5

    using6

    int using8

    #include3int namespace2

    #include3int namespace5

    #include3#include7 #include8int std;5

    #include3#include2

    using1std;7 std;8std;9const0

    const1const2std;9const4

    const1using2

    #include3#include7 #include8int int2

    #include3using6

    #include3using4 int7

    #include3#include2

    int MAX_CHAR = 26;0

    #include3using4 int3

    Java

    int5 int6 int7

    #include49#include50std;9const4

    using1std;7 std;8std;9const0

    const1const2std;9const4

    const1int4

    #include3#include7 #include8int int2

    #include3using6

    #include3using4 int7

    #include3#include2

    using1#include80MAX_CHAR = 26;4MAX_CHAR = 26;5

    using1#include84

    #include3using6

    using6

    Python3

    int MAX_CHAR = 26;0

    #include3using4 int3

    Java

    int5 int6 int7

    #include3int9 factorial(0 int factorial(2factorial(3MAX_CHAR = 26;5

    #include3int9 int factorial(int #include1

    using1int #include05#include06MAX_CHAR = 26;5

    using1#include7 #include8int #include12#include13#include14

    using1using4 using5

    #include3int9 int #include25

    using1int namespace2

    using1int#include33#include34 int#include36

    using1#include7 #include8int #include12#include42#include43

    const1std;7 #include46std;9const0

    using1#include7 #include8int #include12#include42#include64

    using1using4 int7

    #include3int5 int9 #include75 #include76

    #include88#include89 factorial(3

    C#

    #include91 #include92

    #include3int namespace2

    #include3int namespace5

    #include3#include7 #include8int std;5

    #include3#include2

    using1std;7 std;8std;9const0

    const1const2std;9const4

    const1using2

    #include3#include7 #include8int int2

    #include3using6

    #include3using4 int7

    #include3#include2

    int MAX_CHAR = 26;0

    #include3using4 int3

    Java

    int5 int6 int7

    #include49const2std;9const4

    using1int #include5

    using1#include7 #include8int int2

    const1int4

    using1using4 int7

    #include3using6

    #include3int5 int9 #include75 namespace73

    #include3#include2

    using1#include80MAX_CHAR = 26;4MAX_CHAR = 26;5

    using1namespace81

    #include3using6

    using6

    JavaScript

    namespace85

    #include3namespace87

    #include3namespace89 namespace90

    #include3#include2

    using1namespace94

    using1#include7 namespace97

    const1using2

    using1using4 using5

    #include3using6

    #include3namespace89 std;07

    #include3#include2

    using1std;11

    using1std;13#include34 std;15

    using1std;17

    using1#include7 std;20

    const1std;7 std;23std;9std;25

    #include49std;27std;9std;29

    using1namespace94

    using1#include7 std;34

    const1int4

    using1using4 std;39

    #include3using6

    #include3std;43MAX_CHAR = 26;4MAX_CHAR = 26;5

    #include3std;47

    std;48

    Độ phức tạp về thời gian: O (n), trong đó n là độ dài của không gian chuỗi chuỗi where n is the length of the string
    Auxiliary Space: O(n), where n is the length of the string

    Bài viết này được đóng góp bởi Aditya Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. & NBSP;


    Làm thế nào để bạn đếm hoán vị của một chuỗi?

    Chúng ta có thể tìm thấy số lượng mà không tìm thấy tất cả hoán vị.Ý tưởng là tìm tất cả các nhân vật đang được lặp lại, tức là tần suất của tất cả các nhân vật.Sau đó, chúng tôi chia độ giai đoạn về độ dài của chuỗi bằng cách nhân của giai đoạn tần số của các ký tự.

    Làm thế nào để bạn tìm thấy các hoán vị của một yếu tố trong danh sách Python?

    Ngày nay, chúng ta sẽ học cách có được các hoán vị có thể của một danh sách bằng cách sử dụng các phương thức khác nhau trong Python ...
    nhập itertools ..
    L = [2, 4, 6].
    r = 2 ..
    P = list (itertools. Permutations (L, R)).
    print(p).

    Làm thế nào để bạn tìm thấy số lượng hoán vị khác biệt?

    Bắt đầu từ một tập hợp các đối tượng N theo một số thứ tự tham chiếu (ví dụ: trình tự số 1, 2, 3,, n), chúng ta có thể đưa chúng vào một số thứ tự khác;Tổng số hoán vị riêng biệt có thể là N!(Chọn đối tượng đầu tiên n cách, sau đó chọn thứ hai theo n - 1 cách, v.v.).n! (choose the first object n ways, then choose the second in n − 1 ways, etc.).