FastPrepMaximum Crayon Removal Score

Maximum Crayon Removal Score

Wells Fargo logoWells Fargo● HardFULLTIMEOA
Learn

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) → int

Examples

Example 1

colors = "abcccbaba"return = 23

Remove ccc for 9, then one b for 1, then the merged bbb for 9, and finally aa for 4.

Example 2

colors = "aba"return = 5

Remove b for 1, then remove the merged aa for 4.

Example 3

colors = "aaa"return = 9

Remove all three crayons in one operation.

Example 4

colors = "ab"return = 2

The two different crayons must be removed separately for one point each.

Constraints

  • 1 <= colors.length <= 60.
  • colors contains lowercase English letters.

More Wells Fargo problems

See Wells Fargo hiring insights
public int maximumCrayonScore(String colors) {
  // write your code here
}
colors"abcccbaba"
expected23
Checking account…