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.
Examples
01 · Example 1
data = "aabacfgh" return = 21
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.
02 · Example 2
data = "abcb" return = 10
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'.
03 · Example 3
data = "abez" return = 7
"abez" to "abaz"
Each of the substrings is worth 1 point. Hence, the final answer is 7.
More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
public int findMaximumScore(String data) {
// write your code here
}
data"aabacfgh"
expected21
sign in to submit