Maximum Crayon Removal Score
Problem statement
You are given a string colors representing crayons arranged in a row. Equal letters represent equal colors.
In one operation, choose a contiguous group of x crayons of the same color, remove it, and earn x * x points. The remaining crayons close the gap and may form new contiguous groups.
Remove every crayon and return the maximum total score.
Function
maximumCrayonScore(colors: String) → intExamples
Example 1
colors = "abcccbaba"return = 23Remove ccc for 9, then one b for 1, then the merged bbb for 9, and finally aa for 4.
Example 2
colors = "aba"return = 5Remove b for 1, then remove the merged aa for 4.
Example 3
colors = "aaa"return = 9Remove all three crayons in one operation.
Example 4
colors = "ab"return = 2The two different crayons must be removed separately for one point each.
Constraints
1 <= colors.length <= 60.colorscontains lowercase English letters.