Skip to content

Commit f74cf04

Browse files
committed
1946. Largest Number After Mutating Substring: AC
1 parent 3250101 commit f74cf04

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Diff for: src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1467,3 +1467,4 @@ mod s1942_the_number_of_the_smallest_unoccupied_chair;
14671467
mod s1943_describe_the_painting;
14681468
mod s1944_number_of_visible_people_in_a_queue;
14691469
mod s1945_sum_of_digits_of_string_after_convert;
1470+
mod s1946_largest_number_after_mutating_substring;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
*/
44+
pub 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+
51+
impl Solution {
52+
pub fn maximum_number(num: String, change: Vec<i32>) -> String {
53+
let mut bytes = num.into_bytes();
54+
55+
bytes
56+
.iter_mut()
57+
.map(|b| {
58+
let new = change[(*b - b'0') as usize] as u8 + b'0';
59+
(b, new)
60+
})
61+
.skip_while(|(b, new)| **b >= *new)
62+
.take_while(|(b, new)| **b <= *new)
63+
.for_each(|(b, new)| *b = new);
64+
65+
String::from_utf8(bytes).unwrap()
66+
}
67+
}
68+
69+
// submission codes end
70+
71+
#[cfg(test)]
72+
mod tests {
73+
use super::*;
74+
75+
#[test]
76+
fn test_1946_example_1() {
77+
let num = "132".to_string();
78+
let change = vec![9, 8, 5, 0, 3, 6, 4, 2, 6, 8];
79+
80+
let result = "832".to_string();
81+
82+
assert_eq!(Solution::maximum_number(num, change), result);
83+
}
84+
85+
#[test]
86+
fn test_1946_example_2() {
87+
let num = "021".to_string();
88+
let change = vec![9, 4, 3, 5, 7, 2, 1, 9, 0, 6];
89+
90+
let result = "934".to_string();
91+
92+
assert_eq!(Solution::maximum_number(num, change), result);
93+
}
94+
95+
#[test]
96+
fn test_1946_example_3() {
97+
let num = "5".to_string();
98+
let change = vec![1, 4, 7, 5, 3, 2, 5, 6, 9, 4];
99+
100+
let result = "5".to_string();
101+
102+
assert_eq!(Solution::maximum_number(num, change), result);
103+
}
104+
}

0 commit comments

Comments
 (0)