# Find the number of strings formed using distinct characters of a given string

Given a string str consisting of lowercase English alphabets, the task is to find the count of all possible string of maximum length that can be formed using the characters of str such that no two characters in the generated string are same.

Examples:

Input: str = “aba”
Output: 2
“ab” and “ba” are the only valid strings.

Input: str = “geeksforgeeks”
Output: 5040

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: First, count the number of distinct characters in the string say cnt as no two characters can be same in the resultant string. Now, the total number of strings that can be formed with cnt number of characters is cnt! as every character of str has to be present in the generated string in order to maximise the length and no character should appear more than once.

Below is the implementation of the above approach:

## 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<``char``> 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<``char``> distinct_char = ``new` `HashSet<``char``>(); ` `    ``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 `

Output:

```5040
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.