Problem · Dynamic Programming

Maximize Score

Learn this problem
HardCventOA

Problem statement

You are given a string S and an integer array of size N. You need to maximize your score by performing the following operation multiple times until the string S is empty:

Choose a substring from S where all characters are identical.

Calculate the length of this substring, called size.

Add the sizeth element from the array arr to your total score.

Remove the selected substring from S.

Continue this process until the string S is empty, and return the maximum possible score.

Function

maximizeScore(S: String, arr: int[]) → int

Examples

Example 1

S = "abbbac"arr = [2, 5, 3, 1, 7]return = 14

abbbac -> abac -> aac -> c -> ""

5 + 2 + 5 + 2 = 14

Constraints

🦡
drafts saved locally
public int maximizeScore(String s, int[] arr) {
  // write your code here
}
S"abbbac"
arr[2, 5, 3, 1, 7]
expected14
checking account