Maximize the given number by replacing a segment of digits with the alternate digits given

Given many N digits. We are also given 10 numbers which represents the alternate number for all the one-digit numbers from 0 to 9. We can replace any digit in N with the given alternate digit to it, but we are only allowed to replace any consecutive segment of numbers for once only, the task is to replace any consecutive segment of numbers such that the obtained number is the largest of all possible replacements.Â
Examples:Â Â
Input: n = 1337, a[] = {0, 1, 2, 5, 4, 6, 6, 3, 1, 9}Â
Output: 1557Â
1 can be replaced with 1 as a[1] = 1 (No effect)Â
3 can be replaced with 5Â
7 can be replaced with 3 (isn’t required if the number needs to be maximized)
Input: number = 11111, a[] = {0, 5, 2, 5, 4, 6, 6, 3, 1, 9}Â
Output: 55555Â Â
Approach: Since we need to get the largest number possible, hence iterate from the left and find the number whose alternate number is greater than the current one, and keep replacing the upcoming numbers till the alternate number is not smaller.Â
Below is the implementation of the above approach:Â Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the maximized numberstring get_maximum(string s, int a[]){Â Â Â Â int n = s.size();Â
    // Iterate till the end of the string    for (int i = 0; i < n; i++) {Â
        // Check if it is greater or not        if (s[i] - '0' < a[s[i] - '0']) {            int j = i;Â
            // Replace with the alternate till smaller            while (j < n && (s[j] - '0' <= a[s[j] - '0'])) {                s[j] = '0' + a[s[j] - '0'];                j++;            }Â
            return s;        }    }Â
    // Return original s in case    // no change took place    return s;}Â
// Driver Codeint main(){Â Â Â Â string s = "1337";Â Â Â Â int a[] = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };Â Â Â Â cout << get_maximum(s, a);Â
    return 0;} |
Java
// Java implementation of the approachimport java.io.*;class GFG{Â
// Function to return the maximized numberstatic String get_maximum(char[] s, int a[]){Â Â Â Â int n = s.length;Â
    // Iterate till the end of the string    for (int i = 0; i < n; i++)    {Â
        // Check if it is greater or not        if (s[i] - '0' < a[s[i] - '0'])         {            int j = i;Â
            // Replace with the alternate till smaller            while (j < n && (s[j] - '0' <= a[s[j] - '0']))             {                s[j] = (char) ('0' + a[s[j] - '0']);                j++;            }Â
            return String.valueOf(s);        }    }Â
    // Return original s in case    // no change took place    return String.valueOf(s);}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â String s = "1337";Â Â Â Â int a[] = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };Â Â Â Â System.out.println(get_maximum(s.toCharArray(), a));}}Â
/* This code contributed by PrinciRaj1992 */ |
Python3
# Python3 implementation of the approach Â
# Function to return the maximized number def get_maximum(s, a) :     s = list(s)    n = len(s)          # Iterate till the end of the string     for i in range(n) :                 # Check if it is greater or not        if (ord(s[i]) - ord('0') < a[ord(s[i]) - ord('0')]) :            j = i                         # Replace with the alternate till smaller             while (j < n and (ord(s[j]) - ord('0') <=                            a[ord(s[j]) - ord('0')])) :                s[j] = chr(ord('0') + a[ord(s[j]) - ord('0')])                j += 1                         return "".join(s);         # Return original s in case     # no change took place     return s Â
Â
# Driver Code if __name__ == "__main__" :Â Â Â Â Â Â Â Â Â s = "1337"Â Â Â Â a = [ 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 ] Â Â Â Â print(get_maximum(s, a))Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â
class GFG{Â
// Function to return the maximized numberstatic String get_maximum(char[] s, int[] a){Â Â Â Â int n = s.Length;Â
    // Iterate till the end of the string    for (int i = 0; i < n; i++)    {Â
        // Check if it is greater or not        if (s[i] - '0' < a[s[i] - '0'])         {            int j = i;Â
            // Replace with the alternate till smaller            while (j < n && (s[j] - '0' <= a[s[j] - '0']))             {                s[j] = (char) ('0' + a[s[j] - '0']);                j++;            }Â
            return String.Join("",s);        }    }Â
    // Return original s in case    // no change took place    return String.Join("",s);}Â
// Driver Codepublic static void Main(String[] args){Â Â Â Â String s = "1337";Â Â Â Â int[] a = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };Â Â Â Â Console.WriteLine(get_maximum(s.ToCharArray(), a));}}Â
// This code contributed by Rajput-Ji |
PHP
<?php// PHP implementation of the approach// Function to return the maximized numberÂ
function get_maximum($s, $a){Â Â Â Â $n = strlen($s);Â
    // Iterate till the end of the string    for ($i = 0; $i < $n; $i++)    {Â
        // Check if it is greater or not        if ($s[$i] - '0' < $a[$s[$i] - '0'])         {            $j = $i;Â
            // Replace with the alternate till smaller            while ($j < $n && ($s[$j] - '0' <= $a[$s[$j] - '0']))            {                $s[$j] = '0' + $a[$s[$j] - '0'];                $j++;            }Â
            return $s;        }    }Â
    // Return original s in case    // no change took place    return $s;}Â
    // Driver Code    $s = "1337";    $a = array( 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 );    echo get_maximum($s, $a);         // This Code is contributed is Tushill.?> |
Javascript
function getMaximum(s, a) {Â Â Â Â let n = s.length;Â // Store the length of the stringÂ
    // Iterate through the string    for (let i = 0; i < n; i++) {        // Check if the current digit is less than the alternate digit        if (parseInt(s[i]) < a[parseInt(s[i])]) {            let j = i;            // Replace with alternate digit as long as it is less than or equal to the alternate digit            while (j < n && (parseInt(s[j]) <= a[parseInt(s[j])])) {                s = s.slice(0, j) + a[parseInt(s[j])] + s.slice(j + 1); // Replace the digit with alternate digit                j++;            }            return s; // Return the maximized string        }    }    return s; // Return the original string if no change took place}Â
console.log(getMaximum("1337", [0, 1, 2, 5, 4, 6, 6, 3, 1, 9])); // logs "5567"//code by Satyam Nayak |
1557
Â
Time Complexity: Â O(n2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



