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
- Find distinct characters in distinct substrings of a string
- Distinct state codes that appear in a string as contiguous sub-strings
- String with k distinct characters and no same characters adjacent
- Convert given string so that it holds only distinct characters
- Print all distinct characters of a string in order (3 Methods)
- Check whether count of distinct characters in a string is Prime or not
- Length of the smallest sub-string consisting of maximum distinct characters
- Find the number of strings formed using distinct characters of a given string
- Replace minimal number of characters to make all characters pair wise distinct
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count distinct substrings that contain some characters at most k times
- Minimum length substring with exactly K distinct characters
- Count number of substrings with exactly k distinct characters
- Min flips of continuous characters to make all characters same in a 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 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.