You are given a string num, which represents a large integer. You are also given a 0-indexed integer array change of length 10 that maps each digit 0-9 to another digit. More formally, digit d maps to digit change[d].

You may choose to mutate a single substring of num. To mutate a substring, replace each digit num[i] with the digit it maps to in change (i.e. replace num[i] with change[num[i]]).

Return a string representing the largest possible integer after mutating (or choosing not to) a single substring of num.

A substring is a contiguous sequence of characters within the string.

&nbsp;
Example 1:

Input: num = "132", change = [9,8,5,0,3,6,4,2,6,8]
Output: "832"
Explanation: Replace the substring "1":
- 1 maps to change[1] = 8.
Thus, "132" becomes "832".
"832" is the largest number that can be created, so return it.


Example 2:

Input: num = "021", change = [9,4,3,5,7,2,1,9,0,6]
Output: "934"
Explanation: Replace the substring "021":
- 0 maps to change[0] = 9.
- 2 maps to change[2] = 3.
- 1 maps to change[1] = 4.
Thus, "021" becomes "934".
"934" is the largest number that can be created, so return it.


Example 3:

Input: num = "5", change = [1,4,7,5,3,2,5,6,9,4]
Output: "5"
Explanation: "5" is already the largest number that can be created, so return it.


&nbsp;
Constraints:


	1 &lt;= num.length &lt;= 105
	num consists of only digits 0-9.
	change.length == 10
	0 &lt;= change[d] &lt;= 9


class Solution:
def maximumNumber(self, num: str, change: List[int]) -> str:
    flag=0
    ls=list(num)
    for i in range(len(ls)):
        k=int(ls[i])
        if change[k]>k:
            ls[i]=str(change[k])
            flag=1
        elif flag==1 and change[k]<k:
            break
    
    return "".join(ls)
class Solution {
    public String maximumNumber(String num, int[] change) {
        int i=0, n=num.length(), startIndex=-1, substringLength=0;
        
        // traverse through each digit in the input string
        while(i<n) {
            int digit=num.charAt(i)-48;
            // when we encounter a digit which has greater change
            if(change[digit] > digit) {
                startIndex = i;
                // keep on replacing subsequent characters with with the change if they also have greater change
                while(i<n) {
                    digit=num.charAt(i)-48;
                    if(change[digit] < digit) {
                        break;
                    }
                    i+=1;
                }
                substringLength = i-startIndex;
                break;
            }
            i+=1;
        }
        
        // Note: Using String Builder to ensure linear time complexity as java strings are immutable
        StringBuilder result=new StringBuilder("");
        for(int j=0; j<n; j++) {
            int digit=num.charAt(j)-48;
            if(j>=startIndex && j<startIndex+substringLength) {
                result.append(change[digit]);    
            } else {
                result.append(digit);   
            }
        }
        
        return result.toString();
    }
}
//change first longest encountered substring from left which can make string greater in value
class Solution {
public:
    string maximumNumber(string num, vector<int>& change){
        int n=num.size();
        for(int i=0;i<n;i++){
            int x=num[i]-'0';
            if(x < change[x]){//checking whether mutating this char will increment the value or not
            //the mutation continues till mutating the char will increase the value
                while(i<n and num[i]-'0'<=change[num[i]-'0']){
                    num[i++]=change[num[i]-'0']+'0';
                }
                //break the loop when substring ends
                break;
            }
        }
        return num;
    }
};
var maximumNumber = function(num, change) {
    const digits = num.split("");

    let started = false;

    for (let i = 0; i < digits.length; ++i) {
        const origDig = digits[i];
        const changeDig = change[origDig];

        if (changeDig > origDig) {
            started = true;
            digits[i] = changeDig;
        }
        else if (changeDig < origDig && started) {
            break;
        }
    }

    return digits.join("");
};
Largest Number After Mutating Substring