Given a string S, split the given string into as many substrings as possible such that each character from the given string appears in a single substring and print all these possible parts. The task is to print those substrings.
Input: S = “ababcbacadefegdehijhklij”
ababcbaca defegde hijhklij
a, b, c are only present in the first string.
d, e, f, g are only present in the second string.
h, i, j, k, l are only present in the third string.
Input: S = “acbbcc”
a are only present in the first string.
b, c are only present in the second string.
Approach: Follow the steps below to solve the problem:
- Store the last index of occurrence of all characters in the string.
- Since the string contains only lowercase letters, simply use an array of fixed size 26 to store the last indices of each character.
- Initialize an empty string ans = “” and iterate over the given string and follow the steps below:
- Add the current character to the string ans if the last position of the character is more than the current index and increase the length of the partition.
- If the last position of the current character is equal to the current index, then print the current string stored in ans and initialize ans to “” for storing the next partition of the string.
Below is the implementation of the above approach:
ababcbaca defegde hijhklij
Time Complexity: O(N)
Auxiliary Space: O(N)
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.
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Check if max occurring character of one string appears same no. of times in other
- Maximize partitions such that no two substrings have any common character
- Reduce the array such that each element appears at most 2 times
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Map every character of one string to another such that all occurrences are mapped to the same character
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Longest subsequence where every character appears at-least k times
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Count number of times each Edge appears in all possible paths of a given Tree
- Generate an Array such with elements maximized through swapping bits
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Partitions possible such that the minimum element divides all the other elements of the partition
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum K such that every substring of length atleast K contains a character c
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.