# Maximum String Partition

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 .

**Examples:**

Input:str = “aabccd”

Output:4

Explanation:

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

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 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:

## 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 [tabby title="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 ` |

*chevron_right*

*filter_none*

## 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 [tabbyending]

**Output:**

11

**Time Complexity:** O(N), where N is the length of the string.

## Recommended Posts:

- 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
- Lexicographical Maximum substring of string
- Maximum consecutive repeating character in string
- Count maximum-length palindromes 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 difference of zeros and ones in binary string
- Maximum even length sub-string that is permutation of a palindrome
- Maximum occurring character in an input string | Set-2
- Calculate maximum value using '+' or '*' sign between two numbers 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 contribute@geeksforgeeks.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.