java.lang.Object
g2601_2700.s2606_find_the_substring_with_maximum_cost.Solution

public class Solution extends Object
2606 - Find the Substring With Maximum Cost.<p>Medium</p> <p>You are given a string <code>s</code>, a string <code>chars</code> of <strong>distinct</strong> characters and an integer array <code>vals</code> of the same length as <code>chars</code>.</p> <p>The <strong>cost of the substring</strong> is the sum of the values of each character in the substring. The cost of an empty string is considered <code>0</code>.</p> <p>The <strong>value of the character</strong> is defined in the following way:</p> <ul> <li>If the character is not in the string <code>chars</code>, then its value is its corresponding position <strong>(1-indexed)</strong> in the alphabet. <ul> <li>For example, the value of <code>'a'</code> is <code>1</code>, the value of <code>'b'</code> is <code>2</code>, and so on. The value of <code>'z'</code> is <code>26</code>.</li> </ul> </li> <li>Otherwise, assuming <code>i</code> is the index where the character occurs in the string <code>chars</code>, then its value is <code>vals[i]</code>.</li> </ul> <p>Return <em>the maximum cost among all substrings of the string</em> <code>s</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;adaa&rdquo;, chars = &ldquo;d&rdquo;, vals = [-1000]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The value of the characters &ldquo;a&rdquo; and &ldquo;d&rdquo; is 1 and -1000 respectively. The substring with the maximum cost is &ldquo;aa&rdquo; and its cost is 1 + 1 = 2. It can be proven that 2 is the maximum cost.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;abc&rdquo;, chars = &ldquo;abc&rdquo;, vals = [-1,-1,-1]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The value of the characters &ldquo;a&rdquo;, &ldquo;b&rdquo; and &ldquo;c&rdquo; is -1, -1, and -1 respectively. The substring with the maximum cost is the empty substring &quot;&quot; and its cost is 0. It can be proven that 0 is the maximum cost.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10<sup>5</sup></code></li> <li><code>s</code> consist of lowercase English letters.</li> <li><code>1 <= chars.length <= 26</code></li> <li><code>chars</code> consist of <strong>distinct</strong> lowercase English letters.</li> <li><code>vals.length == chars.length</code></li> <li><code>-1000 <= vals[i] <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumCostSubstring

      public int maximumCostSubstring(String s, String chars, int[] vals)