Computer Science, asked by davarenisha6, 2 months ago

Your task is to complete a function GetArrangements() that returns the count of all the possible arrangements of length N that can be derived from the characters of an input string of length N. The input string may or may not have repeated characters. Your function should handle all the possible cases.

For example,
for the input provided as follows:

ABCB

The function should return

12

Explanation:

For input string, “ABCB” whose length is 4, the possible sub-strings of length 4 are ABCB, ACBB, ABBC, BBAC, BABC, BACB, CBAB, CABB, CBBA, BBCA, BCBA and BCAB. Therefore your function should return 12 for this input string

Another example,
for the input provided as follows:

ABC

The function should return
6

Answers

Answered by manveerrao0
2

Explanation:

In second example, number of character is 11 and here h and y are repeated 2 times whereas g is repeated 3 times.

So, number of permutation is 11! / (2!2!3!) = 1663200

Below is the implementation of above idea.

C++

// C++ program to find number of distinct

// permutations of a string.

#include<bits/stdc++.h>

using namespace std;

const int MAX_CHAR = 26;

// Utility function to find factorial of n.

int factorial(int n)

{

int fact = 1;

for (int i = 2; i <= n; i++)

fact = fact * i;

return fact;

}

// Returns count of distinct permutations

// of str.

int countDistinctPermutations(string str)

{

int length = str.length();

int freq[MAX_CHAR];

memset(freq, 0, sizeof(freq));

// finding frequency of all the lower case

// alphabet and storing them in array of

// integer

for (int i = 0; i < length; i++)

if (str[i] >= 'a')

freq[str[i] - 'a']++;

// finding factorial of number of appearances

// and multiplying them since they are

// repeating alphabets

int fact = 1;

for (int i = 0; i < MAX_CHAR; i++)

fact = fact * factorial(freq[i]);

printf("%d", countDistinctPermutations(str));

return 0;

}

Java

Python

C#

Output:

840

Answered by lavanyapalaparthi000
0

Explanation:

for input string "ABCD" whose length is 4 , the possible sub strings of length 4 are ABCD,ACBB,ABBC,BBAC,BABC,BACB,CBAB,CABB,CBBA,BBCA,BCBA,BCAB.therefore your function should return 12 for this input string

Similar questions