Skip to content

feat: add solutions to lc problem: No.1061 #4462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,79 @@ function smallestEquivalentString(s1: string, s2: string, baseStr: string): stri
}
```

#### Rust

```rust
impl Solution {
pub fn smallest_equivalent_string(s1: String, s2: String, base_str: String) -> String {
fn find(x: usize, p: &mut Vec<usize>) -> usize {
if p[x] != x {
p[x] = find(p[x], p);
}
p[x]
}

let mut p = (0..26).collect::<Vec<_>>();
for (a, b) in s1.bytes().zip(s2.bytes()) {
let x = (a - b'a') as usize;
let y = (b - b'a') as usize;
let px = find(x, &mut p);
let py = find(y, &mut p);
if px < py {
p[py] = px;
} else {
p[px] = py;
}
}

base_str
.bytes()
.map(|c| (b'a' + find((c - b'a') as usize, &mut p) as u8) as char)
.collect()
}
}
```

#### C#

```cs
public class Solution {
public string SmallestEquivalentString(string s1, string s2, string baseStr) {
int[] p = new int[26];
for (int i = 0; i < 26; i++) {
p[i] = i;
}

int Find(int x) {
if (p[x] != x) {
p[x] = Find(p[x]);
}
return p[x];
}

for (int i = 0; i < s1.Length; i++) {
int x = s1[i] - 'a';
int y = s2[i] - 'a';
int px = Find(x);
int py = Find(y);
if (px < py) {
p[py] = px;
} else {
p[px] = py;
}
}

var res = new System.Text.StringBuilder();
foreach (char c in baseStr) {
int idx = Find(c - 'a');
res.Append((char)(idx + 'a'));
}

return res.ToString();
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,79 @@ function smallestEquivalentString(s1: string, s2: string, baseStr: string): stri
}
```

#### Rust

```rust
impl Solution {
pub fn smallest_equivalent_string(s1: String, s2: String, base_str: String) -> String {
fn find(x: usize, p: &mut Vec<usize>) -> usize {
if p[x] != x {
p[x] = find(p[x], p);
}
p[x]
}

let mut p = (0..26).collect::<Vec<_>>();
for (a, b) in s1.bytes().zip(s2.bytes()) {
let x = (a - b'a') as usize;
let y = (b - b'a') as usize;
let px = find(x, &mut p);
let py = find(y, &mut p);
if px < py {
p[py] = px;
} else {
p[px] = py;
}
}

base_str
.bytes()
.map(|c| (b'a' + find((c - b'a') as usize, &mut p) as u8) as char)
.collect()
}
}
```

#### C#

```cs
public class Solution {
public string SmallestEquivalentString(string s1, string s2, string baseStr) {
int[] p = new int[26];
for (int i = 0; i < 26; i++) {
p[i] = i;
}

int Find(int x) {
if (p[x] != x) {
p[x] = Find(p[x]);
}
return p[x];
}

for (int i = 0; i < s1.Length; i++) {
int x = s1[i] - 'a';
int y = s2[i] - 'a';
int px = Find(x);
int py = Find(y);
if (px < py) {
p[py] = px;
} else {
p[px] = py;
}
}

var res = new System.Text.StringBuilder();
foreach (char c in baseStr) {
int idx = Find(c - 'a');
res.Append((char)(idx + 'a'));
}

return res.ToString();
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class Solution {
public string SmallestEquivalentString(string s1, string s2, string baseStr) {
int[] p = new int[26];
for (int i = 0; i < 26; i++) {
p[i] = i;
}

int Find(int x) {
if (p[x] != x) {
p[x] = Find(p[x]);
}
return p[x];
}

for (int i = 0; i < s1.Length; i++) {
int x = s1[i] - 'a';
int y = s2[i] - 'a';
int px = Find(x);
int py = Find(y);
if (px < py) {
p[py] = px;
} else {
p[px] = py;
}
}

var res = new System.Text.StringBuilder();
foreach (char c in baseStr) {
int idx = Find(c - 'a');
res.Append((char)(idx + 'a'));
}

return res.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
impl Solution {
pub fn smallest_equivalent_string(s1: String, s2: String, base_str: String) -> String {
fn find(x: usize, p: &mut Vec<usize>) -> usize {
if p[x] != x {
p[x] = find(p[x], p);
}
p[x]
}

let mut p = (0..26).collect::<Vec<_>>();
for (a, b) in s1.bytes().zip(s2.bytes()) {
let x = (a - b'a') as usize;
let y = (b - b'a') as usize;
let px = find(x, &mut p);
let py = find(y, &mut p);
if px < py {
p[py] = px;
} else {
p[px] = py;
}
}

base_str
.bytes()
.map(|c| (b'a' + find((c - b'a') as usize, &mut p) as u8) as char)
.collect()
}
}