Tap here to switch tabs
Problem
Submissions

Problem: Find the Substring With Maximum Cost

med
30 min
Try to solve the Find the Substring With Maximum Cost problem.

Statement

You are given a string s, a string chars of distinct characters, and an integer array vals of the same length as chars.

The value of each character is defined as follows:

  • If the character appears in chars at index i, its value is vals[i].

  • Otherwise, its value is its 11 based position in the alphabet (that is, 'a' has a value of 11, 'b' has a value of 22, and so on up to 'z' with a value of 2626).

The cost of a substring is the sum of the values of its characters. The cost of an empty substring is defined as 00.

Return the maximum cost among all substrings of s.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s consists of lowercase English letters.

  • 1≤1 \leq chars.length ≤26\leq 26

  • chars consists of distinct lowercase English letters.

  • vals.length ==== chars.length

  • −1000≤-1000 \leq vals[i] ≤1000\leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find the Substring With Maximum Cost

med
30 min
Try to solve the Find the Substring With Maximum Cost problem.

Statement

You are given a string s, a string chars of distinct characters, and an integer array vals of the same length as chars.

The value of each character is defined as follows:

  • If the character appears in chars at index i, its value is vals[i].

  • Otherwise, its value is its 11 based position in the alphabet (that is, 'a' has a value of 11, 'b' has a value of 22, and so on up to 'z' with a value of 2626).

The cost of a substring is the sum of the values of its characters. The cost of an empty substring is defined as 00.

Return the maximum cost among all substrings of s.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s consists of lowercase English letters.

  • 1≤1 \leq chars.length ≤26\leq 26

  • chars consists of distinct lowercase English letters.

  • vals.length ==== chars.length

  • −1000≤-1000 \leq vals[i] ≤1000\leq 1000