Given an even integer **N**, the task is to construct a string such that the total number of **distinct** substrings of that string which are **not a palindrome** equals **N ^{2}**.

**Examples:**

Input:N = 2Output:aabbExplanation:

All the distinct non palindromic substrings areab, abb, aab and aabb.

Therefore, the count of non-palindromic substrings is 4 = 2^{ 2}

Input:N = 4Output:cccczzzzExplanation:

All distinct non-palindromic substrings of the string arecz, czz, czzz, czzzz, ccz, cczz, cczzz, cczzzz, cccz, ccczz, ccczzz, ccczzzz, ccccz, cccczz, cccczzz, cccczzzz.

The count of non-palindromic substrings is 16.

**Approach:**

It can be observed that, if the first **N** characters of a string are same, followed by **N** identical characters different than the first **N** characters, then the count of distinct non-palindromic substrings will be **N ^{2}**.

Proof:N = 3

str = “aaabbb”

The string can be split into two substrings ofNcharacters each: “aaa” and “bbb”

The first character ‘a’ from the first substring formsNdistinct non-palindromic substrings “ab”, “abb”, “abbb” with the second substring.

Similiarly first two characters “aa” forms N distinct non-palindromic substrings “aab”, “aabb”, “aabbb”.

Similarly, remaining N – 2 characters of the first substring each formNdistinct non-palindromic substrings as well.

Therefore, the total number of distinct non-palindromic substrings is equal toN.^{2}

Therefore, to solve the problem, print **‘a’** as the first **N** characters of the string and **‘b’** as the next **N** characters of the string.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to construct a string ` `// having N*N non-palindromic substrings ` `void` `createString(` `int` `N) ` `{ ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `cout << ` `'a'` `; ` ` ` `} ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `cout << ` `'b'` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` ` ` `createString(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement ` `// the above approach ` `class` `GFG{ ` ` ` `// Function to construct a string ` `// having N*N non-palindromic substrings ` `static` `void` `createString(` `int` `N) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `System.out.print(` `'a'` `); ` ` ` `} ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `System.out.print(` `'b'` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` ` ` `createString(N); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to construct a string ` `# having N*N non-palindromic substrings ` `def` `createString(N): ` ` ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `print` `(` `'a'` `, end ` `=` `'') ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `print` `(` `'b'` `, end ` `=` `'') ` ` ` `# Driver Code ` `N ` `=` `4` ` ` `createString(N) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to construct a string ` `// having N*N non-palindromic substrings ` `static` `void` `createString(` `int` `N) ` `{ ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `Console.Write(` `'a'` `); ` ` ` `} ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `Console.Write(` `'b'` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 4; ` ` ` ` ` `createString(N); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh` |

*chevron_right*

*filter_none*

**Output:**

aaaabbbb

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.