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 N2.
Input: N = 2
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
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.
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 N2.
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.
Similiarly 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:
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.
- Find distinct characters in distinct substrings of a string
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Count of substrings having all distinct characters
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Minimize splits to generate monotonous Substrings from given String
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Minimum changes to a string to make all substrings distinct
- Count distinct substrings of a string using Rabin Karp algorithm
- Count of Distinct Substrings occurring consecutively in a given String
- Count ways to split a Binary String into three substrings having equal count of zeros
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Iterative program to generate distinct Permutations of a String
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Contiguous subsegments of a string having distinct subsequent characters
- Find the String having each substring with exactly K distinct characters
- Count number of substrings with exactly k distinct characters
- Count distinct substrings that contain some characters at most k times
- Count number of distinct substrings of a given length
- Count of substrings of length K with exactly K distinct characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.