Skip to content

Commit 22c9caf

Browse files
authored
feat: add rust solution to lc problem: No.1202 (doocs#1191)
1 parent 88aa72d commit 22c9caf

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

solution/1200-1299/1202.Smallest String With Swaps/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,67 @@ public:
170170
};
171171
```
172172
173+
### **Rust**
174+
175+
```rust
176+
impl Solution {
177+
#[allow(dead_code)]
178+
pub fn smallest_string_with_swaps(s: String, pairs: Vec<Vec<i32>>) -> String {
179+
let n = s.as_bytes().len();
180+
let s = s.as_bytes();
181+
let mut disjoint_set: Vec<usize> = vec![0; n];
182+
let mut str_vec: Vec<Vec<u8>> = vec![Vec::new(); n];
183+
let mut ret_str = String::new();
184+
185+
// Initialize the disjoint set
186+
for i in 0..n {
187+
disjoint_set[i] = i;
188+
}
189+
190+
// Union the pairs
191+
for pair in pairs {
192+
Self::union(pair[0] as usize, pair[1] as usize, &mut disjoint_set);
193+
}
194+
195+
// Initialize the return vector
196+
for (i, c) in s.iter().enumerate() {
197+
let p_c = Self::find(i, &mut disjoint_set);
198+
str_vec[p_c].push(*c);
199+
}
200+
201+
// Sort the return vector in reverse order
202+
for cur_vec in &mut str_vec {
203+
cur_vec.sort();
204+
cur_vec.reverse();
205+
}
206+
207+
// Construct the return string
208+
for i in 0..n {
209+
let index = Self::find(i, &mut disjoint_set);
210+
ret_str.push(str_vec[index].last().unwrap().clone() as char);
211+
str_vec[index].pop();
212+
}
213+
214+
ret_str
215+
}
216+
217+
#[allow(dead_code)]
218+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
219+
if d_set[x] != x {
220+
d_set[x] = Self::find(d_set[x], d_set);
221+
}
222+
d_set[x]
223+
}
224+
225+
#[allow(dead_code)]
226+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
227+
let p_x = Self::find(x, d_set);
228+
let p_y = Self::find(y, d_set);
229+
d_set[p_x] = p_y;
230+
}
231+
}
232+
```
233+
173234
### **Go**
174235

175236
```go

solution/1200-1299/1202.Smallest String With Swaps/README_EN.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,67 @@ public:
155155
};
156156
```
157157
158+
### **Rust**
159+
160+
```rust
161+
impl Solution {
162+
#[allow(dead_code)]
163+
pub fn smallest_string_with_swaps(s: String, pairs: Vec<Vec<i32>>) -> String {
164+
let n = s.as_bytes().len();
165+
let s = s.as_bytes();
166+
let mut disjoint_set: Vec<usize> = vec![0; n];
167+
let mut str_vec: Vec<Vec<u8>> = vec![Vec::new(); n];
168+
let mut ret_str = String::new();
169+
170+
// Initialize the disjoint set
171+
for i in 0..n {
172+
disjoint_set[i] = i;
173+
}
174+
175+
// Union the pairs
176+
for pair in pairs {
177+
Self::union(pair[0] as usize, pair[1] as usize, &mut disjoint_set);
178+
}
179+
180+
// Initialize the return vector
181+
for (i, c) in s.iter().enumerate() {
182+
let p_c = Self::find(i, &mut disjoint_set);
183+
str_vec[p_c].push(*c);
184+
}
185+
186+
// Sort the return vector in reverse order
187+
for cur_vec in &mut str_vec {
188+
cur_vec.sort();
189+
cur_vec.reverse();
190+
}
191+
192+
// Construct the return string
193+
for i in 0..n {
194+
let index = Self::find(i, &mut disjoint_set);
195+
ret_str.push(str_vec[index].last().unwrap().clone() as char);
196+
str_vec[index].pop();
197+
}
198+
199+
ret_str
200+
}
201+
202+
#[allow(dead_code)]
203+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
204+
if d_set[x] != x {
205+
d_set[x] = Self::find(d_set[x], d_set);
206+
}
207+
d_set[x]
208+
}
209+
210+
#[allow(dead_code)]
211+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
212+
let p_x = Self::find(x, d_set);
213+
let p_y = Self::find(y, d_set);
214+
d_set[p_x] = p_y;
215+
}
216+
}
217+
```
218+
158219
### **Go**
159220

160221
```go
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn smallest_string_with_swaps(s: String, pairs: Vec<Vec<i32>>) -> String {
4+
let n = s.as_bytes().len();
5+
let s = s.as_bytes();
6+
let mut disjoint_set: Vec<usize> = vec![0; n];
7+
let mut str_vec: Vec<Vec<u8>> = vec![Vec::new(); n];
8+
let mut ret_str = String::new();
9+
10+
// Initialize the disjoint set
11+
for i in 0..n {
12+
disjoint_set[i] = i;
13+
}
14+
15+
// Union the pairs
16+
for pair in pairs {
17+
Self::union(pair[0] as usize, pair[1] as usize, &mut disjoint_set);
18+
}
19+
20+
// Initialize the return vector
21+
for (i, c) in s.iter().enumerate() {
22+
let p_c = Self::find(i, &mut disjoint_set);
23+
str_vec[p_c].push(*c);
24+
}
25+
26+
// Sort the return vector in reverse order
27+
for cur_vec in &mut str_vec {
28+
cur_vec.sort();
29+
cur_vec.reverse();
30+
}
31+
32+
// Construct the return string
33+
for i in 0..n {
34+
let index = Self::find(i, &mut disjoint_set);
35+
ret_str.push(str_vec[index].last().unwrap().clone() as char);
36+
str_vec[index].pop();
37+
}
38+
39+
ret_str
40+
}
41+
42+
#[allow(dead_code)]
43+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
44+
if d_set[x] != x {
45+
d_set[x] = Self::find(d_set[x], d_set);
46+
}
47+
d_set[x]
48+
}
49+
50+
#[allow(dead_code)]
51+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
52+
let p_x = Self::find(x, d_set);
53+
let p_y = Self::find(y, d_set);
54+
d_set[p_x] = p_y;
55+
}
56+
}

0 commit comments

Comments
 (0)