📌  相关文章
📜  查找使用给定字符串的不同字符形成的字符串数

📅  最后修改于: 2022-05-13 01:57:08.224000             🧑  作者: Mango

查找使用给定字符串的不同字符形成的字符串数

给定一个由小写英文字母组成的字符串str ,任务是找到所有可能的最大长度字符串的计数,该字符串可以使用str的字符形成,使得生成的字符串中没有两个字符相同。
例子:

方法:首先,计算字符串中不同字符的数量,比如cnt ,因为结果字符串中没有两个字符可以相同。现在,可以用cnt个字符组成的字符串总数是cnt!因为str的每个字符都必须出现在生成的字符串中以最大化长度,并且任何字符都不应出现多次。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the factorial of n
int fact(int n)
{
    int fact = 1;
    for (int i = 1; i <= n; i++)
        fact *= i;
 
    return fact;
}
 
// Function to return the count of all
// possible strings that can be formed
// with the characters of the given string
// without repeating characters
int countStrings(string str, int n)
{
 
    // To store the distinct characters
    // of the string str
    set distinct_char;
    for (int i = 0; i < n; i++) {
        distinct_char.insert(str[i]);
    }
 
    return fact(distinct_char.size());
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int n = str.length();
 
    cout << countStrings(str, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the factorial of n
static int fact(int n)
{
    int fact = 1;
    for (int i = 1; i <= n; i++)
        fact *= i;
 
    return fact;
}
 
// Function to return the count of all
// possible strings that can be formed
// with the characters of the given string
// without repeating characters
static int countStrings(String str, int n)
{
 
    // To store the distinct characters
    // of the string str
    Set distinct_char = new HashSet<>();
    for (int i = 0; i < n; i++)
    {
        distinct_char.add(str.charAt(i));
    }
 
    return fact(distinct_char.size());
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    int n = str.length();
 
    System.out.println(countStrings(str, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
 
# Function to return the factorial of n
def fact(n) :
 
    fact = 1;
    for i in range(1, n + 1) :
        fact *= i;
 
    return fact;
 
# Function to return the count of all
# possible strings that can be formed
# with the characters of the given string
# without repeating characters
def countStrings(string, n) :
 
    # To store the distinct characters
    # of the string str
    distinct_char = set();
    for i in range(n) :
        distinct_char.add(string[i]);
     
    return fact(len(distinct_char));
 
# Driver code
if __name__ == "__main__" :
 
    string = "geeksforgeeks";
    n = len(string);
 
    print(countStrings(string, n));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to return the factorial of n
static int fact(int n)
{
    int fact = 1;
    for (int i = 1; i <= n; i++)
        fact *= i;
 
    return fact;
}
 
// Function to return the count of all
// possible strings that can be formed
// with the characters of the given string
// without repeating characters
static int countStrings(String str, int n)
{
 
    // To store the distinct characters
    // of the string str
    HashSet distinct_char = new HashSet();
    for (int i = 0; i < n; i++)
    {
        distinct_char.Add(str[i]);
    }
 
    return fact(distinct_char.Count);
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    int n = str.Length;
 
    Console.WriteLine(countStrings(str, n));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
5040