Given a string. The task is to find the maximum number P, such that given string can be partitioned into P contiguous substrings such that any two adjacent substring must be different. More formally and .
Input: str = “aabccd”
We can divide the given string into four string, like “a”, “ab”, “c”, “cd”. We can not divide
it more than four parts, if we do then the condition will not
Input: str = “aaaa”
- Here we only have to focus on the value of P, not upon finding those P substrings.
- We will solve it greedily, we always check the current string that we have with the previous string that has been taken already.
- If we found that both of them are same then we will go forward otherwise create a partition here and change the previous track of the string to current string, means we will treat this current string as the previous string for future comparison.
Below is the implementation of above Approach:
Time Complexity: O(N), where N is the length of the string.
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.
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Partition an array such into maximum increasing segments
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Ways to split string such that each partition starts with distinct character
- Number of ways to partition a string into two balanced subsequences
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Partition the string in two parts such that both parts have at least k different characters
- Count ways to partition a string such that both parts have equal distinct characters
- Partition a number into two divisible parts
- Partition first N natural number into two sets such that their sum is not coprime
- Partition the digits of an integer such that it satisfies a given condition
- Partitions possible such that the minimum element divides all the other elements of the partition
- Set partition is NP complete
- Maximum length Subsequence with alternating sign and maximum Sum
- Return maximum occurring character in an input string
- Maximum length prefix of one string that occurs as subsequence in another
- Maximum consecutive repeating character in string
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
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.