1946. Largest Number After Mutating Substring Medium
1/**
2 * [1946] Largest Number After Mutating Substring
3 *
4 * 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].
5 * 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]]).
6 * Return a string representing the largest possible integer after mutating (or choosing not to) a single substring of num.
7 * A substring is a contiguous sequence of characters within the string.
8 *
9 * Example 1:
10 *
11 * Input: num = "<u>1</u>32", change = [9,8,5,0,3,6,4,2,6,8]
12 * Output: "<u>8</u>32"
13 * Explanation: Replace the substring "1":
14 * - 1 maps to change[1] = 8.
15 * Thus, "<u>1</u>32" becomes "<u>8</u>32".
16 * "832" is the largest number that can be created, so return it.
17 *
18 * Example 2:
19 *
20 * Input: num = "<u>021</u>", change = [9,4,3,5,7,2,1,9,0,6]
21 * Output: "<u>934</u>"
22 * Explanation: Replace the substring "021":
23 * - 0 maps to change[0] = 9.
24 * - 2 maps to change[2] = 3.
25 * - 1 maps to change[1] = 4.
26 * Thus, "<u>021</u>" becomes "<u>934</u>".
27 * "934" is the largest number that can be created, so return it.
28 *
29 * Example 3:
30 *
31 * Input: num = "5", change = [1,4,7,5,3,2,5,6,9,4]
32 * Output: "5"
33 * Explanation: "5" is already the largest number that can be created, so return it.
34 *
35 *
36 * Constraints:
37 *
38 * 1 <= num.length <= 10^5
39 * num consists of only digits 0-9.
40 * change.length == 10
41 * 0 <= change[d] <= 9
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/largest-number-after-mutating-substring/
47// discuss: https://leetcode.com/problems/largest-number-after-mutating-substring/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn maximum_number(num: String, change: Vec<i32>) -> String {
53 String::new()
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_1946() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.