Largest Number After Mutating Substring LeetCode solution in C++

1
// change first longest encountered substring from left which can make string
2
// greater in value
3
class Solution {
4
public:
5
string maximumNumber(string num, vector<int> &change) {
6
int n = num.size();
7
for (int i = 0; i < n; i++) {
8
int x = num[i] - '0';
9
if (x < change[x]) { // checking whether mutating this char will increment
10
// the value or not
11
// the mutation continues till mutating the char will increase the value
12
while (i < n and num[i] - '0' <= change[num[i] - '0']) {
13
num[i++] = change[num[i] - '0'] + '0';
14
}
15
// break the loop when substring ends
16
break;
17
}
18
}
19
return num;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0