Given a string str of length L and an integer N, the task is to form a total of (L / N) contiguous subsegments of the string which contain distinct subsequent characters.
Note: that the integer N will be a factor of the length of the string i.e L.
Input: str = “geeksforgeeksgfg”, N = 4
The length of “geeksforgeeksgfg” is 16, therefore there will be 4 subsegments.
The first subsegment will contain the characters g, e, e and k but alphabet ‘e’ is repeating,
So we discard one ‘e’. Therefore the final subsegment will be “gek”.
Similarly, the other three subsegments will be “sfor”, “gek” and “sfg”.
Each subsegment should have subsequent distinct characters.
Input: str = “aabdekfgf”, N = 3
Approach: An array is created everytime the iteration is started over the new subsegment. Those elements are stored in a sequence in that array. If any element already exists in the array then it is not pushed again in the array.
Below is the implementation of the above approach:
gek sfor gek sgf
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
- Find the String having each substring with exactly K distinct characters
- String with k distinct characters and no same characters adjacent
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Distinct state codes that appear in a string as contiguous sub-strings
- Count of substrings having all distinct characters
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Count of distinct permutations of length N having no similar adjacent characters
- Replace minimal number of characters to make all characters pair wise distinct
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Largest sum contiguous subarray having only non-negative elements
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Largest Sum Contiguous Subarray having unique elements
- Generate a String of having N*N distinct non-palindromic Substrings
- Print all distinct characters of a string in order (3 Methods)
- Length of the smallest sub-string consisting of maximum distinct characters
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Check whether count of distinct characters in a string is Prime or not
- Convert given string so that it holds only distinct characters
- Find the number of strings formed using distinct characters of a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.