Ways to split string such that each partition starts with distinct character
Given a string s. Let k be the maximum number of partitions possible of the given string with each partition starts with distinct character. The task is to find the number of ways string s can be split into k partition (non-empty) such that each partition start with distinct character.
Examples:
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. Observe that k will be equal to number of distinct characters in the string because only that number of partitions can be maximum such that each partition have distinct starting element.
Now, to find the number of ways to split string into k parts with each partition starts with distinct character. First observe that first partition will always have first character of string fixed, no matter how much it is long. Now, we need to deal with all other character except the first one.
Let’s take an example, say s = “acbbcc”, we have discussed about first character ‘a’ above. Now, to deal with ‘b’ and ‘c’, observe ‘b’ occurs at two positions in the string whereas ‘c’ at three. If we select any positions out of two for ‘b’ and any one position out of three for ‘c’, then, we can partition the string at those positions. Note that number of parts will be equal to 3 (equals to number of distinct characters in s i.e k).
So generalizing the observation, let counti be the number of occurrences of character i in s. So our answer will be product of counti of all i’s such that i occurs in the string and i is not equal to first character of the string.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWays(string s)
{
int count[26] = { 0 };
for ( char x : s)
count[x - 'a' ]++;
count[s[0] - 'a' ] = 1;
int ans = 1;
for ( int i = 0; i < 26; ++i)
if (count[i] != 0)
ans *= count[i];
return ans;
}
int main()
{
string s = "acbbcc" ;
cout << countWays(s) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int countWays(String s)
{
int count[] = new int [ 26 ];
for ( int i = 0 ; i < s.length(); i++)
count[s.charAt(i) - 'a' ]++;
count[s.charAt( 0 ) - 'a' ] = 1 ;
int ans = 1 ;
for ( int i = 0 ; i < 26 ; ++i)
if (count[i] != 0 )
ans *= count[i];
return ans;
}
public static void main(String ags[])
{
String s = "acbbcc" ;
System.out.println(countWays(s));
}
}
|
Python3
def countWays(s):
count = [ 0 ] * 26 ;
for x in s:
count[ ord (x) -
ord ( 'a' )] = (count[ ord (x) -
ord ( 'a' )]) + 1 ;
count[ ord (s[ 0 ]) - ord ( 'a' )] = 1 ;
ans = 1 ;
for i in range ( 26 ):
if (count[i] ! = 0 ):
ans * = count[i];
return ans;
if __name__ = = '__main__' :
s = "acbbcc" ;
print (countWays(s));
|
C#
using System;
class GFG
{
static int countWays( string s)
{
int [] count = new int [26];
for ( int i = 0; i < s.Length; i++)
count[s[i] - 'a' ]++;
count[s[0] - 'a' ] = 1;
int ans = 1;
for ( int i = 0; i < 26; ++i)
if (count[i] != 0)
ans *= count[i];
return ans;
}
public static void Main()
{
string s = "acbbcc" ;
Console.WriteLine(countWays(s));
}
}
|
Javascript
<script>
function countWays(s)
{
let count = new Array(26);
count.fill(0);
for (let i = 0; i < s.length; i++)
count[s[i].charCodeAt() -
'a' .charCodeAt()]++;
count[s[0].charCodeAt() -
'a' .charCodeAt()] = 1;
let ans = 1;
for (let i = 0; i < 26; ++i)
if (count[i] != 0)
ans *= count[i];
return ans;
}
let s = "acbbcc" ;
document.write(countWays(s));
</script>
|
Time Complexity: O(n+26), where n is the size of the given string
Auxiliary Space: O(26), size used to make the count array
Last Updated :
10 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...