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.
- Partition an array such into maximum increasing segments
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Number of ways to partition a string into two balanced subsequences
- Ways to split string such that each partition starts with distinct character
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Partition the string in two parts such that both parts have at least k different characters
- Lexicographical Maximum substring of string
- String with maximum number of unique characters
- Maximum difference of zeros and ones in binary string
- Calculate maximum value using '+' or '*' sign between two numbers in a string
- Maximum number of characters between any two same character in a string
- Maximum number of removals of given subsequence from a string
- Maximum contiguous 1 possible in a binary string after k rotations
- Count maximum-length palindromes 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 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.