📌  相关文章
📜  给定字符串的每个可能长度的不同排列的计数

📅  最后修改于: 2021-09-06 17:47:01             🧑  作者: Mango

给定一个字符串S,任务是计算给定字符串的每个可能长度的不同排列。

注意:字符串不允许重复字符。

方法:这个想法是找到字符串的每个可能长度的组合数,它们的总和是不同长度可能的不同排列的总数。因此,对于 N 个长度的字符串,不同长度的不同排列的总数为:

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
 
#include 
#include 
using namespace std;
 
// Function to find the factorial
// of a number
int fact(int a)
{
    int i, f = 1;
 
    // Loop to find the factorial
    // of the given number
    for (i = 2; i <= a; i++)
        f = f * i;
    return f;
}
 
// Function to find the number
// of permutations possible
// for a given string
int permute(int n, int r)
{
    int ans = 0;
    ans = (fact(n) / fact(n - r));
    return ans;
}
 
// Function to find the total
// number of combinations possible
int findPermutations(int n)
{
    int sum = 0, P;
    for (int r = 1; r <= n; r++) {
        P = permute(n, r);
        sum = sum + P;
    }
    return sum;
}
 
// Driver Code
int main()
{
    string str = "xz";
    int result, n;
    n = str.length();
 
    cout << findPermutations(n);
    return 0;
}


Java
// Java implementation of the
// above approach
class GFG{
 
// Function to find the factorial
// of a number
static int fact(int a)
{
    int i, f = 1;
 
    // Loop to find the factorial
    // of the given number
    for(i = 2; i <= a; i++)
        f = f * i;
     
    return f;
}
 
// Function to find the number
// of permutations possible
// for a given String
static int permute(int n, int r)
{
    int ans = 0;
    ans = (fact(n) / fact(n - r));
    return ans;
}
 
// Function to find the total
// number of combinations possible
static int findPermutations(int n)
{
    int sum = 0, P;
    for(int r = 1; r <= n; r++)
    {
        P = permute(n, r);
        sum = sum + P;
    }
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "xz";
    int result, n;
    n = str.length();
 
    System.out.print(findPermutations(n));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to find the factorial
# of a number
def fact(a):
 
    f = 1
 
    # Loop to find the factorial
    # of the given number
    for i in range(2, a + 1):
        f = f * i
 
    return f
 
# Function to find the number
# of permutations possible
# for a given string
def permute(n, r):
 
    ans = 0
    ans = fact(n) // fact(n - r)
 
    return ans
 
# Function to find the total
# number of combinations possible
def findPermutations(n):
 
    sum = 0
    for r in range(1, n + 1):
        P = permute(n, r)
        sum = sum + P
 
    return sum
 
# Driver Code
str = "xz"
n = len(str)
 
# Function call
print(findPermutations(n))
 
# This code is contributed by Shivam Singh


C#
// C# implementation of the
// above approach
using System;
 
class GFG{
 
// Function to find the factorial
// of a number
static int fact(int a)
{
    int i, f = 1;
 
    // Loop to find the factorial
    // of the given number
    for(i = 2; i <= a; i++)
        f = f * i;
     
    return f;
}
 
// Function to find the number
// of permutations possible
// for a given String
static int permute(int n, int r)
{
    int ans = 0;
    ans = (fact(n) / fact(n - r));
    return ans;
}
 
// Function to find the total
// number of combinations possible
static int findPermutations(int n)
{
    int sum = 0, P;
    for(int r = 1; r <= n; r++)
    {
        P = permute(n, r);
        sum = sum + P;
    }
    return sum;
}
 
// Driver Code
public static void Main(String[] args)
{
    String str = "xz";
    int n;
    n = str.Length;
 
    Console.Write(findPermutations(n));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
4

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live