Given n unique characters that are available in unlimited supply, find the number of possible mapped strings of length n. A mapped string follows these properties:
- The string is divided into two equal halves.
- Both halves contain the same set of characters.
- Each half contains exactly n / 2 unique characters.
- n is always even.
For example, "abccba" is a mapped string because the left half "abc" and the right half "cba" contain the same set of unique characters. However, "abcbcd" is not a mapped string because the left half "abc" and the right half "bcd" contain different sets of characters.
Since the answer can be very large, return the answer modulo 10^9 + 7.
Examples:
Input: n = 2
Output: 2
Explanation: Suppose two unique characters are 'a' and 'b', then there will be two mapped strings are 'aa' and 'bb'.Input: n = 4
Output: 24
Explanation: Suppose four unique characters are 'a', 'b', 'c' and 'd' and for each pair of them there will be four mapped strings. Hence, there are total 6 pairs so 6 * 4 = 24 total mapped strings.
Count Mapped Strings Using Factorial - O(n) Time and O(1) Auxiliary Space
Let k = n / 2. A mapped string contains the same set of k unique characters in both halves.
First, choose k characters from n available characters. This can be done in C(n, k) ways.
For each selected set:
- The k characters can be arranged in the left half in k! ways.
- The same k characters can be arranged independently in the right half in k! ways.
Therefore, the total number of mapped strings is: C(n, k) × k! × k!
Using the combination formula: C(n, k) = n! / (k! × (n - k)!)
Since k = n / 2, we have n - k = k. Therefore: C(n, k) = n! / (k! × k!)
Substituting this into the total count: C(n, k) × k! × k!
= [n! / (k! × k!)] × k! × k!
= n!
Thus, the number of mapped strings is simply n!.
Since n can be as large as 10^5, calculate n! iteratively and take modulo 10^9 + 7 at every step.
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
// Function to find the number of mapped strings.
long long mapStr(long long n) {
long long ans = 1;
// Calculate n! modulo MOD.
for (long long i = 1; i <= n; i++) {
ans = (ans * i) % MOD;
}
return ans;
}
int main() {
long long n = 4;
cout << mapStr(n) << endl;
return 0;
}
class GFG {
static final long MOD = 1000000007;
// Function to find the number of mapped strings.
static long mapStr(long n) {
long ans = 1;
// Calculate n! modulo MOD.
for (long i = 1; i <= n; i++) {
ans = (ans * i) % MOD;
}
return ans;
}
public static void main(String[] args) {
long n = 4;
System.out.println(mapStr(n));
}
}
# Function to find the number of mapped strings.
def mapStr(n):
MOD = 1000000007
ans = 1
# Calculate n! modulo MOD.
for i in range(1, n + 1):
ans = (ans * i) % MOD
return ans
if __name__ == "__main__":
n = 4
print(mapStr(n))
using System;
class GFG
{
// Function to find the number of mapped strings.
static long mapStr(long n)
{
const long MOD = 1000000007;
long ans = 1;
// Calculate n! modulo MOD.
for (long i = 1; i <= n; i++)
{
ans = (ans * i) % MOD;
}
return ans;
}
static void Main()
{
long n = 4;
Console.WriteLine(mapStr(n));
}
}
// Function to find the number of mapped strings.
function mapStr(n) {
const MOD = 1000000007;
let ans = 1;
// Calculate n! modulo MOD.
for (let i = 1; i <= n; i++) {
ans = (ans * i) % MOD;
}
return ans;
}
// Driver code
const n = 4;
console.log(mapStr(n));
Output
24