#2606
Find the Substring With Maximum Cost
specialist · 750 · lc medium +31 · verified · 57.8% accepted · 394 likes · top 54%
Description
You are given a string s, a distinct-character string chars, and an integer array vals the same length as chars. The cost of a substring is the sum of its characters' values. A character's value is vals[i] if it appears at index i in chars; otherwise it equals its 1-indexed alphabetical position. The empty substring has cost 0. Return the maximum cost over all substrings of s.
Example 1:
Input: s = "adaa", chars = "d", vals = [-1000]
Output: 2
Explanation: The value of the characters "a" and "d" is 1 and -1000 respectively.
The substring with the maximum cost is "aa" and its cost is 1 + 1 = 2.
It can be proven that 2 is the maximum cost.
Example 2:
Input: s = "abc", chars = "abc", vals = [-1,-1,-1]
Output: 0
Explanation: The value of the characters "a", "b" and "c" is -1, -1, and -1 respectively.
The substring with the maximum cost is the empty substring "" and its cost is 0.
It can be proven that 0 is the maximum cost.
Code
1
2
3