# Number of strings of length N with no palindromic sub string

Given two positive integers **N, M**. The task is to find the number of strings of length N under the alphabet set of size M such that no substrings of size greater than 1 is palindromic.

Examples:

Input : N = 2, M = 3 Output : 6 In this case, set of alphabet are 3, say {A, B, C} All possible string of length 2, using 3 letters are: {AA, AB, AC, BA, BB, BC, CA, CB, CC} Out of these {AA, BB, CC} contain palindromic substring, so our answer will be 8 - 2 = 6. Input : N = 2, M = 2 Output : 2 Out of {AA, BB, AB, BA}, only {AB, BA} contain non-palindromic substrings.

First, observe, a string does not contain any palindromic substring if the string doesn’t have any palindromic substring of the length 2 and 3, because all the palindromic string of the greater lengths contains at least one palindromic substring of the length of 2 or 3, basically in the center.

So, the following is true:

- There are M ways to choose the first symbol of the string.
- Then there are (M – 1) ways to choose the second symbol of the string. Basically, it should not be equal to first one.
- Then there are (M – 2) ways to choose any next symbol. Basically, it should not coincide with the previous symbols, that aren’t equal.

Knowing this, we can evaluate the answer in the following ways:

- If N = 1, then the answer will be M.
- If N = 2, then the answer is M*(M – 1).
- If N >= 3, then M * (M – 1) * (M – 2)
^{N-2}.

Below is the implementation of above idea :

## C++

`// CPP program to count number of strings of ` `// size m such that no substring is palindrome. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the count of strings with ` `// no palindromic substring. ` `int` `numofstring(` `int` `n, ` `int` `m) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `m; ` ` ` ` ` `if` `(n == 2) ` ` ` `return` `m * (m - 1); ` ` ` ` ` `return` `m * (m - 1) * ` `pow` `(m - 2, n - 2); ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 2, m = 3; ` ` ` `cout << numofstring(n, m) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of strings of ` `// size m such that no substring is palindrome. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the count of strings with ` ` ` `// no palindromic substring. ` ` ` `static` `int` `numofstring(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `m; ` ` ` ` ` `if` `(n == ` `2` `) ` ` ` `return` `m * (m - ` `1` `); ` ` ` ` ` `return` `m * (m - ` `1` `) * (` `int` `)Math.pow(m - ` `2` `, n - ` `2` `); ` ` ` `} ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `2` `, m = ` `3` `; ` ` ` `System.out.println(numofstring(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number of strings of ` `# size m such that no substring is palindrome ` ` ` `# Return the count of strings with ` `# no palindromic substring. ` `def` `numofstring(n, m): ` ` ` `if` `n ` `=` `=` `1` `: ` ` ` `return` `m ` ` ` ` ` `if` `n ` `=` `=` `2` `: ` ` ` `return` `m ` `*` `(m ` `-` `1` `) ` ` ` ` ` `return` `m ` `*` `(m ` `-` `1` `) ` `*` `pow` `(m ` `-` `2` `, n ` `-` `2` `) ` ` ` `# Driven Program ` `n ` `=` `2` `m ` `=` `3` `print` `(numofstring(n, m)) ` ` ` `# This code is contributed ` `# by Shreyanshi Arun. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of strings of ` `// size m such that no substring is palindrome. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the count of strings with ` ` ` `// no palindromic substring. ` ` ` `static` `int` `numofstring(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `m; ` ` ` ` ` `if` `(n == 2) ` ` ` `return` `m * (m - 1); ` ` ` ` ` `return` `m * (m - 1) * (` `int` `)Math.Pow(m - 2, ` ` ` `n - 2); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 2, m = 3; ` ` ` `Console.Write(numofstring(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number ` `// of strings of size m such ` `// that no substring is palindrome. ` ` ` `// Return the count of strings with ` `// no palindromic substring. ` `function` `numofstring(` `$n` `, ` `$m` `) ` `{ ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `$m` `; ` ` ` ` ` `if` `(` `$n` `== 2) ` ` ` `return` `$m` `* (` `$m` `- 1); ` ` ` ` ` `return` `$m` `* (` `$m` `- 1) * ` ` ` `pow(` `$m` `- 2, ` `$n` `- 2); ` `} ` ` ` `// Driver Code ` `{ ` ` ` `$n` `= 2; ` `$m` `= 3; ` ` ` `echo` `numofstring(` `$n` `, ` `$m` `) ; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output**

6

## Recommended Posts:

- Check if all the palindromic sub-strings are of odd length
- Check if a string contains a palindromic sub-string of even length
- Find all palindromic sub-strings of a given string | Set 2
- Find all distinct palindromic sub-strings of a given string
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Number of palindromic subsequences of length k where k <= 3
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Count of sub-strings of length n possible from the given string
- All possible strings of any length that can be formed from a given string
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Make palindromic string non-palindromic by rearranging its letters
- Rearrange the string to maximize the number of palindromic substrings
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Count number of binary strings of length N having only 0's and 1's

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.