📌  相关文章
📜  查找至少重复一个字符的M个字符单词的数量

📅  最后修改于: 2021-05-06 21:05:52             🧑  作者: Mango

给定两个整数NM ,任务是对由给定的N个不同字符形成的M个字符长度的单词总数进行计数,以使单词中至少一个字符重复一次以上。

例子:

方法:
N个字符可能存在的M个字符词的总数总数= N M。
N个字符(不重复任何字符可能存在的M个字符单词的总数, noRepeat = N P M。
因此,至少一个字符出现多次的单词总数为– noRepeat,N MN P M。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include  
#include 
using namespace std;
  
// Function to return the
// factorial of a number
int fact(int n)
{
    if (n <= 1)
        return 1;
    return n * fact(n - 1);
}
  
// Function to return the value of nPr
int nPr(int n, int r)
{
    return fact(n) / fact(n - r);
}
  
// Function to return the total number of
// M length words which have at least a
// single character repeated more than once
int countWords(int N, int M)
{
    return pow(N, M) - nPr(N, M);
}
  
// Driver code
int main()
{
    int N = 10, M = 5;
    cout << (countWords(N, M));
    return 0;
}
  
// This code is contributed by jit_t


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the
    // factorial of a number
    static int fact(int n)
    {
        if (n <= 1)
            return 1;
        return n * fact(n - 1);
    }
  
    // Function to return the value of nPr
    static int nPr(int n, int r)
    {
        return fact(n) / fact(n - r);
    }
  
    // Function to return the total number of
    // M length words which have at least a
    // single character repeated more than once
    static int countWords(int N, int M)
    {
        return (int)Math.pow(N, M) - nPr(N, M);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int N = 10, M = 5;
        System.out.print(countWords(N, M));
    }
}


Python3
# Python3 implementation for the above approach
  
# Function to return the
# factorial of a number
def fact(n):
  
    if (n <= 1):
        return 1;
    return n * fact(n - 1);
  
# Function to return the value of nPr
def nPr(n, r):
  
    return fact(n) // fact(n - r);
  
# Function to return the total number of
# M length words which have at least a
# single character repeated more than once
def countWords(N, M):
  
    return pow(N, M) - nPr(N, M);
  
# Driver code
N = 10; M = 5;
print(countWords(N, M));
  
# This code is contributed by Code_Mech


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
    // Function to return the
    // factorial of a number
    static int fact(int n)
    {
        if (n <= 1)
            return 1;
        return n * fact(n - 1);
    }
  
    // Function to return the value of nPr
    static int nPr(int n, int r)
    {
        return fact(n) / fact(n - r);
    }
  
    // Function to return the total number of
    // M length words which have at least a
    // single character repeated more than once
    static int countWords(int N, int M)
    {
        return (int)Math.Pow(N, M) - nPr(N, M);
    }
  
    // Driver code
    static public void Main ()
    {
        int N = 10, M = 5;
        Console.Write(countWords(N, M));
    }
}
  
// This code is contributed by ajit.


输出:
69760