1625. Lexicographically Smallest String After Applying Operations Medium
1/**
2 * [1625] Lexicographically Smallest String After Applying Operations
3 *
4 * You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.
5 * You can apply either of the following two operations any number of times and in any order on s:
6 *
7 * Add a to all odd indices of s (0-indexed). Digits post 9 are cycled back to 0. For example, if s = "3456" and a = 5, s becomes "3951".
8 * Rotate s to the right by b positions. For example, if s = "3456" and b = 1, s becomes "6345".
9 *
10 * Return the lexicographically smallest string you can obtain by applying the above operations any number of times on s.
11 * A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. For example, "0158" is lexicographically smaller than "0190" because the first position they differ is at the third letter, and '5' comes before '9'.
12 *
13 * Example 1:
14 *
15 * Input: s = "5525", a = 9, b = 2
16 * Output: "2050"
17 * Explanation: We can apply the following operations:
18 * Start: "5525"
19 * Rotate: "2555"
20 * Add: "2454"
21 * Add: "2353"
22 * Rotate: "5323"
23 * Add: "5222"
24 * Add: "5121"
25 * Rotate: "2151"
26 * Add: "2050"
27 * There is no way to obtain a string that is lexicographically smaller then "2050".
28 *
29 * Example 2:
30 *
31 * Input: s = "74", a = 5, b = 1
32 * Output: "24"
33 * Explanation: We can apply the following operations:
34 * Start: "74"
35 * Rotate: "47"
36 * Add: "42"
37 * Rotate: "24"
38 * There is no way to obtain a string that is lexicographically smaller then "24".
39 *
40 * Example 3:
41 *
42 * Input: s = "0011", a = 4, b = 2
43 * Output: "0011"
44 * Explanation: There are no sequence of operations that will give us a lexicographically smaller string than "0011".
45 *
46 *
47 * Constraints:
48 *
49 * 2 <= s.length <= 100
50 * s.length is even.
51 * s consists of digits from 0 to 9 only.
52 * 1 <= a <= 9
53 * 1 <= b <= s.length - 1
54 *
55 */
56pub struct Solution {}
57
58// problem: https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/
59// discuss: https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/discuss/?currentPage=1&orderBy=most_votes&query=
60
61// submission codes start here
62
63impl Solution {
64 pub fn find_lex_smallest_string(s: String, a: i32, b: i32) -> String {
65 String::new()
66 }
67}
68
69// submission codes end
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn test_1625() {
77 }
78}
79
Back
© 2025 bowen.ge All Rights Reserved.