Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Find the Substring With Maximum Cost

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2606" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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 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. The value of the character is defined in the following way: If the character is not in the string chars, then its value is its corresponding position (1-indexed) in the alphabet. For example, the value of 'a' is 1, the value of 'b' is 2, and so on. The value of 'z' is 26. Otherwise, assuming i is the index where the character occurs in the string chars, then its value is vals[i]. Return the maximum cost among all substrings of the string s. Example 1: Input: s = "adaa", chars = "d", vals = [-1000] Output: 2 Explanation: 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 Explanation: 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

Explanation

Here's a breakdown of the solution and the Python code:

  • High-Level Approach:

    • Character Value Mapping: Create a dictionary (hash map) to store the values of characters. This dictionary will prioritize the custom values provided in chars and vals. If a character isn't in chars, its value will be its alphabetical position.
    • Kadane's Algorithm: Use Kadane's Algorithm to find the maximum sum of a contiguous subarray (substring) within the string s. Kadane's Algorithm efficiently tracks the maximum sum ending at each position.
  • Complexity:

    • Runtime: O(n), where n is the length of the string s.
    • Storage: O(1), as the size of the dictionary is bounded by the size of the alphabet (26).

Code

    def maximum_cost_substring(s: str, chars: str, vals: list[int]) -> int:
    """
    Calculates the maximum cost among all substrings of the given string s.

    Args:
        s: The input string.
        chars: A string of distinct characters.
        vals: An integer array of the same length as chars, representing the values of the characters in chars.

    Returns:
        The maximum cost among all substrings of the string s.
    """

    char_values = {}
    for i, char in enumerate(chars):
        char_values[char] = vals[i]

    max_so_far = 0
    current_max = 0

    for char in s:
        if char in char_values:
            value = char_values[char]
        else:
            value = ord(char) - ord('a') + 1

        current_max += value

        if current_max < 0:
            current_max = 0

        max_so_far = max(max_so_far, current_max)

    return max_so_far

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Find the Substring With Maximum Cost