2606. Find the Substring With Maximum Cost Medium
1/**
2 * [2606] Find the Substring With Maximum Cost
3 *
4 * You are given a string s, a string chars of distinct characters and an integer array vals of the same length as chars.
5 * The cost of the substring is the sum of the values of each character in the substring. The cost of an empty string is considered 0.
6 * The value of the character is defined in the following way:
7 *
8 * If the character is not in the string chars, then its value is its corresponding position (1-indexed) in the alphabet.
9 *
10 * For example, the value of 'a' is 1, the value of 'b' is 2, and so on. The value of 'z' is 26.
11 *
12 *
13 * Otherwise, assuming i is the index where the character occurs in the string chars, then its value is vals[i].
14 *
15 * Return the maximum cost among all substrings of the string s.
16 *
17 * <strong class="example">Example 1:
18 *
19 * Input: s = "adaa", chars = "d", vals = [-1000]
20 * Output: 2
21 * Explanation: The value of the characters "a" and "d" is 1 and -1000 respectively.
22 * The substring with the maximum cost is "aa" and its cost is 1 + 1 = 2.
23 * It can be proven that 2 is the maximum cost.
24 *
25 * <strong class="example">Example 2:
26 *
27 * Input: s = "abc", chars = "abc", vals = [-1,-1,-1]
28 * Output: 0
29 * Explanation: The value of the characters "a", "b" and "c" is -1, -1, and -1 respectively.
30 * The substring with the maximum cost is the empty substring "" and its cost is 0.
31 * It can be proven that 0 is the maximum cost.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= s.length <= 10^5
37 * s consist of lowercase English letters.
38 * 1 <= chars.length <= 26
39 * chars consist of distinct lowercase English letters.
40 * vals.length == chars.length
41 * -1000 <= vals[i] <= 1000
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/find-the-substring-with-maximum-cost/
47// discuss: https://leetcode.com/problems/find-the-substring-with-maximum-cost/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn maximum_cost_substring(s: String, chars: String, vals: Vec<i32>) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_2606() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.