Given a string. The task is to find the maximum number P, such that a given string can be partitioned into P contiguous substrings such that any two adjacent substrings must be different. More formally, S = S_{1}S_{2}....S_{P} and S_{i} \ne S_{i + 1}(0 \leq i \leq P - 1) .
Examples:
Input: str = "aabccd"
Output: 4
Explanation:
We can divide the given string into four strings, like "a", "ab", "c", "cd". We can not divide
it are more than four parts. If we do, then the condition S_{i} \ne S_{i + 1}(0 \leq i \leq P - 1) will not
satisfy
Input: str = "aaaa"
Output: 3
Approach:
- 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 find that both of them are the same, then we will go forward, otherwise, create a partition here, and change the previous track of the string to the current string, which means we will treat this current string as the previous string for future comparison.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// Return the count of string
int maxPartition(string s)
{
// P will store the answer
int n = s.length(), P = 0;
// Current will store current string
// Previous will store the previous
// string that has been taken already
string current = "", previous = "";
for (int i = 0; i < n; i++) {
// Add a character to current string
current += s[i];
if (current != previous) {
// Here we will create a partition and
// update the previous string with
// current string
previous = current;
// Now we will clear the current string
current.clear();
// Increment the count of partition.
P++;
}
}
return P;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
int ans = maxPartition(s);
cout << ans << "\n";
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Return the count of string
static int maxPartition(String s)
{
// P will store the answer
int n = s.length(), P = 0;
// Current will store current string
// Previous will store the previous
// string that has been taken already
String current = "", previous = "";
for (int i = 0; i < n; i++)
{
// Add a character to current string
current += s.charAt(i);
if (!current.equals(previous))
{
// Here we will create a partition and
// update the previous string with
// current string
previous = current;
// Now we will clear the current string
current = "";
// Increment the count of partition.
P++;
}
}
return P;
}
// Driver code
public static void main (String[] args)
{
String s = "geeksforgeeks";
int ans = maxPartition(s);
System.out.println(ans);
}
}
// This code is contributed by ihritik
Python3
# Python3 implementation of the above approach
# Return the count of string
def maxPartition(s):
# P will store the answer
n = len(s)
P = 0
# Current will store current string
# Previous will store the previous
# that has been taken already
current = ""
previous = ""
for i in range(n):
# Add a character to current string
current += s[i]
if (current != previous):
# Here we will create a partition and
# update the previous with
# current string
previous = current
# Now we will clear the current string
current = ""
# Increment the count of partition.
P += 1
return P
# Driver code
s = "geeksforgeeks"
ans = maxPartition(s)
print(ans)
# This code is contributed by Mohit Kumar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Return the count of string
static int maxPartition(string s)
{
// P will store the answer
int n = s.Length, P = 0;
// Current will store current string
// Previous will store the previous
// string that has been taken already
string current = "", previous = "";
for (int i = 0; i < n; i++)
{
// Add a character to current string
current += s[i];
if (!current.Equals(previous))
{
// Here we will create a partition and
// update the previous string with
// current string
previous = current;
// Now we will clear the current string
current = "";
// Increment the count of partition.
P++;
}
}
return P;
}
// Driver code
public static void Main ()
{
string s = "geeksforgeeks";
int ans = maxPartition(s);
Console.WriteLine(ans);
}
}
// This code is contributed by ihritik
JavaScript
<script>
// Javascript implementation of the above approach
// Return the count of string
function maxPartition(s)
{
// P will store the answer
var n = s.length, P = 0;
// Current will store current string
// Previous will store the previous
// string that has been taken already
var current = "", previous = "";
for (var i = 0; i < n; i++) {
// Add a character to current string
current += s[i];
if (current != previous) {
// Here we will create a partition and
// update the previous string with
// current string
previous = current;
// Now we will clear the current string
current = "";
// Increment the count of partition.
P++;
}
}
return P;
}
// Driver code
var s = "geeksforgeeks";
var ans = maxPartition(s);
document.write( ans);
</script>
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the given string.
Similar Reads
Maximum Balanced String Partitions Given a balanced string str of size N with an equal number of L and R, the task is to find a maximum number X, such that a given string can be partitioned into X balanced substring. A string called to be balanced if the number of 'L's in the string equals the number of 'R's. Examples: Input : str =
7 min read
Sliding Window Maximum : Set 2 Set 1: Sliding Window Maximum (Maximum of all subarrays of size k).Given an array arr of size N and an integer K, the task is to find the maximum for each and every contiguous subarray of size K. Examples: Input: arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}, K = 3 Output: 3 3 4 5 5 5 6 All contiguous subarra
14 min read
Maximum average sum partition of an array Given an array, we partition a row of numbers A into at most K adjacent (non-empty) groups, then the score is the sum of the average of each group. What is the maximum score that can be scored? Examples: Input : A = { 9, 1, 2, 3, 9 } K = 3 Output : 20 Explanation : We can partition A into [9], [1, 2
10 min read
Maximize String Partitions with No Common Characters Given a string s consisting of lowercase English letters, partition s into the maximum number of substrings such that no two substrings share a common character. Return the total number of such substrings.Examples:Input: s = "acbbcc" Output: 2Explanation: "a" and "cbbcc" are two substrings that do n
13 min read
The Painter's Partition Problem Given an array arr[] and k, where the array represents the boards and each element of the given array represents the length of each board. k numbers of painters are available to paint these boards. Consider that each unit of a board takes 1 unit of time to paint. The task is to find the minimum time
15+ min read
Minimum partitions of String such that each part is at most K Given a string S of size N consisting of numerical digits 1-9 and a positive integer K, the task is to minimize the partitions of the string such that each part is at most K. If itâs impossible to partition the string print -1. Examples: Input: S = "3456", K = 45Output: 2Explanation: One possible wa
8 min read