Skip to content

Commit 1a66cd5

Browse files
committed
feat: add solutions to lc problem: No.1061
No.1061.Lexicographically Smallest Equivalent String
1 parent c520c5a commit 1a66cd5

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,79 @@ function smallestEquivalentString(s1: string, s2: string, baseStr: string): stri
249249
}
250250
```
251251

252+
#### Rust
253+
254+
```rust
255+
impl Solution {
256+
pub fn smallest_equivalent_string(s1: String, s2: String, base_str: String) -> String {
257+
fn find(x: usize, p: &mut Vec<usize>) -> usize {
258+
if p[x] != x {
259+
p[x] = find(p[x], p);
260+
}
261+
p[x]
262+
}
263+
264+
let mut p = (0..26).collect::<Vec<_>>();
265+
for (a, b) in s1.bytes().zip(s2.bytes()) {
266+
let x = (a - b'a') as usize;
267+
let y = (b - b'a') as usize;
268+
let px = find(x, &mut p);
269+
let py = find(y, &mut p);
270+
if px < py {
271+
p[py] = px;
272+
} else {
273+
p[px] = py;
274+
}
275+
}
276+
277+
base_str
278+
.bytes()
279+
.map(|c| (b'a' + find((c - b'a') as usize, &mut p) as u8) as char)
280+
.collect()
281+
}
282+
}
283+
```
284+
285+
#### C#
286+
287+
```cs
288+
public class Solution {
289+
public string SmallestEquivalentString(string s1, string s2, string baseStr) {
290+
int[] p = new int[26];
291+
for (int i = 0; i < 26; i++) {
292+
p[i] = i;
293+
}
294+
295+
int Find(int x) {
296+
if (p[x] != x) {
297+
p[x] = Find(p[x]);
298+
}
299+
return p[x];
300+
}
301+
302+
for (int i = 0; i < s1.Length; i++) {
303+
int x = s1[i] - 'a';
304+
int y = s2[i] - 'a';
305+
int px = Find(x);
306+
int py = Find(y);
307+
if (px < py) {
308+
p[py] = px;
309+
} else {
310+
p[px] = py;
311+
}
312+
}
313+
314+
var res = new System.Text.StringBuilder();
315+
foreach (char c in baseStr) {
316+
int idx = Find(c - 'a');
317+
res.Append((char)(idx + 'a'));
318+
}
319+
320+
return res.ToString();
321+
}
322+
}
323+
```
324+
252325
<!-- tabs:end -->
253326

254327
<!-- solution:end -->

solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README_EN.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,79 @@ function smallestEquivalentString(s1: string, s2: string, baseStr: string): stri
250250
}
251251
```
252252

253+
#### Rust
254+
255+
```rust
256+
impl Solution {
257+
pub fn smallest_equivalent_string(s1: String, s2: String, base_str: String) -> String {
258+
fn find(x: usize, p: &mut Vec<usize>) -> usize {
259+
if p[x] != x {
260+
p[x] = find(p[x], p);
261+
}
262+
p[x]
263+
}
264+
265+
let mut p = (0..26).collect::<Vec<_>>();
266+
for (a, b) in s1.bytes().zip(s2.bytes()) {
267+
let x = (a - b'a') as usize;
268+
let y = (b - b'a') as usize;
269+
let px = find(x, &mut p);
270+
let py = find(y, &mut p);
271+
if px < py {
272+
p[py] = px;
273+
} else {
274+
p[px] = py;
275+
}
276+
}
277+
278+
base_str
279+
.bytes()
280+
.map(|c| (b'a' + find((c - b'a') as usize, &mut p) as u8) as char)
281+
.collect()
282+
}
283+
}
284+
```
285+
286+
#### C#
287+
288+
```cs
289+
public class Solution {
290+
public string SmallestEquivalentString(string s1, string s2, string baseStr) {
291+
int[] p = new int[26];
292+
for (int i = 0; i < 26; i++) {
293+
p[i] = i;
294+
}
295+
296+
int Find(int x) {
297+
if (p[x] != x) {
298+
p[x] = Find(p[x]);
299+
}
300+
return p[x];
301+
}
302+
303+
for (int i = 0; i < s1.Length; i++) {
304+
int x = s1[i] - 'a';
305+
int y = s2[i] - 'a';
306+
int px = Find(x);
307+
int py = Find(y);
308+
if (px < py) {
309+
p[py] = px;
310+
} else {
311+
p[px] = py;
312+
}
313+
}
314+
315+
var res = new System.Text.StringBuilder();
316+
foreach (char c in baseStr) {
317+
int idx = Find(c - 'a');
318+
res.Append((char)(idx + 'a'));
319+
}
320+
321+
return res.ToString();
322+
}
323+
}
324+
```
325+
253326
<!-- tabs:end -->
254327

255328
<!-- solution:end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Solution {
2+
public string SmallestEquivalentString(string s1, string s2, string baseStr) {
3+
int[] p = new int[26];
4+
for (int i = 0; i < 26; i++) {
5+
p[i] = i;
6+
}
7+
8+
int Find(int x) {
9+
if (p[x] != x) {
10+
p[x] = Find(p[x]);
11+
}
12+
return p[x];
13+
}
14+
15+
for (int i = 0; i < s1.Length; i++) {
16+
int x = s1[i] - 'a';
17+
int y = s2[i] - 'a';
18+
int px = Find(x);
19+
int py = Find(y);
20+
if (px < py) {
21+
p[py] = px;
22+
} else {
23+
p[px] = py;
24+
}
25+
}
26+
27+
var res = new System.Text.StringBuilder();
28+
foreach (char c in baseStr) {
29+
int idx = Find(c - 'a');
30+
res.Append((char)(idx + 'a'));
31+
}
32+
33+
return res.ToString();
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn smallest_equivalent_string(s1: String, s2: String, base_str: String) -> String {
3+
fn find(x: usize, p: &mut Vec<usize>) -> usize {
4+
if p[x] != x {
5+
p[x] = find(p[x], p);
6+
}
7+
p[x]
8+
}
9+
10+
let mut p = (0..26).collect::<Vec<_>>();
11+
for (a, b) in s1.bytes().zip(s2.bytes()) {
12+
let x = (a - b'a') as usize;
13+
let y = (b - b'a') as usize;
14+
let px = find(x, &mut p);
15+
let py = find(y, &mut p);
16+
if px < py {
17+
p[py] = px;
18+
} else {
19+
p[px] = py;
20+
}
21+
}
22+
23+
base_str
24+
.bytes()
25+
.map(|c| (b'a' + find((c - b'a') as usize, &mut p) as u8) as char)
26+
.collect()
27+
}
28+
}

0 commit comments

Comments
 (0)