I have a string composed of many substrings,My aim is to group substrings such that for each grouped substring, the number of unique elements in the same is not more than a cut off k.

lets say i have set the cut off as:4

and lets say my formula is a=b*c/(b+c) + b*c*c*e/(a+b+c+d) + f/g

substrings:b*c/(b+c) , b*c*c*e/(a+b+c+d) , f/g

in this case the optimal way to group based on the cut off of 4 is:

a=z+y ( number of unique elements : 3)
z=b*c/(b+c)+f/g ( number of unique elements : 5)
y=b*e/(a+b+d) ( number of unique elements : 5)

i need to know whats the best way to solve the problem.

implemented until now : Break up the subformulas and put them on a stack,write a function to calculate the unique element count of anystring.
Im stuck in what would be the best algorithm to implement the above mentioned difficulty, please help!