# Find the count of M character words which have at least one character repeated

Given two integers **N** and **M**, the task is count the total words of **M** character length formed by the given **N** distinct characters such that the words have at least one character repeated more than once.

**Examples:**

Input:N = 3, M = 2

Output:3

Suppose the characters are {‘a’, ‘b’, ‘c’}

All 2 length words that can be formed with these characters

are “aa”, “ab”, “ac”, “ba”, “bb”, “bc”, “ca”, “cb” and “cc”.

Out of these words only “aa”, “bb” and “cc” have

at least one character repeated more than once.

Input:N = 10, M = 5

Output:69760

**Approach:**

Total number of M character words possible from N characters, **total = N ^{M}**.

Total number of M character words possible from N characters where no character repeats itself,

**noRepeat =**.

^{N}P_{M}So, total words where at least a single character appear more than once is

**total – noRepeat**i.e.

**N**.

^{M}–^{N}P_{M}Below is the implementation of the above approach:

## C++

`// C++ implementation for the above approach ` `#include <math.h> ` `#include <iostream> ` `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 ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

**Output:**

69760

## Recommended Posts:

- Print number of words, vowels and frequency of each character
- Shortest distance to every other character from given character
- Count all substrings having character K
- Count of sub-strings that do not consist of the given character
- Java program to count the occurrences of each character
- Count occurrences of a given character using Regex in Java
- Count occurrence of a given character in a string using Stream API in Java
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Sub-strings that start and end with one character and have at least one other
- Java Program to get a character from a String
- Nth character in Concatenated Decimal String
- Character.isIdentifierIgnorable() in Java with Examples
- Character.offsetByCodePoints() in Java with Examples
- Character.offsetByCodePoints() Method in java

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.