Skip to content

Commit 36666f0

Browse files
committed
1625. Lexicographically Smallest String After Applying Operations: AC
1 parent 345f9f6 commit 36666f0

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1225,3 +1225,4 @@ mod s1620_coordinate_with_maximum_network_quality;
12251225
mod s1621_number_of_sets_of_k_non_overlapping_line_segments;
12261226
mod s1622_fancy_sequence;
12271227
mod s1624_largest_substring_between_two_equal_characters;
1228+
mod s1625_lexicographically_smallest_string_after_applying_operations;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 than "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 than "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+
*/
56+
pub 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+
63+
impl Solution {
64+
pub fn find_lex_smallest_string(s: String, a: i32, b: i32) -> String {
65+
let chars = s.bytes().collect::<Vec<u8>>();
66+
let n = chars.len();
67+
let mut result = s.clone();
68+
69+
let mut q = std::collections::VecDeque::<String>::new();
70+
q.push_back(result.clone());
71+
72+
let mut visited = std::collections::HashSet::<String>::new();
73+
while !q.is_empty() {
74+
let cur = q.pop_front().unwrap();
75+
if result > cur {
76+
result = cur.clone();
77+
}
78+
79+
let mut ca = cur.bytes().collect::<Vec<u8>>();
80+
81+
for i in (1..n).step_by(2) {
82+
ca[i] = (ca[i] - b'0' + a as u8) % 10 + b'0';
83+
}
84+
85+
let added = String::from_utf8(ca).unwrap();
86+
if visited.insert(added.clone()) {
87+
q.push_back(added);
88+
}
89+
90+
let mut rotated = String::new();
91+
rotated.push_str(&cur[n - b as usize..]);
92+
rotated.push_str(&cur[..n - b as usize]);
93+
94+
if visited.insert(rotated.clone()) {
95+
q.push_back(rotated);
96+
}
97+
}
98+
99+
result
100+
}
101+
}
102+
103+
// submission codes end
104+
105+
#[cfg(test)]
106+
mod tests {
107+
use super::*;
108+
109+
#[test]
110+
fn test_1625_example_1() {
111+
let s = "5525".to_string();
112+
let a = 9;
113+
let b = 2;
114+
115+
let result = "2050".to_string();
116+
117+
assert_eq!(Solution::find_lex_smallest_string(s, a, b), result);
118+
}
119+
120+
#[test]
121+
fn test_1625_example_2() {
122+
let s = "74".to_string();
123+
let a = 5;
124+
let b = 1;
125+
126+
let result = "24".to_string();
127+
128+
assert_eq!(Solution::find_lex_smallest_string(s, a, b), result);
129+
}
130+
131+
#[test]
132+
fn test_1625_example_3() {
133+
let s = "0011".to_string();
134+
let a = 4;
135+
let b = 2;
136+
137+
let result = "0011".to_string();
138+
139+
assert_eq!(Solution::find_lex_smallest_string(s, a, b), result);
140+
}
141+
}

0 commit comments

Comments
 (0)