![]() |
VOOZH | about |
Given a string W, and two arrays X[] and B[] of size N each where the ASCII value of character X[i] is redefined to B[i]. Find the substring with the maximum sum of the ASCII (American Standard Code for Information Interchange) value of the characters.
Note: Uppercase & lowercase both will be present in the string W.
Input: W = "abcde", N = 1, X[] = { 'c' }, B[] = { -1000 }
Output: de
Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum valueInput: W = "dbfbsdbf", N = 2, X[] = { 'b', 's' }, B[] = { -100, 45 }
Output: dbfbsdbf
Explanation: Substring "dbfbsdbf" has the maximum sum of ascii values.
Approach- This can be solved using the following idea:
Keep a map(ordered or unordered) where we can store the redefined ASCII values of characters that are provided in array X, Now use Kadane's algorithm to find the maximum substring sum with redefined ASCII values of characters.
Follow the steps mentioned below to solve the problem:
Below is the implementation of the above approach:
de
Time Complexity: O(|W|) where|W| is the length of the string
Auxiliary Space: O(N)