VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-creating-strings-ii/

⇱ CSES Solutions - Creating Strings II - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Creating Strings II

Last Updated : 23 Jul, 2025

Given a string, your task is to calculate the number of different strings that can be created using its characters.

Examples:

Input: S = "aa"
Output: 1
Explanation: There is only 1 possible string: "aa".

Input: S = "aabac"
Output: 20
Explanation: There are 20 possible strings: "aaabc", "aaacb", "aabac", "aabca", "aacab", "aacba", "abaac", "abaca", "abcaa", "acaab", "acaba", "acbaa", "baaac", "baaca", "bacaa", "bcaaa", "caaab", "caaba", "cabaa" and "cbaaa".

Approach: To solve the problem, follow the below idea:

We can use the concept of permutations to solve this problem. Given a string of the length n the number of the different strings that can be created using its characters is n! where n! represents the factorial of the n.

However, since the characters in the given string might be repeated we need to the consider the repetitions. We'll calculate the factorial of the length of string and then divide it by the factorial of the count of the each character's occurrence.

Below is the implementation of the algorithm:


Output
20

Time Complexity: O(N logN)
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: