From 1a66cd5efaf1f0af8c0f37303a75aa7e59c7fe94 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 5 Jun 2025 06:24:30 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1061 No.1061.Lexicographically Smallest Equivalent String --- .../README.md | 73 +++++++++++++++++++ .../README_EN.md | 73 +++++++++++++++++++ .../Solution.cs | 35 +++++++++ .../Solution.rs | 28 +++++++ 4 files changed, 209 insertions(+) create mode 100644 solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cs create mode 100644 solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.rs diff --git a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md index e09b744474194..b8b2a3f3c5a9d 100644 --- a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md +++ b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md @@ -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 { + if p[x] != x { + p[x] = find(p[x], p); + } + p[x] + } + + let mut p = (0..26).collect::>(); + 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(); + } +} +``` + diff --git a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README_EN.md b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README_EN.md index 087107581dd3f..6b6fe19389562 100644 --- a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README_EN.md +++ b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README_EN.md @@ -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 { + if p[x] != x { + p[x] = find(p[x], p); + } + p[x] + } + + let mut p = (0..26).collect::>(); + 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(); + } +} +``` + diff --git a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cs b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cs new file mode 100644 index 0000000000000..37ad347c91d1d --- /dev/null +++ b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cs @@ -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(); + } +} diff --git a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.rs b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.rs new file mode 100644 index 0000000000000..fbcee2927a315 --- /dev/null +++ b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.rs @@ -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 { + if p[x] != x { + p[x] = find(p[x], p); + } + p[x] + } + + let mut p = (0..26).collect::>(); + 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() + } +}