Substring with maximum ASCII sum when some ASCII values are redefined
Last Updated :
08 Feb, 2024
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 value
Input: 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:
- Take two empty strings ans=”” and res =””.
- If the size of the given string is 1, return the original string as it will be the only maximum string.
- Take an unordered map and store redefined ASCII values in that map.
- Traverse the string and increase the sum every time by ASCII value of the character(predefined or redefined) and store the string in ‘ans‘ till the sum is greater than zero.
- If the sum becomes negative, clear the string ‘ans‘ and put sum = 0 again.
- Every time check whether the sum is greater than the maximum or not.
- If the sum is greater than the maximum, update maximum = sum and res = ans.
- Return res as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string maxSum(string w, char x[], int b[], int n)
{
string ans = "" , res = "" ;
if (w.length() == 1)
return w;
long long maxi = INT_MIN, sum = 0, s = 0;
unordered_map< char , int > mp;
for ( int i = 0; i < n; i++) {
mp[x[i]] = b[i];
}
for ( int i = 0; i < w.length(); i++) {
ans += w[i];
string temp;
if (mp.find(w[i]) == mp.end()) {
sum += w[i];
}
else {
sum += mp[w[i]];
}
if (sum < 0) {
sum = 0;
ans.clear();
}
if (sum > maxi) {
maxi = sum;
res = ans;
}
}
return res;
}
int main()
{
string W = "abcde" ;
int N = 1;
char X[N] = { 'c' };
int B[N] = { -1000 };
cout << maxSum(W, X, B, N) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static String maxSum(String w, char [] x, int [] b, int n)
{
String ans = "" , res = "" ;
if (w.length() == 1 ) {
return w;
}
int maxi = Integer.MIN_VALUE, sum = 0 , s = 0 ;
HashMap<Character, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
mp.put(x[i], b[i]);
}
for ( int i = 0 ; i < w.length(); i++) {
ans += w.charAt(i);
String temp = "" ;
if (!mp.containsKey(w.charAt(i))) {
sum += w.charAt(i);
}
else {
sum += mp.get(w.charAt(i));
}
if (sum < 0 ) {
sum = 0 ;
ans = "" ;
}
if (sum > maxi) {
maxi = sum;
res = ans;
}
}
return res;
}
public static void main(String[] args)
{
String W = "abcde" ;
int N = 1 ;
char [] X = { 'c' };
int [] B = { - 1000 };
System.out.println(maxSum(W, X, B, N));
}
}
|
Python3
import sys
def maxSum(w, x, b, n):
ans = ""
res = ""
if ( len (w) = = 1 ):
return w
maxi = - 1 * sys.maxsize
sum , s = 0 , 0
mp = dict ()
for i in range (n):
mp[x[i]] = b[i]
for i in range ( len (w)):
ans = ans + w[i]
temp = ""
if w[i] in mp.keys():
sum = sum + mp[w[i]]
else :
sum = sum + ord (w[i])
if ( sum < 0 ):
sum = 0
ans = ""
if ( sum > maxi):
maxi = sum
res = ans
return res
W = "abcde"
N = 1
X = [ 'c' ]
B = [ - 1000 ]
print (maxSum(W, X, B, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static string maxSum( string w, char [] x, int [] b,
int n)
{
string ans = "" , res = "" ;
if (w.Length == 1)
return w;
long maxi = Int32.MinValue;
long sum = 0;
long longE = 0;
Dictionary< int , int > mp
= new Dictionary< int , int >();
for ( int i = 0; i < 26; i++) {
mp[i] = 0;
}
for ( int i = 0; i < n; i++) {
mp[x[i] - 'a' ] = b[i];
}
for ( int i = 0; i < w.Length; i++) {
ans += w[i];
string temp;
if (mp[w[i] - 'a' ] == 0) {
sum += w[i];
}
else {
sum += mp[w[i] - 'a' ];
}
if (sum < 0) {
sum = 0;
ans = "" ;
}
if (sum > maxi) {
maxi = sum;
res = ans;
}
}
return res;
}
public static void Main()
{
string W = "abcde" ;
int N = 1;
char [] X = { 'c' };
int [] B = { -1000 };
Console.WriteLine(maxSum(W, X, B, N));
}
}
|
Javascript
function maxSum(w, x, b, n) {
let ans = "" , res = "" ;
if (w.length == 1)
return w;
let maxi = Number.MIN_VALUE, sum = 0, s = 0;
let mp = new Map();
for (let i = 0; i < n; i++) {
mp.set(x[i], b[i]);
}
for (let i = 0; i < w.length; i++) {
ans += w[i];
let temp;
if (!mp.has(w[i])) {
sum += w[i].charCodeAt(0);
}
else {
sum += mp.get(w[i]);
}
if (sum < 0) {
sum = 0;
ans = "" ;
}
if (sum > maxi) {
maxi = sum;
res = ans;
}
}
return res;
}
let W = "abcde" ;
let N = 1;
let X = ['c'];
let B = [-1000];
console.log(maxSum(W, X, B, N) + "<br>" );
|
Time Complexity: O(|W|) where|W| is the length of the string
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...