The method of splitting strings so that each partition starts with a different character

Given a string s. Let K be the maximum number of partitions possible for a given string, each beginning with a different character. The task is to find a way to split the strings into multiple K partitions (non-empty) so that each partition starts with a different character.

Example:

Input : s = "abb"
Output : 2
"abb" can be maximum split into 2 
partitions {a, bb} with distinct 
starting character, so k = 2. And, number of ways to split "abb" into 
2 partition with distinct starting 
character is 2 that are {a, bb} and
{ab, b}.

Input : s = "acbbcc"
Output : 6

First, we need to find the value of k. It is observed that k will be equal to the number of different characters in the string since only the maximum number of partitions can make each partition have a different starting element.

Now, the number of ways to find a way to divide a string into k parts in each partition starts with different characters. First of all, the first partition will always fix the first character of the string, no matter how long it is. Now, we need to deal with all but the first character.

Let’s take an example, let’s say s=”acbbcc”, we have already discussed the first character “a” above. Now, to deal with “b” and “c”, observe that “b” appears in two positions in the string and “c” appears in three positions. If we choose any of the two locations for “b” and any of the three locations for “c”, we can partition the string on those locations. Note that the number of parts will be equal to 3 (equal to the number of different characters in s, i.e. k).

So in a nutshell, let’s count the number of times the character i appears in s. So our answer would be the product of the count I all I make me appear in the string, and I am not equal to the first character of the string.

Here’s how this method works:

Table of Contents

C ++

// CPP Program to find number of way 
// to split string such that each partition
// starts with distinct character with 
// maximum number of partitions.
#include <bits/stdc++.h>
  
using namespace std;
  
// Returns the number of we can split
// the string
int countWays(string s)
{
     int count[26] = { 0 };
  
     // Finding the frequency of each
     // character.
     for ( char x : s)
         count[x - 'a' ]++;
  
     // making frequency of first character
     // of string equal to 1.
     count展开 - 'a' ] = 1;
  
     // Finding the product of frequency 
     // of occurrence of each character.
     int ans = 1;
     for ( int i = 0; i < 26; ++i)
         if (count[i] != 0)
             ans *= count[i];
  
     return ans;
}
  
// Driven Program
int main()
{
     string s = "acbbcc" ;
     cout << countWays(s) << endl;
     return 0;
}

Java

// Java Program to find number 
// of way to split string such 
// that each partition starts 
// with distinct character with 
// maximum number of partitions.
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
  
// Returns the number of we 
// can split the string
static int countWays(String s)
{
     int count[] = new int [ 26 ];
  
     // Finding the frequency of
     // each character.
     for ( int i = 0 ; i < s.length(); i++)
         count展开++;
  
     // making frequency of first 
     // character of string equal to 1.
     count展开 = 1 ;
  
     // Finding the product of frequency 
     // of occurrence of each character.
     int ans = 1 ;
     for ( int i = 0 ; i < 26 ; ++i)
         if (count[i] != 0 )
             ans *= count[i];
  
     return ans;
}
  
// Driver Code
public static void main(String ags[])
{
     String s = "acbbcc" ;
     System.out.println(countWays(s));
}
}
  
// This code is contributed
// by Subhadeep

Python3

# Python3 Program to find number of way
# to split string such that each partition
# starts with distinct character with
# maximum number of partitions.
  
# Returns the number of we can split
# the string
def countWays(s):
     count = [ 0 ] * 26 ;
  
     # Finding the frequency of each
     # character.
     for x in s:
         count[ ord (x) - 
               ord ( 'a' )] = (count[ ord (x) - 
                                  ord ( 'a' )]) + 1 ;
  
     # making frequency of first character
     # of string equal to 1.
     count[ ord (s[ 0 ]) - ord ( 'a' )] = 1 ;
  
     # Finding the product of frequency
     # of occurrence of each character.
     ans = 1 ;
     for i in range ( 26 ):
         if (count[i] ! = 0 ):
             ans * = count[i];
  
     return ans;
  
# Driver Code
if __name__ = = '__main__' :
     s = "acbbcc" ;
     print (countWays(s));
  
# This code is contributed by Rajput-Ji

C#

// C# Program to find number 
// of way to split string such 
// that each partition starts 
// with distinct character with 
// maximum number of partitions.
  
using System;
   
class GFG
{
   
// Returns the number of we 
// can split the string
static int countWays( string s)
{
     int [] count = new int [26];
   
     // Finding the frequency of
     // each character.
     for ( int i = 0; i < s.Length; i++)
         count展开 - 'a' ]++;
   
     // making frequency of first 
     // character of string equal to 1.
     count展开 - 'a' ] = 1;
   
     // Finding the product of frequency 
     // of occurrence of each character.
     int ans = 1;
     for ( int i = 0; i < 26; ++i)
         if (count[i] != 0)
             ans *= count[i];
   
     return ans;
}
   
// Driver Code
public static void Main()
{
     string s = "acbbcc" ;
     Console.WriteLine(countWays(s));
}
}

The output is as follows:

6