LeetCode Problem

How to Solve Find the Substring With Maximum Cost

This problem involves finding the substring with the highest cost based on character values provided in a separate array. The trick is to use a hash lookup for character values and efficiently scan the string to compute possible substring costs. A careful approach is needed to avoid time complexity issues due to the large input sizes.

GhostInterview Help

Need help with Find the Substring With Maximum Cost without spending extra time grinding it?

GhostInterview can read Find the Substring With Maximum Cost from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2606Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem involves finding the substring with the highest cost based on character values provided in a separate array. The trick is to use a hash lookup for character values and efficiently scan the string to compute possible substring costs. A careful approach is needed to avoid time complexity issues due to the large input sizes.

Problem Statement

Given a string s, a string chars of distinct characters, and an integer array vals, you must calculate the maximum cost of any substring of s. The cost of each substring is determined by summing the values of its characters based on the vals array, where each character's value corresponds to its position in chars.

The task is to find the substring with the maximum total cost. The cost of an empty substring is 0. A solution should account for the fact that character values could be negative, meaning the highest cost substring could be one with only neutral or positive values, or potentially an empty string.

Examples

Example 1

Input: s = "adaa", chars = "d", vals = [-1000]

Output: 2

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

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.

Constraints

  • 1 <= s.length <= 105
  • s consist of lowercase English letters.
  • 1 <= chars.length <= 26
  • chars consist of distinct lowercase English letters.
  • vals.length == chars.length
  • -1000 <= vals[i] <= 1000

Solution Approach

Character Cost Mapping

Create a hash map or dictionary where each character in chars maps to its corresponding value in vals. This allows quick lookup of character values when iterating through the string s.

Array Scanning with Dynamic Programming

As you scan through string s, dynamically calculate the total cost of every possible substring. Update the maximum cost as you encounter higher totals. Efficiently use an array to store partial costs during the scan.

Handling Negative Values

Since values can be negative, carefully track when to reset or skip substrings with negative costs. An empty substring might be optimal if all character values are negative.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the final approach. A straightforward scan through the string with hash lookups will result in O(n) time complexity, where n is the length of s. Space complexity will also depend on the number of unique characters, which is O(m) where m is the size of chars (at most 26).

What Interviewers Usually Probe

  • Look for the ability to efficiently use hash lookups during the array scan.
  • Evaluate if the candidate can handle negative values in substring calculations.
  • Assess if the candidate can optimize space complexity with the use of a mapping or array.

Common Pitfalls or Variants

Common pitfalls

  • Misunderstanding how to map character values to substrings efficiently.
  • Failing to handle negative character values correctly, potentially missing the optimal empty substring.
  • Overcomplicating the approach by trying to check every possible substring without utilizing dynamic programming or array scanning.

Follow-up variants

  • What if the input string s contains only one character?
  • How would the solution change if the characters in chars are not distinct?
  • What if there are multiple substrings with the same maximum cost?

How GhostInterview Helps

  • GhostInterview provides step-by-step guidance to build the optimal approach for this problem using hash lookups and array scanning.
  • It helps identify common pitfalls like handling negative values and optimizing for both time and space complexity.
  • The solver also assists in structuring the dynamic programming solution efficiently, ensuring the candidate doesn't overlook simple optimizations.

Topic Pages

FAQ

What is the primary technique to solve the Find the Substring With Maximum Cost problem?

The primary technique involves using array scanning along with hash lookup for character values, ensuring efficient substring cost calculation.

How do I handle negative character values in this problem?

You should carefully track the total cost as you iterate through the string and reset or skip negative cost substrings, potentially choosing the empty substring.

Can this problem be solved in linear time?

Yes, by using an array scanning technique with hash lookups, you can solve this problem in O(n) time complexity.

What is the space complexity of this problem?

The space complexity is O(m), where m is the number of distinct characters in chars, which is at most 26.

Is dynamic programming necessary for solving this problem?

Dynamic programming is useful for optimizing substring cost calculations, though simple array scanning with partial sums may also suffice.

GhostInterview Solver

Need direct help with Find the Substring With Maximum Cost instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find the Substring With Maximum Cost from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.