FastPrepFastPrep
Problem Brief

Find Maximum Score

NEW GRADOA
See Amazon online assessment and hiring insights

Data analysts at Amazon are analyzing a string, data. Each of its characters is worth 1 point. Any substring in which all characters are adjacent to characters within one position in the English alphabet is also worth 1 point. For example, "aa", "ab", and "ba" are each worth 1 point.

To improve the score, at most one character can be replaced with any other character from the lowercase English alphabet.

Given a string, data, find the maximum score that can be achieved.

1Example 1

Input
data = "aabacfgh"
Output
21
Explanation
This test case was added on 06-05-2025. It was shared by a user friend in the Solution section, and I happen to come across it. I don’t know who you are, but thank you so much for sharing something so helpful! As alaways, relevant source ss is included in the Problem Source section :) The initial score is 17, one point for each of the 8 letters, and the following 9 substring contributing one point each: 'aa', 'ab', 'ba', 'aab', 'aba', 'aaba', 'fg', 'gh', 'fgh'. In the optimal scenario, change the character at the 5th position Le., data[4] = 'c'to 'b'. The string data becomes "aababfgh" and the score is thus calculated as: 1 point for each of the 8 letters of the string. The following substrings worth 1 point each: data[0:1] = "aa", data[0:2] = "aab", data[0:3] = "aaba", data[0:4] = "aabab". Total count = 4. data[1:2] = "ab", data[1:3] = "aba", data[1:4] = "abab". Total Count = 3. data[2:3] = "ba", data[2:4] = "bab". Total count = 2. data[3:4] = "ab". Total count = 1. data[5:6] = "fg", data[5:7] = "fgh", Total count = 2. data[6:7] = "gh". Total count = 1. Therefore, the total points is 8 +4+3+2+1+2+1=21. Hence, the answer is 21.

2Example 2

Input
data = "abcb"
Output
10
Explanation
This test case was added on 06-05-2025. It was shared by a user friend in the Solution section, and I happen to come across it. I don’t know who you are, but thank you so much for sharing something so helpful! As alaways, relevant source ss is included in the Problem Source section :) The string is already in one of its most efficient forms, so no changes can increase Its score. A point is scored for 'a', 'b', 't,b Show Applications abc', 'bcb', and 'abcb'.

3Example 3

Input
data = "abez"
Output
7
Explanation
"abez" to "abaz" Each of the substrings is worth 1 point. Hence, the final answer is 7.
public int findMaximumScore(String data) {
  // write your code here
}
Input

data

"aabacfgh"

Output

21

Sign in to submit your solution.