Maximum String Partition

25 May, 2021
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, and .
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 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++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // Return the count of stringint 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 codeint main(){     string s = "geeksforgeeks";     int ans = maxPartition(s);     cout << ans << "\n";     return 0;}
// Java implementation of the above approachclass GFG{// Return the count of stringstatic 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 codepublic static void main (String[] args){    String s = "geeksforgeeks";     int ans = maxPartition(s);     System.out.println(ans);}} // This code is contributed by ihritik
# Python3 implementation of the above approach # Return the count of stringdef 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 codes = "geeksforgeeks" ans = maxPartition(s) print(ans) # This code is contributed by Mohit Kumar
// C# implementation of the above approachusing System;class GFG{// Return the count of stringstatic 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 codepublic static void Main (){    string s = "geeksforgeeks";     int ans = maxPartition(s);     Console.WriteLine(ans);}} // This code is contributed by ihritik
<script> // Javascript implementation of the above approach // Return the count of stringfunction 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 codevar s = "geeksforgeeks";var ans = maxPartition(s);document.write( ans); </script>
11
 
Time Complexity: O(N), where N is the length of the string. 
mohit kumar 29
ihritik
noob2000
substring
Analysis
Greedy
Randomized
Strings
Strings
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Difference between Deterministic and Non-deterministic Algorithms
What is algorithm and why analysis of it is important?
Time-Space Trade-Off in Algorithms
Complexity analysis of various operations of Binary Min Heap
Dijkstra's shortest path algorithm | Greedy Algo-7
Program for array rotation
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Write a program to print all permutations of a given string
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2