Given string str of size N, the task is to print the number of substrings formed after maximum possible partitions such that no two substrings have a common character.
Input : str = “ababcbacadefegdehijhklij”
Output : 3
Partitioning at the index 8 and at 15 produces three substrings “ababcbaca”, “defegde” and “hijhklij” such that none of them have a common character. So, the maximum partitions is 3.
Input: str = “aaa”
Since, the string consists of a single character, no partition can be performed.
- Find the last index of every unique character from the end of the string and store it in a map.
- Traverse the array from the index 0 up to the last index and make a separate variable to store the ending index of partition.
- Traverse for every variable in the string and check if the end of the partition, denoted by the index of str[i] stored in the map, is greater than the previous end. If so, update it.
- Check if the current variable exceeds the end of the partition, it means the first partition is found. Update the end of the partition to max(k, mp[str[i]]).
- Traverse the whole string and use a similar process to find the next partition and so on.
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.
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Check if there is any common character in two given strings
- Minimize length of Substrings containing at least one common Character
- Find the count of M character words which have at least one character repeated
- Count substrings that starts with character X and ends with character Y
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest Common Prefix using Character by Character Matching
- 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
- Check if two strings have a common substring
- Find if two given Quadratic equations have common roots or not
- Rearrange the given string such that all prime multiple indexes have same character
- Maximize profit when divisibility by two numbers have associated profits
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Check whether all the substrings have number of vowels atleast as that of consonants
- Count divisors of n that have at-least one digit common with n
- Find a string such that every character is lexicographically greater than its immediate next character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
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.