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 count_{i} be the number of occurrences of character i in s. So our answer will be product of count_{i} 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++

`// 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[s[0] - ` `'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; ` `} ` |

*chevron_right*

*filter_none*

## 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[s.charAt(i) - ` `'a'` `]++; ` ` ` ` ` `// making frequency of first ` ` ` `// character of string equal to 1. ` ` ` `count[s.charAt(` `0` `) - ` `'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 ags[]) ` `{ ` ` ` `String s = ` `"acbbcc"` `; ` ` ` `System.out.println(countWays(s)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Subhadeep ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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[s[i] - ` `'a'` `]++; ` ` ` ` ` `// making frequency of first ` ` ` `// character of string equal to 1. ` ` ` `count[s[0] - ` `'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)); ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count ways to partition a string such that both parts have equal distinct characters
- Count substrings that starts with character X and ends with character Y
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Split the string into minimum parts such that each part is in the another string
- Number of strings which starts and ends with same character after rotations
- Largest index for each distinct character in given string with frequency K
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Find if a string starts and ends with another given string
- Number of ways to partition a string into two balanced subsequences
- Maximum length palindromic substring such that it starts and ends with given char
- Number of ways to split a binary number such that every part is divisible by 2
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find a string such that every character is lexicographically greater than its immediate next character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Partition the string in two parts such that both parts have at least k different characters
- Check if given string can be split into four distinct strings

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.