2
public String maximumNumber(String num, int[] change) {3
int i = 0, n = num.length(), startIndex = -1, substringLength = 0;5
// traverse through each digit in the input string7
int digit = num.charAt(i) - 48;8
// when we encounter a digit which has greater change9
if (change[digit] > digit) {11
// keep on replacing subsequent characters with with the change if they also have greater14
digit = num.charAt(i) - 48;15
if (change[digit] < digit) {20
substringLength = i - startIndex;26
// Note: Using String Builder to ensure linear time complexity as java strings are immutable27
StringBuilder result = new StringBuilder("");28
for (int j = 0; j < n; j++) {29
int digit = num.charAt(j) - 48;30
if (j >= startIndex && j < startIndex + substringLength) {31
result.append(change[digit]);37
return result.toString();