Generate a String of having N*N distinct non-palindromic Substrings
Last Updated :
11 May, 2021
Given an even integer N, the task is to construct a string such that the total number of distinct substrings of that string that are not a palindrome equals N2.
Examples:
Input: N = 2
Output: aabb
Explanation:
All the distinct non-palindromic substrings are ab, abb, aab and aabb.
Therefore, the count of non-palindromic substrings is 4 = 2 2
Input: N = 4
Output: cccczzzz
Explanation:
All distinct non-palindromic substrings of the string are cz, 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 the same, followed by N identical characters different from the first N characters, then the count of distinct non-palindromic substrings will be N2.
Proof:
N = 3
str = "aaabbb"
The string can be split into two substrings of N characters each: "aaa" and "bbb"
The first character 'a' from the first substring forms N distinct non-palindromic substrings "ab", "abb", "abbb" with the second substring.
Similarly, first two characters "aa" forms N distinct non-palindromic substrings "aab", "aabb", "aabbb".
Similarly, remaining N - 2 characters of the first substring each form N distinct non-palindromic substrings as well.
Therefore, the total number of distinct non-palindromic substrings is equal to N2.
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;
}
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
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
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
JavaScript
<script>
// JavaScript program for the above approach
// Function to construct a string
// having N*N non-palindromic substrings
function createString(N)
{
for (let i = 0; i < N; i++)
{
document.write('a');
}
for (let i = 0; i < N; i++)
{
document.write('b');
}
}
// Driver Code
let N = 4;
createString(N);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Generate an N-length string having longest palindromic substring of length K Given two integers N and K (K ? N), the task is to obtain a string of length N such that maximum length of a palindromic substring of this string is K. Examples: Input: N = 5, K = 3 Output: "abacd" Explanation: Palindromic substrings are "a", "b", "c", "d" and "aba". Therefore, the longest palindrom
4 min read
All distinct palindromic sub-strings of a given string Given a string str of lowercase ASCII characters. The task is to find all the distinct continuous palindromic sub-strings which are present in the string str. Examples: Input: str = "abaaa"Output: [ "a", "aa", "aaa", "aba", "b" ]Explanation: All 5 distinct continuous palindromic sub-strings are list
15+ min read
Count Palindromic Substrings in a Binary String Given a binary string S i.e. which consists only of 0's and 1's. Calculate the number of substrings of S which are palindromes. String S contains at most two 1's. Examples: Input: S = "011"Output: 4Explanation: "0", "1", "1" and "11" are the palindromic substrings. Input: S = "0" Output: 1Explanatio
7 min read
Count of Palindromic substrings in an Index range Given a string str of small alphabetic characters other than this we will be given many substrings of this string in form of index tuples. We need to find out the count of the palindromic substrings in given substring range. Examples: Input : String str = "xyaabax" Range1 = (3, 5) Range2 = (2, 3) Ou
11 min read
Count of non-palindromic strings of length M using given N characters Given two positive integers N and M, the task is to calculate the number of non-palindromic strings of length M using given N distinct characters. Note: Each distinct character can be used more than once. Examples: Input: N = 3, M = 2 Output: 6 Explanation: Since only 3 characters are given, those 3
8 min read