重新排列 N 个数字和 M 个字母的方法计数,使所有字母保持在一起
给定两个正整数N和M ,分别表示字符串中不同数字和字母的计数,计算重新排列字符串字符以使所有字母相邻的方式的数量的任务。
例子:
Input: N = 2, M = 2
Output: 12
Explanation: Possible ways to rearrange characters of a string such that all alphabets are adjacent: { {N1N2M2M1, N2N1M2M1, N2N1M1M2, N1N2M1M2, M2M1N1N2, M1M2N2N1, M1M2N1N2, M2M1N2N1, N1M1M2N2, N2M1M2N1, N1M2M1N2, N2M2M1N1} }.
Input: N = 2, M = 4
Output: 144
朴素方法:解决此问题的最简单方法是制作一个由N个不同的数字字符和M个不同的字母组成的字符串。现在,生成字符串的所有可能排列并检查字符串的所有字母是否相邻。如果发现为真,则增加计数。最后,打印获得的计数。
时间复杂度: O((N + M)!)
辅助空间: O(N + M)
有效方法:可以根据以下观察解决问题:
Since all alphabets are adjacent, therefore consider all the alphabets as a single character.
Therefore, the total count of ways to rearrange the string by considering all alphabets to a single character = ((N + 1)!) * (M!)
请按照以下步骤解决问题:
- 计算N + 1的阶乘,例如X和M的阶乘,例如Y 。
- 最后,打印(X * Y)的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the factorial
// of the given number
int fact(int n)
{
int ans = 1;
for(int i = 2; i <= n; i++)
ans = ans * i;
return ans;
}
// Function to count ways to rearrange
// characters of the string such that
// all alphabets are adjacent.
int findComb(int N, int M)
{
// Stores factorial of (N + 1)
int x = fact(N + 1);
// Stores factorial of
int y = fact(M);
return (x * y);
}
// Driver Code
int main()
{
// Given a and b
int N = 2;
int M = 2;// Function call
cout<
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the factorial
// of the given number
static int fact(int n)
{
int ans = 1;
for(int i = 2; i <= n; i++)
ans = ans * i;
return ans;
}
// Function to count ways to rearrange
// characters of the String such that
// all alphabets are adjacent.
static int findComb(int N, int M)
{
// Stores factorial of (N + 1)
int x = fact(N + 1);
// Stores factorial of
int y = fact(M);
return (x * y);
}
// Driver Code
public static void main(String[] args)
{
// Given a and b
int N = 2;
int M = 2;
// Function call
System.out.print(findComb(N, M));
}
}
// This code is contributed by umadevi9616
Python3
# Python program of the above approach
import math
# Function to find the factorial
# of the given number
def fact(a):
return math.factorial(a)
# Function to count ways to rearrange
# characters of the string such that
# all alphabets are adjacent.
def findComb(N, M):
# Stores factorial of (N + 1)
x = fact(N + 1)
# Stores factorial of
y = fact(M)
return (x * y)
# Driver Code
if __name__ == "__main__":
# Given a and b
N = 2
M = 2
# Function call
print(findComb(N, M))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the factorial
// of the given number
static int fact(int n)
{
int ans = 1;
for(int i = 2; i <= n; i++)
ans = ans * i;
return ans;
}
// Function to count ways to rearrange
// characters of the string such that
// all alphabets are adjacent.
static int findComb(int N, int M)
{
// Stores factorial of (N + 1)
int x = fact(N + 1);
// Stores factorial of
int y = fact(M);
return (x * y);
}
// Driver Code
public static void Main()
{
// Given a and b
int N = 2;
int M = 2;// Function call
Console.Write(findComb(N, M));
}
}
// This code is contributed by bgangwar59.
Javascript
12
时间复杂度: O(N + M)
辅助空间: O(1)