Maximum String Partition | C ++ implementation of the above approach ; Return the count of string ; P will store the answer ; Current will store current string Previous will store the previous string that has been taken already ; Add a character to current string ; Here we will create a partition and update the previous string with current string ; Now we will clear the current string ; Increment the count of partition . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPartition ( string s ) { int n = s . length ( ) , P = 0 ; string current = " " , previous = " " ; for ( int i = 0 ; i < n ; i ++ ) { current += s [ i ] ; if ( current != previous ) { previous = current ; current . clear ( ) ; P ++ ; } } return P ; } int main ( ) { string s = " geeksforgeeks " ; int ans = maxPartition ( s ) ; cout << ans << " STRNEWLINE " ; return 0 ; }