📜  重新排列 N 个数字和 M 个字母的方法计数,使所有字母保持在一起

📅  最后修改于: 2022-05-13 01:56:06.889000             🧑  作者: Mango

重新排列 N 个数字和 M 个字母的方法计数,使所有字母保持在一起

给定两个正整数NM ,分别表示字符串中不同数字和字母的计数,计算重新排列字符串字符以使所有字母相邻的方式的数量的任务。

例子:

朴素方法:解决此问题的最简单方法是制作一个由N个不同的数字字符和M个不同的字母组成的字符串。现在,生成字符串的所有可能排列并检查字符串的所有字母是否相邻。如果发现为真,则增加计数。最后,打印获得的计数。

时间复杂度: O((N + M)!)
辅助空间: O(N + M)

有效方法:可以根据以下观察解决问题:

请按照以下步骤解决问题:

  1. 计算N + 1的阶乘,例如XM的阶乘,例如Y
  2. 最后,打印(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)