Skip to content

Commit 5e1d07a

Browse files
committed
leetcode
1 parent 1e09ea9 commit 5e1d07a

4 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
3+
-* 1061. Lexicographically Smallest Equivalent String *-
4+
5+
6+
You are given two strings of the same length s1 and s2 and a string baseStr.
7+
8+
We say s1[i] and s2[i] are equivalent characters.
9+
10+
For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'.
11+
Equivalent characters follow the usual rules of any equivalence relation:
12+
13+
Reflexivity: 'a' == 'a'.
14+
Symmetry: 'a' == 'b' implies 'b' == 'a'.
15+
Transitivity: 'a' == 'b' and 'b' == 'c' implies 'a' == 'c'.
16+
For example, given the equivalency information from s1 = "abc" and s2 = "cde", "acd" and "aab" are equivalent strings of baseStr = "eed", and "aab" is the lexicographically smallest equivalent string of baseStr.
17+
18+
Return the lexicographically smallest equivalent string of baseStr by using the equivalency information from s1 and s2.
19+
20+
21+
22+
Example 1:
23+
24+
Input: s1 = "parker", s2 = "morris", baseStr = "parser"
25+
Output: "makkek"
26+
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
27+
The characters in each group are equivalent and sorted in lexicographical order.
28+
So the answer is "makkek".
29+
Example 2:
30+
31+
Input: s1 = "hello", s2 = "world", baseStr = "hold"
32+
Output: "hdld"
33+
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
34+
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
35+
Example 3:
36+
37+
Input: s1 = "leet-code", s2 = "programs", baseStr = "source-code"
38+
Output: "aauaaaaada"
39+
Explanation: We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
40+
41+
42+
Constraints:
43+
44+
1 <= s1.length, s2.length, baseStr <= 1000
45+
s1.length == s2.length
46+
s1, s2, and baseStr consist of lowercase English letters.
47+
48+
*/
49+
50+
class UnionFind {
51+
List<int> parent = List.filled(26, 0);
52+
53+
UnionFind() {
54+
for (int i = 0; i < 26; ++i) {
55+
parent[i] = i;
56+
}
57+
}
58+
59+
int find(int a) {
60+
if (a != parent[a]) {
61+
parent[a] = find(parent[a]);
62+
}
63+
return parent[a];
64+
}
65+
66+
void union(int a, int b) {
67+
int parentA = find(a);
68+
int parentB = find(b);
69+
70+
if (parentA == parentB) return;
71+
72+
if (parentA < parentB) {
73+
parent[parentB] = parentA;
74+
} else {
75+
parent[parentA] = parentB;
76+
}
77+
}
78+
}
79+
80+
class A {
81+
String smallestEquivalentString(String s1, String s2, String baseStr) {
82+
UnionFind uf = UnionFind();
83+
for (int i = 0; i < s1.length; ++i) {
84+
uf.union(s1.codeUnitAt(i) - 'a'.codeUnitAt(0),
85+
s2.codeUnitAt(i) - 'a'.codeUnitAt(0));
86+
}
87+
88+
StringBuffer sb = StringBuffer();
89+
for (String ch in baseStr.split("")) {
90+
sb.writeCharCode(
91+
('a'.codeUnitAt(0) + uf.find(ch.codeUnitAt(0) - 'a'.codeUnitAt(0))));
92+
}
93+
return sb.toString();
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# A
2+
3+
## Solution - Union Find
4+
5+
```dart
6+
class UnionFind {
7+
List<int> parent = List.filled(26, 0);
8+
9+
UnionFind() {
10+
for (int i = 0; i < 26; ++i) {
11+
parent[i] = i;
12+
}
13+
}
14+
15+
int find(int a) {
16+
if (a != parent[a]) {
17+
parent[a] = find(parent[a]);
18+
}
19+
return parent[a];
20+
}
21+
22+
void union(int a, int b) {
23+
int parentA = find(a);
24+
int parentB = find(b);
25+
26+
if (parentA == parentB) return;
27+
28+
if (parentA < parentB) {
29+
parent[parentB] = parentA;
30+
} else {
31+
parent[parentA] = parentB;
32+
}
33+
}
34+
}
35+
36+
/* ================================Solution===================================== */
37+
38+
class Solution {
39+
String smallestEquivalentString(String s1, String s2, String baseStr) {
40+
UnionFind uf = UnionFind();
41+
for (int i = 0; i < s1.length; ++i) {
42+
uf.union(s1.codeUnitAt(i) - 'a'.codeUnitAt(0),
43+
s2.codeUnitAt(i) - 'a'.codeUnitAt(0));
44+
}
45+
46+
StringBuffer sb = StringBuffer();
47+
for (String ch in baseStr.split("")) {
48+
sb.writeCharCode(
49+
('a'.codeUnitAt(0) + uf.find(ch.codeUnitAt(0) - 'a'.codeUnitAt(0))));
50+
}
51+
return sb.toString();
52+
}
53+
}
54+
````

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
183183
- [**1443.** Minimum Time to Collect All Apples in a Tree](MinimumTimeToCollectAllApplesInATree/minimum_time_to_collect_all_apples_in_a_tree.dart)
184184
- [**1519.** Number of Nodes in the Sub-Tree With the Same Label](NumberOfNodesInTheSub-TreeWithTheSameLabel/number_of_nodes_in_the_sub_tree_with_the_same_label.dart)
185185
- [**2246.** Longest Path With Different Adjacent Characters](LongestPathWithDifferentAdjacentCharacters/longest_path_with_Different_adjacent_characters.dart)
186+
- [**1061.** Lexicographically Smallest Equivalent String](LexicographicallySmallestEquivalentString/lexicographically_smallest_equivalent_string.dart)
186187

187188
## Reach me via
188189

0 commit comments

Comments
 (0)