Leetcode Problem 2606. Find the Substring With Maximum Cost

2606. Find the Substring With Maximum Cost

Leetcode Solutions

Using Kadane's Algorithm to Find Maximum Cost Substring

  1. Create a dictionary to map each character in chars to its corresponding value in vals.
  2. Initialize variables for the current sum (curr_sum) and maximum sum (max_sum) to 0.
  3. Iterate through each character in the string s. a. If the character is in the dictionary, use the mapped value; otherwise, use its 1-indexed alphabetical position as its value. b. Add the value to curr_sum. c. If curr_sum is greater than max_sum, update max_sum. d. If curr_sum becomes negative, reset it to 0.
  4. Return max_sum if it's positive; otherwise, return 0.
UML Thumbnail

Brute Force Approach to Find Maximum Cost Substring

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...